<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

	<title>SLaks.Blog</title>
	<link rel="alternate"	type="text/html"			href="https://blog.slaks.net/"/>
	<link rel="self"		type="application/atom+xml"	href="https://blog.slaks.net/feeds/posts/default.xml"/>
	<updated>2024-02-25T19:45:40+00:00</updated>
	<id>tag:blogger.com,1999:blog-4137132196361303955</id>
	<author>
		<name>Schabse Laks</name>
		<uri>http://slaks.net</uri>
		<email>Dev@SLaks.Net</email>
	</author>

	
	<entry>
		<id>https://blog.slaks.net_posts/2016-12-26-code-snippets-removable-equals-tokens.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2016-12-26/code-snippets-removable-equals-tokens/"/>
		<title type="text">Code Snippets: Removable = Tokens</title>
		<updated>2016-12-26T00:00:00+00:00</updated>
		<published>2016-12-26T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="code-snippets" term="code-snippets" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;center&gt;&lt;em&gt;Or, torturing compilers for fun &lt;strike&gt;and profit&lt;/strike&gt;.&lt;/em&gt;&lt;/center&gt;

&lt;p&gt;I recently tweeted an interesting C# challenge:&lt;/p&gt;

&lt;div class=&quot;twitter-frame&quot;&gt;
	&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;C# Quiz: Construct a valid program which remains valid when a single = token is removed.&lt;/p&gt;&amp;mdash; Schabse Laks (@Schabse) &lt;a href=&quot;https://twitter.com/Schabse/status/811644097505193984&quot;&gt;December 21, 2016&lt;/a&gt;&lt;/blockquote&gt;
	&lt;script async=&quot;&quot; src=&quot;//platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;p&gt;This has enough solutions to warrant a blog post, explaining how each answer works.&lt;/p&gt;

&lt;p&gt;Note that you must remove an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; character &lt;em&gt;which is parsed as a single token&lt;/em&gt;; that means you can’t remove an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; from a comment (which is not a token at all), or one which is merely part of a larger token like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;=&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;==&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; token is valid in 2 places: Assignments and variable (or similar) initializers.  We must construct situations where the 2 tokens surrounding the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; token remain valid in juxtaposition once it’s removed.  Since there aren’t many places allow two identifiers in sequence, this is not easy.&lt;/p&gt;

&lt;p&gt;First, there are 3 simple answers:&lt;/p&gt;

&lt;h2 id=&quot;1-the-color-color-trick&quot;&gt;1: The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Color Color&lt;/code&gt; trick&lt;/h2&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This utilizes the &lt;a href=&quot;https://github.com/ljw1004/csharpspec/blob/gh-pages/expressions.md#identical-simple-names-and-type-names&quot;&gt;Color Color case&lt;/a&gt;; if an identifier can refer to either a typename or a simple name in the same scope, it’s treated as both, and can be used as either meaning.&lt;/p&gt;

&lt;p&gt;With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; sign, this is an assignment, and both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt;s refer to the field.  Without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; sign, it’s a variable declaration; the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt; refers to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Int32&lt;/code&gt; type, and the second is a variable name (hiding the field).&lt;/p&gt;

&lt;h3 id=&quot;variant-casting&quot;&gt;Variant: Casting&lt;/h3&gt;
&lt;p&gt;Using the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt; declaration as above, you can also construct a cast:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This works because of what appears to be a bug in the compiler; the left side of an assignment expression can be wrapped in parentheses.  Without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, this becomes a cast.&lt;/p&gt;

&lt;p&gt;Since the cast alone is not valid as a statement, I need to wrap it in an assignment.  This also means the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt; variable must be of a type convertible to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt; type (since both are assigned to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; here); the previous answer also works with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string Int32&lt;/code&gt; or any other type.&lt;/p&gt;

&lt;h2 id=&quot;2-collapsing-2-tokens-to-1&quot;&gt;2: Collapsing 2 tokens to 1.&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Credit to &lt;a href=&quot;https://twitter.com/ThisIsJoshVarty/status/811646571825631232&quot;&gt;Josh Varty&lt;/a&gt; for this solution&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is a clever cheat; instead of merely removing a token, it utilizes the missing whitespace to combine the surrounding tokens into a single name.&lt;/p&gt;

&lt;h2 id=&quot;3-invocation-expression&quot;&gt;3. Invocation expression&lt;/h2&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This trick works because variables of delegate type are both callable and (unlike functions) assignable.  By wrapping the right side of the assignment in parentheses, this becomes a valid function call when the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; is removed.&lt;/p&gt;

&lt;p&gt;Making this compile is a bit tricky; the right side must be convertible to both the delegate type and its parameter.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; is the simple way to meet that requirement (and will come up again in more-complex answers).  To avoid the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;, you must declare your own delegate that accepts itself as a parameter; this is impossible to achieve with the built-in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Action&amp;lt;T&amp;gt;&lt;/code&gt; delegates (since you can’t write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Action&amp;lt;Action&amp;lt;Action&amp;lt;...&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; forever).  &lt;a href=&quot;https://tryroslyn.azurewebsites.net/#b:master/f:r/MYGwhgzhAEDC0G8BQBIAJgUxBg5mALhtAG4D2AlmtACIAU10aAlANxLQc2NuckVUBZWk0TtenKgF5otZi2hiOAXyRKgA&quot;&gt;&lt;em&gt;example&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;variant-ref-return&quot;&gt;Variant: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ref&lt;/code&gt; return&lt;/h3&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I can also do this using another new C# 7 feature, &lt;a href=&quot;https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#user-content-ref-returns-and-locals&quot;&gt;ref returns&lt;/a&gt;, which allow a parenthesized expression on the left hand side of an assignment.&lt;/p&gt;

&lt;p&gt;This answer calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M()&lt;/code&gt; whether the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; token exists or not.  With an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, it assigns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; to its return value (by reference).  Without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, it calls that return value, passing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; as a parameter.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;The remaining answers are much more complicated, relying on clever ways to combine or abuse language features new to C# 6 and 7.&lt;/p&gt;

&lt;h2 id=&quot;4-lambda-expressions-vs-expression-bodied-methods&quot;&gt;4. Lambda expressions vs. Expression-bodied methods&lt;/h2&gt;
&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This uses the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; sign to switch between two entirely different declaration syntaxes; variables and expression-bodied methods.&lt;/p&gt;

&lt;p&gt;With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, this is a class field of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Func&amp;lt;int?&amp;gt;&lt;/code&gt;, initialized to a simple lambda expression.  Removing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; changes it to a method whose &lt;em&gt;return&lt;/em&gt; type is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Func&amp;lt;int?&amp;gt;&lt;/code&gt;, with an &lt;a href=&quot;https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#expression-bodied-function-members&quot;&gt;expression body&lt;/a&gt; that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Like the previous example, I use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; to be convertible to both the delegate type itself (for the field initializer) and its return value (for the expression-bodied method); I could also have used a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delegate D D();&lt;/code&gt; and returned &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; itself.&lt;/p&gt;

&lt;h2 id=&quot;5-splitting-a-class-member-in-half&quot;&gt;5. Splitting a class member in half&lt;/h2&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here’s where the real evil comes in to play.  C# 6 introduces &lt;a href=&quot;https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#initializers-for-auto-properties&quot;&gt;auto property initializers&lt;/a&gt;.  This is the first time that the assignment operator can follow something other than a name (or array access), giving me new horizons to explore in its removal.&lt;/p&gt;

&lt;p&gt;In particular, the assignment here is optional, and, when it’s omitted, no semicolon is needed to terminate the expression.  This opens up new opportunities that aren’t possible in methods because of those pesky semicolons.  All I need to do is create an initializer expression that becomes a valid class member declaration once the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; is removed.&lt;/p&gt;

&lt;p&gt;To do this, I turn to the powers of contextual keywords.  When parsed as an expression, the right hand side of this initializer is an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; lambda expression.  Once I remove the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, it becomes an expression-bodied constructor (new to C #7) for a class named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt;.  The body after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&amp;gt;&lt;/code&gt; must be valid as both a lambda that returns the type declared for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; and as a statement (since constructors have no return value), so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&amp;gt; null&lt;/code&gt; won’t work.  The simplest expression which is valid as a statement and returns a value is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new blah()&lt;/code&gt;, so I use that.&lt;/p&gt;

&lt;h3 id=&quot;would-be-variant&quot;&gt;Would-be variant:&lt;/h3&gt;
&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In principle, I should be able to replace the expression-bodied constructor with a simple &lt;a href=&quot;https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#expression-bodies-on-property-like-function-members&quot;&gt;expression-bodied property&lt;/a&gt;, declaring a property of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt;.  Here, the body after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&amp;gt;&lt;/code&gt; is a property getter, so I can return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; like I usually do.&lt;/p&gt;

&lt;p&gt;However, due to a &lt;a href=&quot;https://github.com/dotnet/roslyn/issues/16044&quot;&gt;bug&lt;/a&gt; in the parser, expression-bodied properties cannot have a type named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt;, so this doesn’t actually compile.&lt;/p&gt;

&lt;h2 id=&quot;6-tuple-deconstruction&quot;&gt;6. Tuple Deconstruction&lt;/h2&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is similar to #3 and its variant, taking advantage of another new C# 7 feature, &lt;a href=&quot;https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#user-content-tuples&quot;&gt;tuple support&lt;/a&gt;.  Like the previous answer, this adds new possibilities to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; operator; the left-hand side can now be a parenthesized list.  Without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, the parenthesized list is not legal as-is, so we need to turn it into a parameter list for a method call.&lt;/p&gt;

&lt;p&gt;With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, this is a combination of a tuple literal and a deconstructing variable declaration.  Removing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; completely rewrites the parse tree, turning it into a chained call to a function named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; with two parameters, followed by a call to its returned delegate with two more parameters.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; must be declared separately as fields so that they can be passed as parameters without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; but be declared as variables with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also replace the tuple literal with an explicit tuple construction (&lt;a href=&quot;https://tryroslyn.azurewebsites.net/#b:master/f:r/K4Zwlgdg5gBAygTxAFwKYFsDcAoAxgGwEMQQYBhGAb2xlpgEFdkwB7CAHgBVgAHfVdpGQAaIQD4xMAG6EATgAohMQsJhKARgEoYAXkkRg+fDjpqIyGAA9VCE3SkswAExgBZeduqnTM2THnWMAjaOv7cfKgAdGSyqIRo8gCMqgBMmpp2tAC+2FlAA&quot;&gt;demo&lt;/a&gt;), but the method must still be named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt;, since deconstructing assignment is only valid with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;7-local-functions&quot;&gt;7. Local functions&lt;/h2&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is a cleverer variant of #4, using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; to turn a method declaration into an assignment.  This time, I combine expression-bodied methods with C# 7’s new &lt;a href=&quot;https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#user-content-local-functions&quot;&gt;local functions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Local functions allow me to bring the expression-bodied method trick into statement context, allowing me to turn it into a statement rather than a declaration.  This gives me more options to make it stay when adding the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This example starts as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; lambda expression assigned to a variable named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt; (which therefore must be of a delegate type that returns a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;).  Removing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; turns this into a (non-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt;) local function named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt;.  Like many of the other answers, this utilizes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Color Color&lt;/code&gt; trick to allow &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32&lt;/code&gt; to be both a return type and an assignable name.  Unlike #4, this doesn’t change the expression from a lambda to a return value, so I don’t need to return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; to be compatible with both variants.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus challenge&lt;/strong&gt;: Construct a valid C# program with 2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; tokens that remains valid when either or both of them are removed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; No compilers were (&lt;a href=&quot;https://github.com/dotnet/roslyn/issues?utf8=%E2%9C%93&amp;amp;q=16068%20OR%2016098%20OR%2016044%20author%3Aslaks&quot;&gt;permanently&lt;/a&gt;) harmed in the writing of this blog post.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-11-18-common-crypto-pitfalls.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-11-18/common-crypto-pitfalls/"/>
		<title type="text">Common Cryptographic Pitfalls</title>
		<updated>2015-11-18T00:00:00+00:00</updated>
		<published>2015-11-18T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="cryptography" term="cryptography" />
		
		
		<category scheme="https://blog.slaks.net/#" label="security" term="security" />
		
		
		<category scheme="https://blog.slaks.net/#" label="mistakes" term="mistakes" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;When writing code that deals with security or cryptography, there are a number of mistakes that many people make; some obvious and some quite subtle.  This post describes the most common mistakes I’ve seen and why they’re wrong.&lt;/p&gt;

&lt;h1 id=&quot;dont-re-invent-the-car&quot;&gt;Don’t re-invent the car&lt;/h1&gt;
&lt;p&gt;Correctly using cryptographic primitives is &lt;em&gt;hard&lt;/em&gt;.  If at all possible, you should not use raw cryptographic primitives (even well-accepted ones like AES, RSA, or SHA2) directly; instead, you should use professionally-built and reviewed protocols that use these systems, such as TLS, &lt;a href=&quot;https://nacl.cr.yp.to/&quot;&gt;NaCl&lt;/a&gt;, &lt;a href=&quot;https://github.com/google/keyczar&quot;&gt;Keyczar&lt;/a&gt;, and others.&lt;/p&gt;

&lt;p&gt;There are a variety of subtle issues that professional cryptographers know about and you don’t (such padding and timing vulnerabilities), and these higher-level wrappers address these issues for you.&lt;/p&gt;

&lt;p&gt;There have been many security issues resulting from projects that attempt to build their own protocols, and get these wrong.&lt;/p&gt;

&lt;h1 id=&quot;dont-re-invent-the-wheel&quot;&gt;Don’t re-invent the wheel&lt;/h1&gt;
&lt;p&gt;Even if you do need to use primitives directly, stick to known, well-researched primitives, and use the &lt;a href=&quot;https://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html&quot;&gt;best ones available&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In particular, &lt;em&gt;please&lt;/em&gt; don’t try to build your own encryption or hash algorithms.  Cryptography is exceptionally complicated; unless you actually have substantial experience in the mathematical side of cryptanalysis, you are very unlikely to create a secure algorithm.  Standard algorithms like AES have had years of research, and yet still do not have known vulnerabilities (when used correctly).&lt;/p&gt;

&lt;p&gt;On a similar note, try not to write your own implementations of standard algorithms; instead, stick to existing, hardened, well-tested codebases.  These implementations have been hardened to avoid leaking data through timing issues with early termination or CPU caching (eg, not using secret information in CPU branching or loop boundaries).&lt;/p&gt;

&lt;h1 id=&quot;learn-as-much-as-possible&quot;&gt;Learn as much as possible&lt;/h1&gt;
&lt;p&gt;Especially if you do use primitives directly, learn about things like padding attacks, replay vulnerabilities, and the like.  If you have time, take a course about cryptography (eg, &lt;a href=&quot;https://online.stanford.edu/course/cryptography&quot;&gt;Stanford’s online course&lt;/a&gt;).  If you don’t have time for a full course, at least read the Wikipedia articles about algorithms you use.&lt;/p&gt;

&lt;p&gt;Every bit of cryptographic lore you learn may help you catch a mistake in your design or implementation and stop a real-world attacker.&lt;/p&gt;

&lt;p&gt;If you use cryptography in production, you should keep up-to-date with the latest developments; read cryptographic blogs or follow cryptographers on Twitter.  Some good people to follow include &lt;a href=&quot;https://www.schneier.com/&quot;&gt;Bruce Schneier&lt;/a&gt; and &lt;a href=&quot;https://blog.cryptographyengineering.com/&quot;&gt;Matthew Green&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;dont-reuse-keys&quot;&gt;Don’t reuse keys&lt;/h1&gt;
&lt;p&gt;Don’t use the same key for two different purposes.  In worst-case scenarios, you can accidentally serve an endpoint that decrypts input with the same key used elsewhere to encrypt things (yes, this &lt;a href=&quot;https://blogs.msdn.com/b/webdev/archive/2012/10/22/cryptographic-improvements-in-asp-net-4-5-pt-1.aspx&quot;&gt;has happened&lt;/a&gt;).  Even if you use keys only with unrelated cryptosystems, you still risk leaking information that a cryptanalyst can use to derive information about the key.&lt;/p&gt;

&lt;p&gt;Ideally, each cryptosystem you use, and each scenario you use it in, should have its own unique key, each generated by a secure random number generator.  If, for whatever reason, you can’t afford that much randomness, you can accept a single random blob (at least as large as the largest key you need), and use HMAC hashes with different (hard-coded) HMAC keys to derive a separate key from that blob for each use.&lt;/p&gt;

&lt;h1 id=&quot;create-a-threat-model&quot;&gt;Create a threat model&lt;/h1&gt;
&lt;p&gt;It is important to understand what exactly you’re trying to prevent, and who you’re trying to block.  Are you just trying to prevent attackers from impersonating users (eg, authentication for a public forum)?  Are you trying to prevent attackers from reading messages at all (eg, personal chat)?  Are you trying to prevent attackers from knowing who your users are and who they’re communicating with (eg, off-the-record chat)?  Depending on what you’re trying to do, you will need different kinds of cryptosystems, and you will probably impose different requirements on what your product can actually do.&lt;/p&gt;

&lt;p&gt;You should document which bits of data must be kept secret from, or authenticated to, which other parties, and how you intend to do that.  You can then make sure that other features do not accidentally leak data that should be secret (eg, suggesting contacts to new users based on social networks of existing users may leak who people tend to contact).  This also gives you a central place to check that ACLs are properly enforced; that any endpoint that serves data will properly check that the user is allowed to access every part of that data.&lt;/p&gt;

&lt;h1 id=&quot;use-ivs&quot;&gt;Use IVs&lt;/h1&gt;
&lt;p&gt;If you need to use block ciphers (such as AES) directly, never use &lt;a href=&quot;https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation&quot;&gt;ECB&lt;/a&gt; (Electronic Code Book) mode; it encrypts identical plaintext blocks to identical ciphertext and can leak information about common plaintext.&lt;/p&gt;

&lt;p&gt;Instead, use CBC or CTR mode, which let you provide an additional input that will differentiate identical blocks.&lt;/p&gt;

&lt;h1 id=&quot;dont-reuse-ivs&quot;&gt;Don’t reuse IVs&lt;/h1&gt;
&lt;p&gt;Following up from the previous point, in order to properly benefit from this protection, your IVs / nonces must be unique every time you encrypt.&lt;/p&gt;

&lt;p&gt;A common beginner’s mistake is to either use a fixed IV, or generate an IV by hashing the key or the plaintext (or some other fixed value).   This defeats (most of) the purpose of CBC; since two identical plaintexts use the same IV, they will encrypt to identical ciphertexts, leaking information again (using a fixed IV will scramble identical blocks in different plaintexts, but that alone is not enough).&lt;/p&gt;

&lt;p&gt;Instead, you should generate a (from a cryptographically-secure RNG) a random IV for each message, and transmit it alongside the ciphertext.&lt;/p&gt;

&lt;h1 id=&quot;dont-expose-error-messages-or-other-internal-information&quot;&gt;Don’t expose error messages or other internal information&lt;/h1&gt;
&lt;p&gt;When dealing with crypto code, never show raw error messages to the user.  Detailed error messages from crypto libraries should go in (access-controlled) logs.  Your user-facing error messages should just say things like “Invalid input” and not give any details about why.  If you expose actual error messages like “Invalid block size” or “Bad padding at byte &lt;em&gt;X&lt;/em&gt;”, cryptanalysts may be able to slowly deduce bits of plaintexts or even keys, using techniques like &lt;a href=&quot;https://en.wikipedia.org/wiki/Padding_oracle_attack&quot;&gt;padding oracle attacks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This also applies to error messages resulting from decrypted data.  If you parse a decrypted plaintext as XML, showing a detailed XML parsing error can help the attacker see exactly how many bytes he got right, and in some cases can even leak actual plaintext (from more-useful error messages that include the invalid line).  If you expose these raw error messages, attackers may even be able to fully decrypt arbitrary non-XML ciphertexts by reading the XML parse errors.&lt;/p&gt;

&lt;p&gt;Similarly, if you decrypt a ciphertext and validate the data using some business rules, detailed error messages from those rules can also leak information.  Even if you only show these errors to authenticated, trusted users, you may have subtler risks, such as allowing users to decrypt parts of other users’ data, or attackers who manage to exfiltrate your error messages from those trusted users (eg, by posing as tech support).&lt;/p&gt;

&lt;h1 id=&quot;validate-everything&quot;&gt;Validate everything&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Don’t&lt;/strong&gt; be liberal with what you accept.  When creating public APIs, it can be helpful to accept all requests, even if they don’t exactly match what you’re expecting.  However, when dealing with cryptography or with sensitive information, this is exactly the attitude you don’t want.&lt;/p&gt;

&lt;p&gt;Security-critical code should validate all attacker-controllable inputs as strictly as possible. Any invalid or unexpected data should be rejected immediately with no further processing, and with completely generic error messages.  When possible, you should sign (&lt;a href=&quot;https://www.daemonology.net/blog/2009-06-24-encrypt-then-mac.html&quot;&gt;with a MAC&lt;/a&gt;) all ciphertexts, so that you can reject any data that an attacker has modified before it has a chance to interact with anything else (and potentially expose weaknesses or side-channels in your decryption code).&lt;/p&gt;

&lt;p&gt;Like detailed error messages, accepting invalid data can allow attackers to slowly learn parts of your encryption keys, using padding oracle attacks or timing vulnerabilities to see minute differences in how your system handles different inputs.  Rejecting modified inputs immediately prevents these attacks.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;Note: Although I have some understanding of the use of cryptographic primtiives, I am not a professional cryptographer.&lt;/em&gt;&lt;br /&gt;
Please take any cryptographic advice with a grain of salt (pun intended).  If you are building highly sensitive systems, you should verify with your company’s cryptographer (you &lt;em&gt;do&lt;/em&gt; have one, right?)&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-10-31-chains-of-trust-the-root-of-all-security.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-10-31/chains-of-trust-the-root-of-all-security/"/>
		<title type="text">Chains of Trust – The Root of all Security</title>
		<updated>2015-10-31T00:00:00+00:00</updated>
		<published>2015-10-31T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="security" term="security" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;The entire field of computer security is dedicated to verifying the source, confidentiality, or integrity of information or communication.  This includes guaranteeing that the web page or other resource you’re seeing actually came from the entity you’re trying to reach (and has not been modified); guaranteeing that information you transmit will only be readable by that entity, or that the user connecting to a server is actually in possession of a token.&lt;/p&gt;

&lt;p&gt;This raises a problem.  Security may be all about trust and authentication, but how can you establish that trust in the first place?  Without some existing indicator of identity, all the cryptography in the world cannot tell whether the data you’re receiving came from Bob, or from Mallory pretending to be Bob.  If you start from a blank slate, any kind of proof you could ask Bob for can easily be duplicated by Mallory, leaving you none the wiser.&lt;/p&gt;

&lt;p&gt;To solve this chicken-and-egg problem,&lt;/p&gt;

&lt;p&gt;Bootstrapping - voice authentiation for OTR chat&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-10-13-web-authentication-arms-race-a-tale-of-two-security-experts.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-10-13/web-authentication-arms-race-a-tale-of-two-security-experts/"/>
		<title type="text">The Web Authentication Arms Race – A Tale of Two Security Experts</title>
		<updated>2015-10-13T00:00:00+00:00</updated>
		<published>2015-10-13T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="security" term="security" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Web authentication systems have evolved over the past ten years to counter a growing variety of threats.  This post will present a fictional arms race between a web application developer and an attacker, showing how different threats can be countered with the latest security technologies.&lt;/p&gt;

&lt;p&gt;This entire conversation assumes that the user has already legitimately established some form of trust anchor (eg, a password or hardware token) with the defender before the attacker came onto the scene.  Cryptography &lt;a href=&quot;https://blogs.msdn.com/b/ericlippert/archive/2011/09/27/keep-it-secret-keep-it-safe.aspx&quot;&gt;can only be used&lt;/a&gt; to transfer existing trust or secrecy across time or space; if the attacker impersonates the defender before the user establishes anything, it becomes impossible for the user to tell which party is legitimate.  This also assumes that the site itself has no vulnerabilities (such as XSS or CSRF) that would allow attackers to run code or read data, or read certificates from the server.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: Users will enter a username &amp;amp; password, and I will give them an authentication cookie for me to trust in the future.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will watch your network traffic and steal the passwords as they come down the wire.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will change the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;form&amp;gt;&lt;/code&gt; to submit over HTTPS, so you won’t see any readable passwords.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will run an active &lt;a href=&quot;https://en.wikipedia.org/wiki/Man-in-the-middle_attack&quot;&gt;MITM attack&lt;/a&gt; as the user loads the login page, and insert Javascript that sends the password to my server in the background.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will serve the login page itself over HTTPS too, so you won’t be able to read or change it.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will watch your network traffic and steal the resulting authentication cookies, so I can still impersonate users even without knowing the password.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will serve the entire site over HTTPS (and mark the cookie as Secure), so you won’t be able to see any cookies.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will run an active MITM attack against your entire site and serve it over HTTP, letting me see all of your traffic (including passwords and cookies) again.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will serve a &lt;a href=&quot;https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Strict-Transport-Security&lt;/code&gt; header&lt;/a&gt;, telling the browser to always refuse to load my site over HTTP (assuming the user has already visited the site over a trusted connection to establish a trust anchor).&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will find or compromise a shady certificate authority and get my own certificate for your domain name, letting me run my MITM attack and still serve HTTPS.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will serve a &lt;a href=&quot;https://en.wikipedia.org/wiki/HTTP_Public_Key_Pinning&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Public-Key-Pins&lt;/code&gt; header&lt;/a&gt;, telling the browser to refuse to load my site with any certificate other than the one I specify.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;At this point, there is no reasonable way for the attacker to run an MITM attack without first compromising the browser.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will make a fake login page and phish users for passwords.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will add two-factor authentication, making your stolen passwords useless without the non-reusable second factor.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will change my phishing page to request a second factor as well, then immediately use it to log in once.  (this will give the attacker a single login session with no way of logging in again, but that is often enough to cause harm)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will replace my SMS or &lt;a href=&quot;https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm&quot;&gt;TOTP&lt;/a&gt; second factor with a private key on a &lt;a href=&quot;https://www.yubico.com/products/yubikey-hardware/&quot;&gt;tamper-resistant hardware device&lt;/a&gt;, rendering an MITM attack completely unable to use the stolen credential (the private key is used to sign a challenge from the server, and never leaves the device).  This also prevents phishing attacks, since the browser will incorporate the site origin into the challenge signed by the private key, and will refuse to send a challenge signed for the defender’s server to any other origin.  This is only possible because the browser actively cooperates, unlike purely web-based solutions like SQRL.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Private keys, such as U2F devices, are unphishable credentials; it is now completely impossible for anyone who does not have physical posession of the private key to authenticate.  Note that this assumes that the hardware device is trusted; if the attacker can swap the device for a device with a known private key, all bets are off.  Also note that you should still use a password in conjuction with the hardware device, to prevent an attacker from simply stealing the device (if the device itself requires a password to operate, that’s also fine).&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will trick the user into installing a malicious browser extension or desktop application, then use it to read the authentication cookie from the browser’s cookie jar.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will use &lt;a href=&quot;https://www.browserauth.net/channel-bound-cookies&quot;&gt;channel-bound cookies&lt;/a&gt;, linking my authentication cookie to the private key used to generate the SSL connection.  This way, the authentication cookie will only work in an HTTPS session backed by the same private key, preventing the attacker from using it on his computer.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will change my malicious code to exfiltrate the private key as well as the authentication cookie, allowing me to completely clone the SSL connection on my machine, and still use the cookie.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will hope that the user’s browser signs its HTTPS connections with a hardware-based private key (hardware-backed token binding), preventing the attacker from cloning the SSL session without access to that private key (which never leaves the hardware device).&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Attacker&lt;/strong&gt;: I will change my malicious code to run a reverse proxy through the user’s browser, sending my arbitrary requests through the same token-bound SSL session as the user’s actual requests.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Defender&lt;/strong&gt;: I will encourage users to use a platform &amp;amp; browser that does not allow processes or extensions to interact with security contexts for other origins.  This way, the attacker’s malicious code will not be able to read my cookies or send requests to my site.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Assuming no application-level vulnerabilities (such as XSS or CSRF), and no vulnerabilities in the platform itself, such a platform would be completely secure against any kind of attack.  Unfortunately, I am not aware of any such platform that also supports unphishable credentials.  Chrome OS supports unphishable credentials, but offers no way to prevent extensions from sending HTTP requests to your origin.  Most mobile browsers (on non-rooted devices) do not support extensions at all, but do not currently support unphishable credentials.&lt;/em&gt;&lt;/p&gt;

&lt;h1 id=&quot;theory-vs-practice&quot;&gt;Theory vs. Practice&lt;/h1&gt;
&lt;p&gt;In theory, given these assumptions, this design is completely secure.  In practice, however, the situation is rarely so simple.  There are a number of issues which make these assumptions difficult to meet, or negate security in other ways.&lt;/p&gt;

&lt;h2 id=&quot;authenticating-the-user&quot;&gt;Authenticating the user&lt;/h2&gt;
&lt;p&gt;This technique serves to completely prove that the user is in physical possession of a hardware token.  However, it does not help assure you that the hardware token actually belongs to the user you think it does.  If an attacker can convince you that &lt;em&gt;his&lt;/em&gt; hardware token belongs to the user, he can still perform MITM attacks, since your server has no way of knowing that he isn’t actually the user.&lt;/p&gt;

&lt;p&gt;By definition, you cannot use a hardware token to authenticate the user who is setting up a hardware token, so you’re back to relying on classical HTTPS and certificate pinning, leaving you vulnerable to malicious code running on the client.  If you’re associating an online account with some existing entity, such as a bank account (as opposed to creating an entirely new account, such as an email account), you’ll need some way of proving that the user creating the account is actually associated with the existing entity, and not an attacker impersonating or MITMing the user.  The best way to do this is for the user to verify themselves in person and associate the token using trusted, defender-controlled hardware.&lt;/p&gt;

&lt;h2 id=&quot;trusting-hardware&quot;&gt;Trusting hardware&lt;/h2&gt;
&lt;p&gt;It is impossible to defend against an attacker who can run arbitrary code on the user’s machine (since there is no way for the defender’s server to distinguish between requests sent from legitimate code on the user’s behalf, vs. hostile code from the attacker).  The completely-secure scenario above postulated that the user’s platform does not allow arbitrary code in the first place, neatly side-stepping this problem.&lt;/p&gt;

&lt;p&gt;Mobile platforms attempt to make this guarantee, preventing other applications from reading the browser’s cookie jar or private keys.  However, completely enforcing this is harder; any security vulnerability (or actual nefarious code) in the browser, OS, or even platform hardware may negate this guarantee.  For example, the ability to root or jailbreak an Android or iOS device (and thus bypass the protections around the browser) mean that they do not actually provide this guarantee.&lt;/p&gt;

&lt;p&gt;An important step forward in this regard is trusted boot, as used by Windows and Chrome OS, in which the entire chain of execution from the BIOS to the boot loader to the OS to the hardware drivers is signed and verified on each boot.  However, security vulnerabilities in the signed code can still lead the arbitrary code execution and compromise the machine.&lt;/p&gt;

&lt;p&gt;More troublingly (assuming nation-state level attackers), even if the user does have completely-secure hardware, the attacker can replace it with attacker-controlled hardware that contains a reverse proxy and bypasses all of these protections.  There is no simple way for the user to authenticate their own hardware device (other than keeping it in sight at &lt;em&gt;all&lt;/em&gt; times); the attacker can open up the existing device and copy all data and private keys to the backdoored device to ensure that it looks and behaves identically.&lt;/p&gt;

&lt;p&gt;(You could build a tamper-proof hardware-backed private key that will self-destruct if removed from the PCB, but you’ll need to hope that your tamper-proofing is better than the attacker’s tampering.  And that the attacker can’t modify other parts of the hardware to insert a backdoor without removing anything.)&lt;/p&gt;

&lt;h2 id=&quot;forgot-password&quot;&gt;Forgot password&lt;/h2&gt;
&lt;p&gt;Finally, the Forgot Password feature is the complete antithesis of these security guarantees.  The entire point of Forgot Password is to authenticate the user &lt;em&gt;without&lt;/em&gt; any of these trusted credentials.  Thus, this reintroduces the problem of initially associating the user, as mentioned in the first issue.  In this situation, unlike account creation, you always need to prove who the user is, since you’re associating the user with an existing account, rather than simply creating a new account from scratch.&lt;/p&gt;

&lt;p&gt;Forgot Password features are typically implementing by delegating authentication to some other (implicitly-trusted) service; usually email.  When first creating the account, the user specifies an email address, and the user can then authenticate by proving that he can read an email sent to that address (usually alongside a low-grade second factor such as a security question).  However, this opens up additional risks; any attacker that can compromise the user’s email account (and the second factor, which is usually easy) can compromise the account at any time.  In addition, if the user legitimately uses the Forgot Password, there is no way to prevent malicious code already running on the client from MITMing that connection and compromising the account (eg, by adding the attacker’s private key alongside the user’s).&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-08-20-handling-css-margins.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-08-20/handling-css-margins/"/>
		<title type="text">Handling margins in CSS</title>
		<updated>2015-08-20T00:00:00+00:00</updated>
		<published>2015-08-20T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="css" term="css" />
		
		
		<category scheme="https://blog.slaks.net/#" label="code-quality" term="code-quality" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Writing accurate, maintainable CSS for large websites requires careful planning and meticulous attention to detail.  One detail that can often slip through the gaps is proper spacing between elements.  If a view has a number of independent, optional components laid out in a vertical (or horizontal) stack, it can be tricky to ensure that each piece has correct spacing around it in all configurations.&lt;/p&gt;

&lt;p&gt;I’ve found that&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-06-30-code-snippets-impossible-code.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-06-30/code-snippets-impossible-code/"/>
		<title type="text">Code Snippets: Impossible Code</title>
		<updated>2015-06-30T00:00:00+00:00</updated>
		<published>2015-06-30T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Java" term="java" />
		
		
		<category scheme="https://blog.slaks.net/#" label="code-snippets" term="code-snippets" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;em&gt;This post is part of a &lt;a href=&quot;/#code-snippets&quot;&gt;series&lt;/a&gt; of blog posts called code snippets.  These blog posts will explore successively more interesting ways to do simple tasks or abuse language features.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I recently set out to create snippets of code that have nothing inherently wrong with them, but can never appear in a valid program.&lt;/p&gt;

&lt;h1 id=&quot;impossible-accessibility&quot;&gt;Impossible accessibility&lt;/h1&gt;
&lt;p&gt;The simplest example is a statement that uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;internal&lt;/code&gt; types from two different assemblies, so that there is no project that it could legally appear in:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// In A.dll:&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;internal&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// In B.dll:&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that the method itself is not impossible to call;  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Base.Method()&lt;/code&gt; is perfectly legal anywhere in the first assembly.  However, the specific expression &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container.Derived.Method()&lt;/code&gt; is impossible, since it uses types from both assemblies.&lt;/p&gt;

&lt;p&gt;Adding an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[InternalsVisibleTo]&lt;/code&gt; attribute to the first assembly, or compiling both assemblies with circular references (this is possible, if you try very hard), would make this statement legal.  To prevent that, you can simply make both nested members &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;protected&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;void-expressions&quot;&gt;void expressions&lt;/h1&gt;
&lt;p&gt;A more interesting approach is to create an expression of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;.  This expression is completely fine, except that there is no way to &lt;em&gt;use&lt;/em&gt; an expression of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt; anywhere.&lt;/p&gt;

&lt;p&gt;The only valid place that the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt; can appear in C# is as a method return type; thus, the only way to make an expression of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt; is a method call.  Method calls are legal as expression statements, so that is perfectly fine.&lt;/p&gt;

&lt;p&gt;However, there is one feature in the language that lets you call an expression with an arbitrary return type, in a syntax which is &lt;em&gt;not&lt;/em&gt; legal as an expression statement: LINQ queries.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Impossible&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Impossible&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This would compile to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Impossible.Select(x =&amp;gt; 1)&lt;/code&gt;, which is a perfectly fine expression statement, returning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;.  However, query expressions, unlike method call expressions, are not valid as expression statements, so this call is illegal.&lt;/p&gt;

&lt;p&gt;Any attempt to use this query will result in either “CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement”, “CS0127 Since ‘Method()’ returns void, a return keyword must not be followed by an object expression”, or “CS0815: Cannot assign void to an implicitly-typed variable”.  (except for &lt;a href=&quot;https://github.com/dotnet/roslyn/issues/1830&quot;&gt;a bug&lt;/a&gt; in the RC release of the Roslyn compiler)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side note&lt;/em&gt;: I need to take the unusual approach of making a LINQ query against a static method on a type to prevent people from making it compile by adding a matching extension method that does not return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;impossible-overload-resolution&quot;&gt;Impossible overload resolution&lt;/h1&gt;
&lt;p&gt;A subtler approach is to make two overloads of a generic method such that it is impossible to distinguish between them:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There is no way to call either of these methods with the same type for both generic type parameters (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Method&amp;lt;int, int&amp;gt;&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Ordinarily, C# has a number of ways to help disambiguate between generic overloads.  You can cast ambiguous parameters to the exact argument type of the desired overload.  You can explicitly specify type parameters.  You can call the method through a specific qualifier (to disambiguate between static and instance methods, or between methods inherited from a base class).  You can even pass named parameters if the two overloads have different parameters names.&lt;/p&gt;

&lt;p&gt;However, these methods are identical in all of these regards, leaving no way to tell the compiler which one you want to call.  (note that it is possible to call them at runtime using Reflection)&lt;/p&gt;

&lt;h1 id=&quot;generic-constraint-conflict&quot;&gt;Generic constraint conflict&lt;/h1&gt;

&lt;p&gt;Another option is to create a method with conflicting generic type constraints, such that there is no possible type that will satisfy the constraints.  Ordinarily, the compiler will not let you do that; a type parameter cannot be constrained to two different classes.  However, you can bypass this restriction using more generics:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Z&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is impossible to call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Method&lt;/code&gt; on any instance parameterized with classes that do not inherit one another (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&amp;lt;Exception, Type&amp;gt;&lt;/code&gt;).  Unlike the previous example, it is &lt;em&gt;completely&lt;/em&gt; impossible to call this method on such an instantiation, even with reflection – there is no type that is valid value for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Z&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;impossible-inheritance&quot;&gt;Impossible inheritance&lt;/h1&gt;
&lt;p&gt;Any of these impossible methods can also be used to make a class that can never have any instances, by making an abstract method that cannot be implemented:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Impossible&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Any attempt to create a concrete non-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;abstract&lt;/code&gt; subclass of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Impossible&lt;/code&gt; will result in “CS0462 The inherited members &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Base&amp;lt;T, U&amp;gt;.Method(T)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Base&amp;lt;T, U&amp;gt;.Method(U)&lt;/code&gt; have the same signature in type ‘Impossible’, so they cannot be overridden”.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Z&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Impossible&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, implementing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Impossible&lt;/code&gt; will give “CS0455 Type parameter ‘Z’ inherits conflicting constraints ‘Exception’ and ‘Type’”.&lt;/p&gt;

&lt;h1 id=&quot;interface-method-conflict&quot;&gt;Interface method conflict&lt;/h1&gt;
&lt;p&gt;Since Java is less feature-rich than C#, there are far fewer examples of impossible code in Java.  However, this feature sparsity can be used to create code which is only impossible in Java – inheriting conflicting interface methods:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Java, it is impossible to implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt;.  In C#, this would be possible with explicit interface implementation, which essentially implements each method with a private (and therefore unique) compiler-generated name, avoiding problems of identical overloads.  However, since Java does not support this feature, you’d need to make two methods with the same signature but different return types, which is impossible.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side note:&lt;/em&gt; I need to add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;final&lt;/code&gt; because otherwise, making &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; abstract would let it compile without implementing the method.&lt;/p&gt;

&lt;p&gt;If you can think of other examples of impossible code, please comment!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-06-26-code-snippets-fast-property-access-reflection.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-06-26/code-snippets-fast-property-access-reflection/"/>
		<title type="text">Code Snippets: Fast Runtime Property Access with Reflection</title>
		<updated>2015-06-26T00:00:00+00:00</updated>
		<published>2015-06-26T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="reflection" term="reflection" />
		
		
		<category scheme="https://blog.slaks.net/#" label="code-snippets" term="code-snippets" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;em&gt;This post is part of a &lt;a href=&quot;/#code-snippets&quot;&gt;series&lt;/a&gt; of blog posts called code snippets.  These blog posts will explore successively more interesting ways to do simple tasks or abuse language features.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Reflection is great for accessing all properties (or an arbitrary property named at runtime) of an arbitrary type.  However, Reflection has performance costs which can be unacceptable in critical codepaths.  Instead, you can add an upfront cost and create generic delegates to read or write such properties without any overhead at all (depending on what you’re doing with the values, you can even avoid boxing).&lt;/p&gt;

&lt;p&gt;The typical goal for this kind of code is to get the value of a property with the specified name.&lt;/p&gt;

&lt;p&gt;The naive (and slowest) way to do this is straight-up reflection:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This does all of the work of locating the property and dynamically invoking its getter on every call.  You can improve this by caching each &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PropertyInfo&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PropertyInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;PropertyInfo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;property&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;TryGetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;property&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;property&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;property&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(if multiple threads are involved, you’ll need either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[ThreadStatic]&lt;/code&gt; or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ConcurrentDictionary&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;However, this still repeats the work of dynamically invoking the getter method.  You can eliminate this by creating a delegate that points to the get method.  Creating the delegate still involves some costly reflection, but once it’s created, calling it will be as fast as calling any other delegate.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getters&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;TryGetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;getter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Delegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CreateDelegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;),&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetMethod&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;getters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This uses &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms173174&quot;&gt;delegate return type covariance&lt;/a&gt; to create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Func&amp;lt;..., object&amp;gt;&lt;/code&gt; from a getter method that returns any more-derived type.  This feature actually predates generic covariance; it will work even on .Net 2.0.&lt;/p&gt;

&lt;p&gt;This approach has some caveats.  This can only work if you know the type of the objects you’re operating on at compile-time (eg, in a generic method to bind or serialize parameters).  If you’re operating on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt;s at compile time, this cannot work, since such a delegate wouldn’t be type-safe.&lt;/p&gt;

&lt;p&gt;This also won’t work for properties that return value types.  Variance &lt;a href=&quot;https://stackoverflow.com/a/12454932/34397&quot;&gt;works because&lt;/a&gt; the runtime representation of the types are completely identical, so that the JITted code doesn’t know or care that the actual type parameter is different.  Value types require different codegen than reference types, so this cannot begin to work.&lt;/p&gt;

&lt;p&gt;To solve these problems, you can do actual runtime code generation, using the ever-handy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression&amp;lt;T&amp;gt;&lt;/code&gt;.  Specifically, you need to create an expression tree that casts an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt; parameter to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; (if you don’t know the actual type at compile time), then boxes the result of the property into an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt; (if it’s a value type).&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;getter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;(&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;param&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression.Convert()&lt;/code&gt; node will automatically generate a boxing conversion if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; is a value type, making this work.  This whole block is only necessary if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; is a value type; if it’s a reference type, you can skip the entire runtime codegen phase with the previous example.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-06-18-code-snippets-and-in-the-darkness-bind-them.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-06-18/code-snippets-and-in-the-darkness-bind-them/"/>
		<title type="text">Code Snippets: And in the darkness bind() them</title>
		<updated>2015-06-18T00:00:00+00:00</updated>
		<published>2015-06-18T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="code-snippets" term="code-snippets" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;em&gt;This post is part of a &lt;a href=&quot;/#code-snippets&quot;&gt;series&lt;/a&gt; of blog posts called code snippets.  These blog posts will explore successively more interesting ways to do simple tasks or abuse language features.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Javascript’s &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind()&lt;/code&gt; method&lt;/a&gt; (new to ES5) is very useful for passing callbacks or event handlers that use an existing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It also has some more confusing uses, especially when combined with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you have an array of functions, and you want to execute every function in the array, you can write&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, all you’re really doing is calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.call()&lt;/code&gt; on every function in the array.
Therefore, you would instead want to write&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, this won’t work, because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call()&lt;/code&gt; calls the function in its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; parameter (ie, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myFunc.call()&lt;/code&gt;), not its first argument.  This code would end up running&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will throw a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TypeError&lt;/code&gt;, since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call()&lt;/code&gt; can only be called on a function.&lt;/p&gt;

&lt;p&gt;What you actually want to run is&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In other words, you want to call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call()&lt;/code&gt; and pass your function as its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; (before any other parameters).  To make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach()&lt;/code&gt; do that, you need to bind &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call()&lt;/code&gt; to itself:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To make this code shorter (and even more confusing), note that since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function&lt;/code&gt; is itself an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function&lt;/code&gt;, you can skip the prototype and reference &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; directly:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can make it even shorter by referencing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; from a shorter function:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;myFuncs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(other short function names, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Date&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isNaN&lt;/code&gt;, would also work)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side note:&lt;/em&gt; Please, don’t do that.&lt;/p&gt;

&lt;p&gt;This technique effectively “uncurries” the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; parameter from a function.  It takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call()&lt;/code&gt;, which accepts the function to call as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;, and produces a function that accepts the function to call as its first normal parameter (and ignores &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;).  You can use the same technique for other prototype functions.  For example, you can turn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.push&lt;/code&gt; into a standalone function that mutates its first parameter:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pushInto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myArray&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;pushInto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 1,2,3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or you can take an array of strings and turn them to uppercase:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also do even more confusing things by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind()&lt;/code&gt;ing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; itself.   For example, you can simplify the above examples by wrapping the bound &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; in its own function:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uncurryThis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;copyArray&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uncurryThis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;copy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;copyArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This works by binding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind()&lt;/code&gt; to have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; as its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; parameter, so that the resulting function becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call.bind()&lt;/code&gt;.  I’ve seen this trick in &lt;a href=&quot;https://github.com/kriskowal/q/blob/78131d76e9fdeaee11094f5f4afa9182d801348e/q.js#L123-L124&quot;&gt;actual production code&lt;/a&gt;, in the most popular Javascript promise library (it was eventually replaced with an explicit version &lt;a href=&quot;https://github.com/kriskowal/q/blob/75058c0d70d36dd7e814c13493866043df39c858/q.js#L266-L268&quot;&gt;for performance reasons&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;You can take an array of objects, and turn it into an array of functions bound to those objects:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;elements&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cloners&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cloneNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;clone0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cloners&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This takes an array of elements and creates an array of functions that will clone those elements on each call.  It works by binding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind()&lt;/code&gt; to bind &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cloneNode()&lt;/code&gt;; it ends up calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cloneNode.bind(array[x])&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side note:&lt;/em&gt; With great power comes great responsibility.  Just because you can write code like this doesn’t mean you should.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-06-16-code-snippets-variadic-generics-csharp.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-06-16/code-snippets-variadic-generics-csharp/"/>
		<title type="text">Code Snippets: Variadic Generics in C#</title>
		<updated>2015-06-16T00:00:00+00:00</updated>
		<published>2015-06-16T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="generics" term="generics" />
		
		
		<category scheme="https://blog.slaks.net/#" label="code-snippets" term="code-snippets" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;em&gt;This post is part of a &lt;a href=&quot;/#code-snippets&quot;&gt;series&lt;/a&gt; of blog posts called code snippets.  These blog posts will explore successively more interesting ways to do simple tasks or abuse language features.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;C++ introduced &lt;a href=&quot;https://en.wikipedia.org/wiki/Variadic_template&quot;&gt;Variadic Templates&lt;/a&gt; – template classes or functions that can take an arbitrary number of template parameters (like varargs/paramarray function parameters).  This feature has a number of uses.  It’s the simplest way arbitrary tuple or function types.  It’s also useful when making a function that can take an arbitrary number of objects or delegates.&lt;/p&gt;

&lt;p&gt;C# does not support this feature.  For variadic types, there is no direct workaround; this is why the BCL has 16 overloads of the generic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Func&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Action&lt;/code&gt; delegates, and 8 overloads of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tuple&lt;/code&gt;, to cover all common uses.&lt;/p&gt;

&lt;p&gt;However, when designing something that needs to take an arbitrary number of objects of different generic parameters, there are workarounds.&lt;/p&gt;

&lt;p&gt;This simplest workaround is to use repeated method calls.  You can make a generic function that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;, and call it separately for each parameter with inferred type parameters.  For example:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Container1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Container1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WithStuff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Do interesting things...&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Container1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithStuff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;())&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithStuff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MemoryStream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithStuff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CredentialCache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The disadvantage of this approach is that the repeated method calls don’t look nice, especially if you’re just trying to accept a number of parameters for a single thing.&lt;/p&gt;

&lt;p&gt;A more interesting approach is to abuse &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb384062&quot;&gt;collection initializers&lt;/a&gt;.  Collection initializers let you write a list of expressions (or argument lists) in braces (just like an array initializer), and compile them into a series of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add()&lt;/code&gt; calls.  The nice part of this feature is that each item compiles to its own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add()&lt;/code&gt; call (with its own generic parameters and type inference), giving you a way to hide the repeated calls from the syntax.&lt;/p&gt;

&lt;p&gt;The previous example can thus be adapted to look like this:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Container2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Do interesting things...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NotSupportedException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Container2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MemoryStream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CredentialCache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The class must implement the non-generic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEnumerable&lt;/code&gt; interface in order to allow collection initializers (since the feature is meant, as its name implies, to work with collections).&lt;/p&gt;

&lt;p&gt;Unlike normal method calls, collection initializers don’t expose any way to explicitly specify generic type parameters; they can only be called using type inference.  For methods that accept instances of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; (or lambdas that return such instances), this is not a problem; the compiler can infer the type parameter from the compile-time type of the expression used.  If your method only uses the generic type parameter as the parameter type of a lambda, you must instead explicitly specify the parameter type in the lambda (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(string x) =&amp;gt; blah&lt;/code&gt;), telling the compiler what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;Note that this only works at construction time (syntactically, this is an optional part of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword).  If you’re trying to make a variadic method on an existing instance (or a static method), you can make a new class to serve as the method’s parameter, and put the collection initializer in that class.  For example:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValidationManager&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ValidateItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ItemValidators&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ItemValidators&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IReadOnlyCollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Errors&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AsReadOnly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NotSupportedException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;validation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ValidationManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;validation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ValidateItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ItemValidators&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Stream cannot be empty&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MemoryStream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Length&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Email address must have a display name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MailAddress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsNullOrWhiteSpace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DisplayName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you may have noticed from this example, these workarounds don’t help you &lt;em&gt;store&lt;/em&gt; variadically-typed items.  What you do with the parameters you receive depends on what you’re ultimately trying to accomplish.  If you’re dealing with reflection (but want strongly typed lambdas), you can simply store &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typeof(T)&lt;/code&gt;.  If you just need instances (like the above example), you can cast the items to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt;.  However, the most powerful way to deal with this is to collect non-generic delegates that close over the generic parameter (creating a generic closure class).  As long as you’re able to do useful things from a non-generic entry-point (eg, cast an object to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;), you can retain the generic parameter from within the delegate.  For example:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ItemReporter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reporters&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;();&lt;/span&gt;


    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reporter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;reporters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;\t&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reporter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Report&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;params&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NewLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reporters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


    &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NotSupportedException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reporter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ItemReporter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;; &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Length&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; bytes&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;U&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DirectoryInfo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;: &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;EnumerateFiles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; files&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reporter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Report&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UtcNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DirectoryInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CurrentDirectory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As mentioned above, you must explicitly specify the lambda parameters to allow the compiler to infer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; for each generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add()&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;This demonstrates one more limitation of this workaround: It is impossible to store type information.  If you implement this class in C++, you’d be able to enforce – at compile time – that the arguments passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Report()&lt;/code&gt; match the types of the reporters passed to the constructor (since those types would be encoded in the template parameters of the instance’s compile-time type).  In C#, however, the only way to implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Report()&lt;/code&gt; is to accept an array of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt;, which obviously has no type safety.&lt;/p&gt;

&lt;p&gt;Note that this approach does not work for base classes or interfaces; if the reporter doesn’t have a delegate for an object’s exact runtime type, the dictionary lookup will fail.  It is impossible to allow that with a dictionary (since assignment compatibility is not an equivalence relation).  If you want to support that, you could either loop through every reporter until you find one with a compatible type (which would be O(n) in the number of reporters, but would be simpler code), or recursively loop through the base classes and interfaces of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;obj.GetType()&lt;/code&gt; until you find a type in the dictionary (which would be O(n) in the depth of the type hierarchy).&lt;/p&gt;

&lt;p&gt;This technique is particularly useful for visitor patterns (example &lt;a href=&quot;https://github.com/madskristensen/WebEssentials2013/blob/9316c9f373d4d764148f01e1402a2bf7a2e4ffda/EditorExtensions/CSS/Adornments/ColorTagger.cs#L53-L62&quot;&gt;usage&lt;/a&gt; and &lt;a href=&quot;https://github.com/madskristensen/WebEssentials2013/blob/9316c9f373d4d764148f01e1402a2bf7a2e4ffda/EditorExtensions/Shared/Helpers/Css/CssItemAggregator.cs&quot;&gt;implementation&lt;/a&gt;); more details coming soon in a separate blog post.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-06-12-code-snippets-conditional-casting.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-06-12/code-snippets-conditional-casting/"/>
		<title type="text">Code Snippets: Conditional Casting</title>
		<updated>2015-06-12T00:00:00+00:00</updated>
		<published>2015-06-12T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="casts" term="casts" />
		
		
		<category scheme="https://blog.slaks.net/#" label="code-snippets" term="code-snippets" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;em&gt;This post is the beginning of a new &lt;a href=&quot;/#code-snippets&quot;&gt;series&lt;/a&gt; of blog posts called code snippets.  These blog posts will explore successively more interesting ways to do simple tasks or abuse language features.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When writing C#, you will occasionally need to check whether an object is an instance of a certain type (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt;), and use part of that type if it is.&lt;/p&gt;

&lt;p&gt;This is most commonly written as&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Use b...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This approach is simple and readable.  However, it has an unnecessary performance hit.  .Net casts are not free; every cast instruction must check that the object being casted is in fact convertible to that type.  Casting the same object twice will duplicate that check for no good reason, and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms182271&quot;&gt;should be avoided&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The recommended pattern for this is to pull the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;as&lt;/code&gt; check outside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Use b....&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, this code has the disadvantage of leaking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; into the containing scope, allowing you to accidentally use it outside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; block when it might be null.&lt;/p&gt;

&lt;p&gt;A clever (but less readable) alternative is&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Use b...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(&lt;a href=&quot;https://twitter.com/jonskeet/status/104281895289888768&quot;&gt;Credit&lt;/a&gt; to Jon Skeet)&lt;/p&gt;

&lt;p&gt;This works by abusing the elements of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop, and, in particular, taking advantage of the fact that the variable declared in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; statement is within its own scope.  The first clause simply declares the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; variable, and initializes it to be either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; or the correct type.  After running that, the compiler will immediately run the second clause (the termination condition).  Thus, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; is null (if the cast failed), it will exit the “loop” immediately.  Finally, after the body of the code block, it will run the third clause, setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; back to null and ensuring that the loop will not run again.&lt;/p&gt;

&lt;p&gt;For simple uses, C# 6 introduces the ideal way to do this with the conditional access operator:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)?.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StreamWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)?.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the cast fails, it will evaluate to null, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?.&lt;/code&gt; operator won’t do anything.  However, this only helps if you’re doing exactly one method call on the casted expression, and does not work for assignments or &lt;a href=&quot;https://github.com/dotnet/roslyn/issues/1276&quot;&gt;event handler registration&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side note:&lt;/em&gt; This should generally be avoided; instead, consider using a virtual method on the base class, or a visitor pattern.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-06-11-easier-csharp-async-with-await.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-06-11/easier-csharp-async-with-await/"/>
		<title type="text">Concurrency, part 6: Easier asynchrony in C# with await</title>
		<updated>2015-06-11T00:00:00+00:00</updated>
		<published>2015-06-11T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="async" term="async" />
		
		
		<category scheme="https://blog.slaks.net/#" label="promises" term="promises" />
		
		
		<category scheme="https://blog.slaks.net/#" label="multi-threading" term="multi-threading" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2015-06-10/advanced-promise-usage&quot;&gt;Last time&lt;/a&gt;, I described more advanced patterns for complicated workflows involving asynchronous operations.  These patterns can be annoying to write.  However, modern compilers can bear the brunt of this complexity, allowing you to write code as if the operations were synchronous, then letting the compiler transform your code into mes promise chains.&lt;/p&gt;

&lt;p&gt;C# 5 introduces this with its flagship new &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/hh191443&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; keywords&lt;/a&gt;.  However, the wealth of possibilities opened by this feature has left many developers confused about when to make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; methods and when not to.&lt;/p&gt;

&lt;h1 id=&quot;non-blocking-io&quot;&gt;Non-blocking IO&lt;/h1&gt;
&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; keyword does not &lt;em&gt;create&lt;/em&gt; asynchrony; instead, it allows you to call existing asynchronous methods easily.  If your code isn’t calling any existing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*Async()&lt;/code&gt; methods (which return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; won’t do any you good.  However, if you have the option of calling synchronous or asynchronous versions of an operation (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamWriter.WriteLine()&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamWriter.WriteLineAsync()&lt;/code&gt;), you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; to switch your code to use asynchronous operations, gaining all of the scalability benefits of non-blocking IO without any of the complexity.&lt;/p&gt;

&lt;h2 id=&quot;where-do-asynchronous-operations-come-from&quot;&gt;Where do asynchronous operations come from?&lt;/h2&gt;

&lt;p&gt;The point of an asynchronous operation is to wait for something to happen without wasting any threads.  For example, if you’re waiting for a web service to send you a reply, you can use an asynchronous HTTP client to wait for that reply and release the thread you were running on to do other things while you wait.  To make this happen, the operating system provides an asynchronous networking API that will wake up and call your asynchronous continuation when a response arrives from the network card.&lt;/p&gt;

&lt;p&gt;In other words, an operation can be made asynchronous if it simply waits for something to happen elsewhere, rather than actively computing something.  Operations like reading or writing from a hard disk or network, or waiting 5 seconds, are all inherently asynchronous; they do not involve any direct work beyond sending a request to the hard disk or network card, or setting a timer interrupt.&lt;/p&gt;

&lt;p&gt;By contrast, operations like adding a million numbers, or encrypting data, are &lt;em&gt;not&lt;/em&gt; asynchronous.  They may take a long time, but all of that time is spent actively computing something with the CPU, so you can’t release your thread and wait for something to happen on its own.  (if you have a separate chip to perform cryptographic operations, this is not true)&lt;/p&gt;

&lt;p&gt;For more details about this distinction, see &lt;a href=&quot;/2014-12-23/parallelism-async-threading-explained&quot;&gt;part 1 of this series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, you cannot use C#’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; keyword to create a new asynchronous operation; you can only create an asynchronous operation if you have some external event to wait for.&lt;/p&gt;

&lt;h1 id=&quot;so-what-are-async-methods-good-for&quot;&gt;So what are async methods good for?&lt;/h1&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; methods are good for &lt;em&gt;composing&lt;/em&gt; existing asynchronous operations – calling multiple operations in sequence and using the results.  All of the complex promise code described in my previous post can be implemented much more easily using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, if you wanted to find out which of two places was warmer, you could write the following asynchronous code by hand:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;placeTasks&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://api.weather.example.com/Locations/&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Uri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;EscapeUriString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;New York, NY&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://api.weather.example.com/Locations/&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Uri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;EscapeUriString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Seattle, WA&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
	
	&lt;span class=&quot;c1&quot;&gt;// Wait for both tasks to finish - for the response headers to start arriving&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WhenAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeTasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ContinueWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ReadAsStreamAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Wait for the response bodies&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ContinueWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xmlBodies&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;XDocument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hottest&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xmlBodies&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Descendants&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Current&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Single&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Temperature&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; makes this much simpler:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Wait for both tasks to finish - for the responses to start arriving&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;responses&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WhenAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeTasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Wait for the response bodies&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bodies&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WhenAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;responses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ReadAsStreamAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()));&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xmlBodies&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bodies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;XDocument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hottest&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xmlBodies&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Descendants&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Current&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Single&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Temperature&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Behind the scenes, the compiler will transform &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; methods into a series of promise callbacks, storing your local variables in a mutable closure class.  (It’s actually more complicated than that; Jon Skeet explained exactly how it works in his &lt;a href=&quot;https://codeblog.jonskeet.uk/2011/05/08/eduasync-part-1-introduction/&quot;&gt;Eduasync blog posts&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;This feature is most helpful for more complicated code, especially when consuming operations in sequence.  For example, the complicated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce()&lt;/code&gt; loop (from my &lt;a href=&quot;/2015-06-10/advanced-promise-usage&quot;&gt;previous blog post&lt;/a&gt;) to asynchronously process an array of items in sequence becomes much simpler:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SomeFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To gather the results, you can simply add them to a list:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;results&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SomeFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;!-- Due to a Markdown bug, I need an element between the code block and the header. --&gt;
&lt;h1 id=&quot;pitfalls&quot;&gt;Pitfalls&lt;/h1&gt;

&lt;p&gt;The most common mistake with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; methods is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async void&lt;/code&gt;.  An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; method is required to return one of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;.  If your function doesn’t return a value, it can be tempting to make it return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async void&lt;/code&gt;.  The problem with this option is that it makes it impossible for the caller to find out when the asynchronous portion of the method is finished, or to handle any errors that are thrown asynchronously.  As I explained in &lt;a href=&quot;/2015-01-04/async-method-patterns&quot;&gt;part 2&lt;/a&gt;, in order to wait for an asynchronous operation to finish, it must return a promise.&lt;/p&gt;

&lt;p&gt;The only correct place to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async void&lt;/code&gt; is event handlers.  Since the code that raised the event doesn’t care when you finish handling it, there is nothing wrong with hiding the promise from it.  (In fact, event handlers &lt;em&gt;must&lt;/em&gt; be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async void&lt;/code&gt;, since event delegates are declared as returning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;.)&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-06-10-advanced-promise-usage.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-06-10/advanced-promise-usage/"/>
		<title type="text">Concurrency, part 5: Advanced Promise Usage</title>
		<updated>2015-06-10T00:00:00+00:00</updated>
		<published>2015-06-10T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="async" term="async" />
		
		
		<category scheme="https://blog.slaks.net/#" label="promises" term="promises" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		
		
		<category scheme="https://blog.slaks.net/#" label="javascript" term="javascript" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2015-01-08/comparing-different-languages-promises-frameworks&quot;&gt;Last time&lt;/a&gt;, I listed standard &amp;amp; third-party promise libraries in popular languages, comparing how each one implements the promise paradigm.  In this post, I’ll describe some advanced patterns for promise usage.&lt;/p&gt;

&lt;h1 id=&quot;error-handling&quot;&gt;Error handling&lt;/h1&gt;
&lt;p&gt;One of the more useful features of promise-based asynchrony is automatic propagation of errors.  However, just like traditional exception handling, this feature is only useful if errors are correctly propagated up the call stack until they reach a method that knows how to handle them.&lt;/p&gt;

&lt;p&gt;Promise-based error handling adds an additional concern in that errors are only passed along explicit promise chains.  If you write a function that creates a promise chain and ignores it (without returning the resulting promise to its caller), errors in that chain will typically be silently ignored; this can hide serious bugs in your code.&lt;/p&gt;

&lt;p&gt;These concerns translate into a simple set of guidelines.  There are two kinds of methods that may involve errors: Library methods, which will throw errors, but do not know how to handle them, and application methods, which typically will not throw errors of their own, but do know how to handle errors from library methods that they call.  Given this distinction, the guidelines are as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Do not swallow errors.  If you call a method that returns a promise, you should either handle errors from that promise directly (for application methods), or return the promise to your caller so that it can handle errors elsewhere.&lt;/li&gt;
  &lt;li&gt;This is not a hard-and-fast rule.  If a library method invokes an operation and knows that no-one can possibly care about any failures (for example, if it tries two equivalent service endpoints, and one fails and the other succeeds), it may swallow the errors.  If possible, it should log the errors to help application developers troubleshoot unexpected scenarios.&lt;/li&gt;
  &lt;li&gt;If a library method creates multiple promise chains, it can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.all()&lt;/code&gt; to observe errors from any branch in the chains (consult the documentation for your promise library to find out how this behaves when multiple errors occur; many libraries have an alternative version of this method that will wait for every error).&lt;/li&gt;
  &lt;li&gt;If you don’t want to wait the method’s returned promise to wait or all of the promise chains to finish, you should probably refactor the method into multiple methods; each function should generally only serve one purpose.  The caller can then call each operation separately and wait for its result or handle errors as appropriate.&lt;/li&gt;
  &lt;li&gt;Any reusable asynchronous function should (almost) always return a promise that consumes all of the function’s asynchrony, allowing callers to wait for the entire operation to finish and to observe any errors that occurred.&lt;/li&gt;
  &lt;li&gt;In C#, this means that you should never write reusable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aync void &lt;/code&gt; methods (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Async Sub&lt;/code&gt; in VB); instead, always use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async Task&lt;/code&gt;, so that callers can wait for &amp;amp; observe the result.&lt;/li&gt;
  &lt;li&gt;Entry-point methods in applications should always handle errors from their promise chains &amp;amp; async method calls, displaying an error to the user and/or sending an error report to the developer.&lt;/li&gt;
  &lt;li&gt;For Javascript, the Q library has a useful &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;done()&lt;/code&gt; helper method, which can be called at the tip of a promise chain to throw any errors and trigger the environment’s default unhandled error notifications (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;window.onerror&lt;/code&gt; in a browser, or the error event in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;process&lt;/code&gt; for Node.js).  This is a convenient way to handle errors from promise chains in your application methods, especially if you already have a generic error handler for this event.&lt;/li&gt;
  &lt;li&gt;If a library method wants to add more context to an error, it can add an error callback that wraps the exception in a new exception with additional detail (making sure to preserve the inner exception details as well), then rethrows the new exception.  This will reject the rest of the promise chain with the new error, just like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throw new MySpecificException(&quot;...&quot;, ex)&lt;/code&gt; would inside a traditional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;catch&lt;/code&gt; block.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary, &lt;strong&gt;never leave a promise chain “dangling”&lt;/strong&gt;.  Instead, either return the resulting promise to your caller, or handle errors yourself at the end of the chain.&lt;/p&gt;

&lt;h1 id=&quot;calling-asynchronous-methods-in-loops&quot;&gt;Calling asynchronous methods in loops&lt;/h1&gt;
&lt;p&gt;One common challenge when working with asynchronous operations is running an asynchronous operation in a loop over a collection of source items.  Here too, promises can help simplify your code.  The technique for doing this depends whether you want to run in sequence or in parallel.&lt;/p&gt;

&lt;h2 id=&quot;parallel-operations&quot;&gt;Parallel Operations&lt;/h2&gt;
&lt;p&gt;If your operations are completely independent, and can run in parallel, you can simply kick off all of them at once, then wait for all of the promises to complete.  In Javascript, this looks like this:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[...];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;allDone&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This code creates an array of promises from the array of items, then waits for all of them to finish.  The resulting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;allDone&lt;/code&gt; promise will resolve to an array of the results of each of the original items.  You can build a promise chain inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; callback to perform a whole sequence of operations on each item and have each sequence run in parallel.  If you need to wait for all of the items to complete before continuing with further parallel operations, you can build a second set of parallel promise chains from the result of the first promise:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;allDone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;average&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;otherFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;average&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This second set of promise chains will also run in parallel, but will only start after all of the first chains finish.&lt;/p&gt;

&lt;h2 id=&quot;sequential-operations&quot;&gt;Sequential operations&lt;/h2&gt;
&lt;p&gt;Running a collection of asynchronous operations in sequence is more complicated.  If you don’t want each operation to start until the previous operation finishes, you must build a chain of promises, like this:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[...];&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This code uses the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.reduce()&lt;/code&gt; method&lt;/a&gt; to build up a chain of promises, executing each function after the previous one finishes.  The first parameter to the callback is the promise returned from the previous iteration (for the first item, it’ll be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.resolve()&lt;/code&gt; passed as the second parameter to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce()&lt;/code&gt;).  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce()&lt;/code&gt; callback adds a handler to that promise to execute the async operation on the current item, and returns the resulting promise, so that the next iteration will wait for it in turn.  The effect looks like this:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The value of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;promise&lt;/code&gt; passed into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce()&lt;/code&gt; callback is the result of the asynchronous function from the previous iteration.  Therefore, you can easily combine the current item with the result of the previous item.&lt;/p&gt;

&lt;p&gt;If you need an array of the results of all of the operations, you need to build the array from the promise results (starting with an empty array in the initial promise):&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[...];&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someFunctionAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;passing-state-along-a-promise-chain&quot;&gt;Passing state along a promise chain&lt;/h1&gt;
&lt;p&gt;When writing more-complex asynchronous workflows, you may need to pass state along a promise chain, from an intermediary promise result to a later promise callback.  For example, you might need to asynchronously fetch a post, fetch its author, then render both objects.&lt;/p&gt;

&lt;p&gt;Unfortunately, there is no good way to do this.  The best option is to build an array of all of the objects you need, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.all()&lt;/code&gt; to wait for new promises while keeping existing values:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;loadPost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;loadAuthor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;authorId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Do something with both values.&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.all()&lt;/code&gt; here will return a promise of the post and the author, which will be resolved once the author loads.  This call is necessary because returning a simple array from a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callback will not wait for the promises in the array.  Without it, you would still have a promise of the author in the second callback.&lt;/p&gt;

&lt;p&gt;In Javascript, the Q promise library has a &lt;a href=&quot;https://github.com/kriskowal/q/wiki/API-Reference#promisespreadonfulfilled-onrejected&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spread()&lt;/code&gt; method&lt;/a&gt; to simplify this pattern.  This method is like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt;, but will flatten the array into individual parameters.  It will even call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;all()&lt;/code&gt; for you, so you can return an array of promises and it will wait for all of them to be resolved first.  It would simplify the above code:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;loadPost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;loadAuthor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;authorId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)];&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;spread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;author&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Do something with both values.&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you need to load multiple items in separate steps, you can keep building larger and larger arrays of all of the items you need to load (obviously, you should try to load them in parallel – by returning multiple promises in a single array – where possible).&lt;/p&gt;

&lt;h1 id=&quot;caching-asynchronous-operations&quot;&gt;Caching asynchronous operations&lt;/h1&gt;
&lt;p&gt;Another common task when writing asynchronous code is to cache the result of an asynchronous operation.  As long as whatever you’re doing is reasonably idempotent (eg, loading data that rarely changes, or executing a fixed version of an external script file), you will generally want to load it just once, and have future calls reuse the first call.  This technique is called &lt;a href=&quot;https://en.wikipedia.org/wiki/Memoization&quot;&gt;memoization&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When doing this, you must be careful to avoid race conditions.  If you only cache each call after the result arrives, you can still end up making multiple calls if the second call is made before the first one responds.  Instead, you should cache the promise of the result immediately, so you can return that promise directly for the next call, whether it has loaded or not.  However, if the call returns an error, you will presumably want to clear it from the cache so that the next call can try again (unless you know it’s a permanent error).&lt;/p&gt;

&lt;p&gt;In Javascript, you might write the following code to memoize any single-argument asynchronous function:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;memoizeAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;thenCatch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;isPermanent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Unless we know that it isn't worth retrying,&lt;/span&gt;
					&lt;span class=&quot;k&quot;&gt;delete&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;		&lt;span class=&quot;c1&quot;&gt;// Remove this argument to try again next time.&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;				&lt;span class=&quot;c1&quot;&gt;// Rethrow the error so the result still fails.&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In multi-threaded environments (ie, not Javascript), you must also beware of thread-safety.  You can do this lock-free fashion by using a &lt;a href=&quot;/2013-07-29/creating-lock-free-data-structures&quot;&gt;compare-and-swap loop&lt;/a&gt; to insert the promise into an immutable map.  Make sure to only actually send the request after the compare-and-swap loop completes successfully, at which point you know that only your thread has added the value.  (Depending on how your request call works, you may need to add a promise resolver to the map, then resolve it to the request result after sending it.)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2015-06-11/easier-csharp-async-with-await&quot;&gt;&lt;em&gt;Next time: Using await for easier asynchrony in C#&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-05-16-challenges-from-silon.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-05-16/challenges-from-silon/"/>
		<title type="text">Challenges in Silon</title>
		<updated>2015-05-16T00:00:00+00:00</updated>
		<published>2015-05-16T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="less" term="less" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;While writing Silon, I had to overcome number of challenges.  You can see the full details in the comments &amp;amp; commit history of this repo, and in some cases in &lt;a href=&quot;https://github.com/less/less.js/issues?utf8=%E2%9C%93&amp;amp;q=is%3Aissue+author%3ASLaks&quot;&gt;bugs I filed in the LESS compiler&lt;/a&gt;.  Here are some of the more interesting ones.&lt;/p&gt;

&lt;p&gt;##Nested XOR
As described above, my operation mixins can take either simple selectors or further logical operations as their operand parameters.  For the most part, this is simple.  For AND operations, I can simply concatenate the parts together using sibling combinators (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt;).  For OR operations, I can simply select each operand separately, and the LESS compiler will automatically expand every combination of alternates via selector nesting.&lt;/p&gt;

&lt;p&gt;However, XOR is much harder.  To implement an XOR selector, I need to invert each operand (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a XOR b === (!a AND b) OR (a AND !b)&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;##LESS Scoping&lt;/p&gt;

&lt;p&gt;##Selector ordering&lt;/p&gt;

&lt;p&gt;##Duplicate subparts&lt;/p&gt;

&lt;p&gt;##Intermediate gates
For this use, the most frustrating issue with CSS is the complete inability to reuse state as a selector.  This is not a common problem.&lt;/p&gt;

&lt;p&gt;##Browser bugs
Chrome (only) will choke horribly if a CSS selector has more than a couple of hundred comma-separated parts.&lt;/p&gt;

&lt;p&gt;##Checkbox styling
To&lt;/p&gt;

&lt;p&gt;##Drawing logic gates&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-05-03-visual-studio-code-under-the-hood.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-05-03/visual-studio-code-under-the-hood/"/>
		<title type="text">Visual Studio Code under the hood</title>
		<updated>2015-05-03T00:00:00+00:00</updated>
		<published>2015-05-03T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio-code" term="visual-studio-code" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Last week, Microsoft shocked the development community with &lt;a href=&quot;https://code.visualstudio.com&quot;&gt;Visual Studio Code&lt;/a&gt;, a new lightweight version of Visual Studio with full support for Mac &amp;amp; Linux.   However, while Visual Studio Code may look very similar to the original Visual Studio, it’s actaully a complete rewrite, with almost nothing in common.  This blog post will explore VS Code’s architecture in greater depth.&lt;/p&gt;

&lt;p&gt;Like many recent products coming from DevDiv (Microsoft’s Developer Division), Visual Studio code is built almost entirely on open-source software (VS Code itself is not open source, but it &lt;a href=&quot;https://twitter.com/khellang/status/593466086705766400&quot;&gt;soon will be&lt;/a&gt;).&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-04-02-typename-comments-the-method-behind-the-madness.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-04-02/typename-comments-the-method-behind-the-madness/"/>
		<title type="text">Typename Comments, part 2: The method behind the madness</title>
		<updated>2015-04-02T00:00:00+00:00</updated>
		<published>2015-04-02T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2015-04-01/typename-comments-a-new-kind-of-comment&quot;&gt;Last time&lt;/a&gt;, I introduced a new syntax for code comments, typename comments.  April Fools!  However, although I do not intend for anyone to actually write code like this, everything I wrote in that blog post is true; this syntax does work.&lt;/p&gt;

&lt;p&gt;The reason this technique works is that the letter classes are both contained in and derived from the outer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt; class.  Because they’re contained in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt; class, they are referenced as static members of that class – &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope.A&lt;/code&gt;.  However, like any other static member, they can also be accessed through a qualifier of any class that derives from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt;.  Since they themselves derive from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt;, they can be chained, writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope.A.B.C&lt;/code&gt;, etc.  This is how we construct typenames that can be arbitrarily long&lt;/p&gt;

&lt;p&gt;Nesting all of the actual classes in the project within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt; class lets them be referenced in the same fashion.  This is why you can write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope.A.B.C.SomeRealClass&lt;/code&gt;.  Since the actual classes don’t also inherit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt;, you cannot chain them further, but there is no need for that.&lt;/p&gt;

&lt;p&gt;Finally, again since all of the actual classes are nested within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt;, you don’t need to qualify the typenames with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope.&lt;/code&gt; – like any other static members, they can be accessed without a qualifier anywhere inside the declaring class.&lt;/p&gt;

&lt;p&gt;This is why you can only use typename comments for types declared within your project (so they can be nested within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt;).  Any other type cannot be referenced at the end of the chain, since it isn’t nested within the class at the tip.&lt;/p&gt;

&lt;p&gt;Like other static members, the qualifier used to reference the member is purely syntactic sugar.  Whether you write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope.MyClass&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope.S.o.m.e.L.o.n.g.C.o.m.m.e.n.t.MyClass&lt;/code&gt;, you’re actually just referencing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope.MyClass&lt;/code&gt; (albeit in a convoluted way), so the comment is not visible in compiled code.&lt;/p&gt;

&lt;p&gt;The syntax highlighting is actually a side-effect of an unrelated feature new to Roslyn.  Roslyn will detect unnecessary qualifications – extra namespaces, using type names instead of keywords, or incorrect or unnecessary qualifiers for static members – and render them in a faded color, and also offer a quick fix to remove them.  This entire technique is about adding unnecessary qualifiers to typenames, so Roslyn fades all of the qualifiers to tell you to remove them.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-04-01-typename-comments-a-new-kind-of-comment.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-04-01/typename-comments-a-new-kind-of-comment/"/>
		<title type="text">Introducing Typename Comments – A New Kind of Comment</title>
		<updated>2015-04-01T00:00:00+00:00</updated>
		<published>2015-04-01T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="april-fools" term="april-fools" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;C# has two well-known styles of comments: C-style comments (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/* ... */&lt;/code&gt;) and C++-style comments (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;// ...&lt;/code&gt;).   This blog post introduces a third kind of comment: typename comments.  With a bit of supporting code, you can write comments as part of type names – in casts, variable declarations, generic parameters, and anywhere else a typename can occur.&lt;/p&gt;

&lt;p&gt;Typename comments are type expressions that precede any type that you define, like this:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;partial&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IPlayer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PlayAgainst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IPlayer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IPlayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;FindDefeatedOpponents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Player&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IPlayer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opponent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pastOpponents&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defeated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IPlayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;FindDefeatedOpponents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pastOpponents&lt;/span&gt;
				&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;defeated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IPlayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PlayAgainst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IPlayer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;opponent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You may have noticed the partial &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt; class that contains all of this code.  Unlike other comments, typename comments need this bit of supporting code.  You can only use typename comments if both the type being referenced and the code containing the comment are wrapped in the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scope&lt;/code&gt; class.  In addition, you also need to add the following supporting code once in your project:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;partial&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;D&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;E&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;G&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;H&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;K&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;M&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;O&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Q&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;W&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Z&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scope&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Although these supporting classes are compiled into your code, the comments themselves, like any other comment, will be stripped by the compiler.&lt;/p&gt;

&lt;p&gt;The best part of typename comments is their broad support spectrum: This syntax feature has been supported since the very first versions of both C# and VB.Net, over 13 years ago.  However, the Visual Studio IDE only added syntax highlighting for typename comments with Roslyn in VS2015.  Although it took them long enough to address this critical issue, you can now enjoy typename comments in their full glory:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/images/2015/vs-typename-comments.png&quot;&gt;&lt;img src=&quot;/images/2015/vs-typename-comments.png&quot; alt=&quot;Screenshot of syntax highlighting for typename comments&quot; style=&quot;max-width:100%;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2015-04-02/typename-comments-the-method-behind-the-madness&quot;&gt;&lt;em&gt;Next time: The method behind the madness&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-01-12-linq-count-considered-occasionally-harmful.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-01-12/linq-count-considered-occasionally-harmful/"/>
		<title type="text">Count() Considered Occasionally Harmful</title>
		<updated>2015-01-12T00:00:00+00:00</updated>
		<published>2015-01-12T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="LINQ" term="linq" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;How many times have you seen code like this?&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;someSequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;LINQ’s [&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.Count()&lt;/code&gt; method](https://msdn.microsoft.com/en-us/library/vstudio/bb338038() has an important performance subtlety which can make this code unnecessarily slow.  The root of the problem is that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; interface, which all LINQ methods operate on, doesn’t actually have a count (if it did, there would be no need for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt; extension method in the first place).  Therefore, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt; method is actually O(n), looping through the entire collection to count every item.&lt;/p&gt;

&lt;p&gt;Fortunately, the implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt; is a bit smarter than this.  As you can see in the &lt;a href=&quot;https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,41ef9e39e54d0d0b&quot;&gt;source&lt;/a&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt; will check whether the source collection implements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICollection&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICollection&lt;/code&gt;, and, if it does, will directly return the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count&lt;/code&gt; property without iterating at all (thus becoming O(1)).  Unfortunately, it does not check for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IReadOnlyCollection&amp;lt;T&amp;gt;&lt;/code&gt;; if you call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt; on a collection that implements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IReadOnlyCollection&amp;lt;T&amp;gt;&lt;/code&gt; but not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICollection&amp;lt;T&amp;gt;&lt;/code&gt;, it will still be O(n).  There are no such types in the BCL, but this has been enough of an issue for Roslyn that they wrote &lt;a href=&quot;https://source.roslyn.codeplex.com/#Roslyn.Diagnostics.Analyzers.CSharp/Performance/LinqAnalyzer.cs&quot;&gt;an analyzer&lt;/a&gt; to prevent it.&lt;/p&gt;

&lt;p&gt;In fact, it can be even worse than that.  It is perfectly possible to make an infinite &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; – a sequence that can be iterated as far as you want, without running out of elements.  For example:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;RandomStream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This sequence will work fine with most LINQ methods, because LINQ will only iterate through the sequence as far as it needs to.  However, if you call any method that consumes the entire collection (such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Last()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OrderBy()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ToList()&lt;/code&gt;, and some others), it will either hang forever, or eventually throw an OverflowException or OutOfMemoryException, depending on exactly what the method does (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Last()&lt;/code&gt;, for example, will hang without throwing anything).&lt;/p&gt;

&lt;p&gt;The lesson from this mess is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt; should be called with caution.  If you actually need to know exactly how many items are in a sequence, there is no alternative; you must simply beware that if the source doesn’t implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICollection&amp;lt;T&amp;gt;&lt;/code&gt;, it will be an O(n) call.  Obviously, if the sequence is in fact infinite, this will not – and cannot – work.&lt;/p&gt;

&lt;p&gt;If, on the other hand, you’re simply checking whether a sequence has at least &lt;em&gt;N&lt;/em&gt; elements, like the code at the beginning of this blog post, there is no need to iterate over the entire sequence, and no reason for the code to fail on infinite sequences.  All you need to do is iterate through the sequence, then stop immediately once you find &lt;em&gt;N&lt;/em&gt; elements.  You can do that in one line of LINQ:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;someSequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Skip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, this code is still not perfect.  If the sequence does in fact implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICollection&amp;lt;T&amp;gt;&lt;/code&gt;, this will be unnecessarily slow; instead of simply checking the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count&lt;/code&gt; property, it needs to instantiate two enumerators (one for each LINQ method), and iterate through the first &lt;em&gt;N&lt;/em&gt; items in the sequence.  In this scenario, checking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count()&lt;/code&gt; is in fact faster.&lt;/p&gt;

&lt;p&gt;To get the best of both worlds, you need to write your own LINQ method:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HasMoreThan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;staticCount&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ICollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;)?.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt; 
					&lt;span class=&quot;p&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ICollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)?.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt;
					&lt;span class=&quot;p&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IReadOnlyCollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;)?.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;staticCount&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;staticCount&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MoveNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This method first checks whether the sequence implements common collection interfaces, and, if so, retrieves the count directly.  If it doesn’t, it manually enumerates that many elements from the enumerator to see if it runs out.&lt;/p&gt;

&lt;p&gt;Using the enumerator directly is slightly faster than using LINQ methods; it avoids allocating an extra iterator state machine from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Skip()&lt;/code&gt;.  You can simplify it like this at a slight GC cost:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Skip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Or&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;All&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MoveNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Each of these implementations involve one extra allocation (an iterator state machine) compared to the longer version.&lt;/p&gt;

</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-01-08-comparing-different-languages-promises-frameworks.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-01-08/comparing-different-languages-promises-frameworks/"/>
		<title type="text">Concurrency, part 4: Comparing promises frameworks in different languages</title>
		<updated>2015-01-08T00:00:00+00:00</updated>
		<published>2015-01-08T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="async" term="async" />
		
		
		<category scheme="https://blog.slaks.net/#" label="promises" term="promises" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		
		
		<category scheme="https://blog.slaks.net/#" label="javascript" term="javascript" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2015-01-05/introducing-promises&quot;&gt;Last time&lt;/a&gt;, I explained what promises are and how they work.  Now, I’ll explore standard &amp;amp; third-party promise libraries in popular languages, explaining how they implement each facet of the promise paradigm.&lt;/p&gt;

&lt;h1 id=&quot;net-languages-c-vb-etc&quot;&gt;.Net languages (C#, VB, etc…)&lt;/h1&gt;
&lt;p&gt;The .Net base class library includes promise classes called &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd321424&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;TResult&amp;gt;&lt;/code&gt;&lt;/a&gt;.  These classes were introduced in .Net 4.0, released in 2010.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; is used for promises which have no value (simply running callbacks once the operation is complete); &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;TResult&amp;gt;&lt;/code&gt; inherits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; and exposes a value as well.&lt;/p&gt;

&lt;h2 id=&quot;methods&quot;&gt;Methods&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;The then() method, which adds a callback to the promise, is called &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd321274&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ContinueWith()&lt;/code&gt;&lt;/a&gt;.  It has a number of overloads which take different kinds of delegates, letting you pass callbacks that don’t return anything, return a synchronous value, or return a further promise for chaining.  All of these methods return a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;TResult&amp;gt;&lt;/code&gt; instance.  The callback receives the original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt; object rather than the result; you can access the result using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Result&lt;/code&gt; property.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ContinueWith()&lt;/code&gt; will, by default, run its callback even if the task failed.  However, if the original task failed, accessing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Result&lt;/code&gt; property inside the callback will rethrow the exception, skipping the rest of the callback.  You can add callbacks that only run on success or failure by passing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskContinuationOptions.OnlyOnRanToCompletion&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskContinuationOptions.OnlyOnFaulted&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;The Deferred object used to create promises from existing asynchronous operations is called &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd449174&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskCompletionSource&amp;lt;TResult&amp;gt;&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;You can also create tasks from legacy APM (Asynchronous Programming Model, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IAsyncResult&lt;/code&gt;) using the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.Factory.FromAsync()&lt;/code&gt; methods&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Promise creation helper methods include &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/hh194922&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.FromResult()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.fromexception&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.FromException()&lt;/code&gt;&lt;/a&gt; to create pre-fulfilled tasks, &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.delay&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.Delay*(&lt;/code&gt;&lt;/a&gt; to create a task that will be resolved at a specified time in the future, and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenall&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.WhenAll()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenany&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.WhenAny()&lt;/code&gt;&lt;/a&gt; to wait for multiple tasks.&lt;/li&gt;
  &lt;li&gt;You can run code on a background thread and get a promise of its eventual result by calling &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.run&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.Run()&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;behavior&quot;&gt;Behavior&lt;/h2&gt;
&lt;p&gt;By default, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ContinueWith()&lt;/code&gt; will always run the callback asynchronously (using the current &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskScheduler&lt;/code&gt;), avoiding reentrancy bugs.  You can override this behavior by passing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskContinuationOptions.ExecuteSynchronously&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Tasks also support cancellation.  Cancellation is a third fulfillment state that is mostly parallel to rejection.  When creating a new Task from a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskCompletionSource&lt;/code&gt;, you can call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SetCancelled&lt;/code&gt; to set the resulting task to canceled.  If a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ContinueWith()&lt;/code&gt; callback throws a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskCancelledException&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OperationCanceledException&lt;/code&gt;, the resulting task will also be canceled.  Cancellation is designed to be used together with the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd997364&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CancellationSource&lt;/code&gt; system&lt;/a&gt; to cancel operations and callbacks.&lt;/p&gt;

&lt;p&gt;Since .Net is a multi-threaded environment, you can also synchronously wait for task to finish by calling &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.wait&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.Wait()&lt;/code&gt;&lt;/a&gt;.  This will block the calling thread until the task is fulfilled, so it should not be called on a UI thread or in highly multi-threaded environments when threads are scarce.&lt;/p&gt;

&lt;p&gt;Most confusingly, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; class can also represent a delegate running on a background thread.  You can create a new task from a delegate, which will return an unstarted task object.  Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Start()&lt;/code&gt; on this instance will run the delegate on its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskScheduler&lt;/code&gt; and eventually resolve the task to its result.  This use (and the entire &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Start()&lt;/code&gt; method) is confusing and should be avoided; instead, simply call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task.Run()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;C# 5.0 &amp;amp; VB 11.0 took this one step further with language-level asynchrony using the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/hh191443&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; keyword&lt;/a&gt;, allowing you work with promises just like you would write synchronous code.  Behind the scenes, the compiler transforms async code into a series of promise callbacks.  Jon Skeet wrote a series of blog posts explaining &lt;a href=&quot;https://blogs.msmvps.com/jonskeet/2011/05/08/eduasync-part-1-introduction/&quot;&gt;exactly how this transformation works&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;availability&quot;&gt;Availability&lt;/h2&gt;
&lt;p&gt;.Net 4.5 introduced &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*TaskAsync()&lt;/code&gt; versions of most of the framework’s non-CPU-bound operations, including stream IO, HTTP requests, database connections, and more.  Note that the async methods in &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.data.common.dbconnection&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbConnection&lt;/code&gt;&lt;/a&gt; &amp;amp; &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.data.common.dbcommand&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbCommand&lt;/code&gt;&lt;/a&gt; will by default run synchronously and return a pre-completed task; it’s up to the derived connection clients to override these methods with non-blocking implementations.  The built-in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SqlConnection&lt;/code&gt; does override these methods; other ADO.Net providers may or may not.&lt;/p&gt;

&lt;p&gt;All modern .Net unit testing frameworks have some level of support for async tests; for more details, see &lt;a href=&quot;https://msdn.microsoft.com/en-us/magazine/dn818494.aspx&quot;&gt;Stephen Cleary’s article on async .Net unit tests&lt;/a&gt;.  Recent versions of ASP.Net MVC and Web API also allow async actions that return Tasks; ASP.Net vNext (6.0) also adds support for async action filters.  Entity Framework also added support for async queries and updates (assuming the underlying provider implements async correctly) &lt;a href=&quot;https://msdn.microsoft.com/en-us/data/jj819165.aspx&quot;&gt;in version 6&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Outside Microsoft’s standard libraries, the situation is murkier.  Older open source libraries for network-bound operations frequent lack async methods or implement them incorrectly (by running a blocking call on a thread pool thread instead of a non-blocking call).  Newer libraries are more likely to get this right, especially with the introduction of language-level async support in 2012.  Stephen Cleary wrote an excellent set of &lt;a href=&quot;https://msdn.microsoft.com/en-us/magazine/jj991977.aspx&quot;&gt;guidelines on how to properly write async APIs&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;java-7--guava&quot;&gt;Java 7 – Guava&lt;/h1&gt;
&lt;p&gt;Java’s promise story is murkier.  Java 1.5 introduced the &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future&amp;lt;T&amp;gt;&lt;/code&gt; interface&lt;/a&gt;, which looks like a promise, but actually isn’t.  A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future&amp;lt;T&amp;gt;&lt;/code&gt; does indeed represent a value which will be computed later, but it offers no way to notify upon completion – it has no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; method.  All you can do with a future is synchronously wait for it to be completed.  Thus, it is completely useless for non-blocking asynchronous operations.&lt;/p&gt;

&lt;p&gt;To solve this, Google’s &lt;a href=&quot;https://code.google.com/p/guava-libraries/&quot;&gt;Guava library&lt;/a&gt; has its own promise-like interface called &lt;a href=&quot;https://google.github.io/guava/releases/20.0/api/docs/com/google/common/util/concurrent/ListenableFuture.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ListenableFuture&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;.  This interface extends the standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future&amp;lt;T&amp;gt;&lt;/code&gt; and adds a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addListener()&lt;/code&gt; method that takes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Runnable&lt;/code&gt; callback and an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Executor&lt;/code&gt; to run the callback with (letting you control whether the callback will run synchronously or not).   This is an extremely simple API designed to be easy to implement; the callback will run on both success and failure and does not allow chaining.  Instead, the full richness &amp;amp; convenience of the usual promise APIs are available as static methods in the &lt;a href=&quot;https://google.github.io/guava/releases/20.0/api/docs/com/google/common/util/concurrent/Futures.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures&lt;/code&gt; class&lt;/a&gt;.
x&lt;/p&gt;
&lt;h2 id=&quot;methods-1&quot;&gt;Methods&lt;/h2&gt;
&lt;p&gt;By default, callback methods will run synchronously.  You can make them run asynchronously by passing an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Executor&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The then() method is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.transform()&lt;/code&gt;, which has overloads that take a callback returning a synchronous value or a further promise (if you don’t need to return a further promise, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.addCallback()&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;To handle errors, call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.withFallback()&lt;/code&gt;.  This does not have an overload that accepts a promise-returning callback; if you need one, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.dereference()&lt;/code&gt; to turn a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ListenableFuture&amp;lt;ListenableFuture&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt; into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ListenableFuture&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;The Deferred object is &lt;a href=&quot;https://google.github.io/guava/releases/20.0/api/docs/com/google/common/util/concurrent/SettableFuture.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SettableFuture&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;, which implements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ListenableFuture&amp;lt;T&amp;gt;&lt;/code&gt; and adds mutation methods to resolve or reject the promise.&lt;/li&gt;
  &lt;li&gt;Promise creation helpers include &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.immediateFuture()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.immediateFailedFuture()&lt;/code&gt; to create pre-fulfilled promises, as well as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.allAsList()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Futures.anyAsList()&lt;/code&gt; to wait for multiple promises.&lt;/li&gt;
  &lt;li&gt;You run code on a background thread in a Java &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Executor&lt;/code&gt; and get a promise of the result by wrapping the executor in &lt;a href=&quot;https://google.github.io/guava/releases/20.0/api/docs/com/google/common/util/concurrent/MoreExecutors.html#listeningDecorator-java.util.concurrent.ExecutorService-&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MoreExecutors.listeningDecorator()&lt;/code&gt;&lt;/a&gt; and calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;submit()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more information, see &lt;a href=&quot;https://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained&quot;&gt;Guava’s introduction to ListenableFuture&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;java-8&quot;&gt;Java 8&lt;/h1&gt;
&lt;p&gt;Java 8 finally introduced a built-in promise interface, &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionStage.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletionStage&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;methods-2&quot;&gt;Methods&lt;/h2&gt;
&lt;p&gt;Every method in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompetionStage&lt;/code&gt; has three variants: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method(callback)&lt;/code&gt; runs its callback synchronously, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;methodAsync(callback)&lt;/code&gt; runs the callback asynchronously on the common ForkJoinPool, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;methodAsync(callback, executor)&lt;/code&gt; runs the callback asynchronously on the specified executor.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The then() method is represented by a number of methods, depending on whether the callback consumes the value and/or returns a new value.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thenAccept()&lt;/code&gt; consumes the value and returns void.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thenApply()&lt;/code&gt; consumes the value and returns a different value.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thenCompose()&lt;/code&gt; consumes the value and returns a new promise.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thenRun()&lt;/code&gt; consumes nothing and returns nothing (it accepts a simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Runnable&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;To handle errors, call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exceptionally()&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handle()&lt;/code&gt; to run a callback whether the stage was resolved or rejected.&lt;/li&gt;
  &lt;li&gt;The Deferred object is &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletableFuture&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;, which implements CompletionStage (and is in fact the only built-in implementation), and adds mutator methods to resolve, reject, or cancel the promise.&lt;/li&gt;
  &lt;li&gt;Promise creation helpers are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletableFuture.allOf()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletableFuture.anyOf()&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletableFuture.completedFuture()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;CompletionStage also has binary versions of all() and any() as instance methods.  Call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stage1.{accept|applyTo|runAfter}Either(stage2, callback)&lt;/code&gt; (parallel to the standard then() variants listed above) to add a then() callback to whichever promise completes first.  Call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stage1.{thenAccept|runAfter}Both()&lt;/code&gt; to add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callback to both promises together.&lt;/li&gt;
  &lt;li&gt;You run code on a background thread in a Java &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Executor&lt;/code&gt; and get a promise of the result by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletableFuture.runAsync()&lt;/code&gt; (returns a promise of void) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletableFuture.supplyAsync()&lt;/code&gt; (returns a promise of a value).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;availability-1&quot;&gt;Availability&lt;/h2&gt;
&lt;p&gt;Unfortunately, CompletionStage is not used anywhere in the JDK standard library.  There are third-party async libraries that return CompletionStages.&lt;/p&gt;

&lt;h1 id=&quot;javascript&quot;&gt;Javascript&lt;/h1&gt;
&lt;p&gt;Javascript has a much richer ecosystem of promises.  Because Javascript is strictly single-threaded, JS programmers have had to deal with asynchrony since day 1.  Different asynchronous APIs have taken different approaches to this; earlier browser APIs such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XmlHttpRequest&lt;/code&gt; uses events, and Node.js uses callbacks everywhere.  Promises slowly began to arrive onto this landscape, including jQuery’s original version of &lt;a href=&quot;https://api.jquery.com/category/deferred-object/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.Deferred&lt;/code&gt;&lt;/a&gt; or Closure’s &lt;a href=&quot;https://docs.closure-library.googlecode.com/git/class_goog_async_Deferred.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;goog.async.Deferred&lt;/code&gt;&lt;/a&gt;, which are mostly, but not entirely, like promises.  Later came a variety of Node.js and/or browser-based promise libraries, including &lt;a href=&quot;https://api.jquery.com/category/deferred-object/&quot;&gt;Q&lt;/a&gt;, &lt;a href=&quot;https://github.com/petkaantonov/bluebird&quot;&gt;Bluebird&lt;/a&gt;, and many others.  Most recently, the upcoming ECMAScript 6 standard includes a &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; class&lt;/a&gt; (although many third-party implementations such as Q have richer APIs).&lt;/p&gt;

&lt;p&gt;To simplify this mess, Kris Kowal, Domenic Denicola, and others put together the &lt;a href=&quot;https://promisesaplus.com&quot;&gt;Promises/A+ spec&lt;/a&gt;, which codifies exactly how Javascript promises should behave.  Promises/A+ specifies how promise resolution should work, and the exact signature and behavior of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; method for adding success &amp;amp; error callbacks and chaining.  It leaves everything else up to the individual implementations; most notably, it makes no mention of how to create a promise in the first place.&lt;/p&gt;

&lt;p&gt;All modern promise implementations, including &lt;a href=&quot;https://api.jquery.com/category/deferred-object/&quot;&gt;Q&lt;/a&gt;, &lt;a href=&quot;https://github.com/petkaantonov/bluebird&quot;&gt;Bluebird&lt;/a&gt;, &lt;a href=&quot;https://docs.angularjs.org/api/ng/service/$q&quot;&gt;Angular’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$q&lt;/code&gt;&lt;/a&gt; (which is based on Q), &lt;a href=&quot;https://github.com/petkaantonov/bluebird&quot;&gt;Bluebird&lt;/a&gt;, Closure’s &lt;a href=&quot;https://docs.closure-library.googlecode.com/git/class_goog_Promise.html&quot;&gt;goog.Promise&lt;/a&gt;, but with the notable exception of jQuery, including the ES6 promise class implement Promises/A+, and also add promise creation and convenience methods.  The Promises/A+ site has a &lt;a href=&quot;https://promisesaplus.com/implementations&quot;&gt;full list of conforming implementations&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;methods-3&quot;&gt;Methods&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;The then() method is precisely specified by Promises/A+, and is thus (almost) the same in every implementation.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; accepts a success callback followed by a failure callback (both of which are optional), and will always call them asynchronously (on the next message loop iteration).&lt;/li&gt;
  &lt;li&gt;Many promise implementation add a third parameter to specify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; parameter with which to call the callbacks.&lt;/li&gt;
  &lt;li&gt;To add error callbacks, pass the second parameter to then.  Most implementations also add a convenience method to only add an error callback; ES6 promises have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.catch()&lt;/code&gt;; Q has &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.catch()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.fail()&lt;/code&gt;, and Closure promises have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.thenCatch()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;The Deferred objects vary between implementations.  ES6 promises and Q 2.0 use a promise constructor as described previously.  Q 1.0 has &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Q.defer()&lt;/code&gt;, which creates a Deferred object; Closure promises have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;goog.Promise.withResolver()&lt;/code&gt; which returns a similar object.&lt;/li&gt;
  &lt;li&gt;Promise creation helpers include &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.resolve&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.reject&lt;/code&gt; to create pre-fulfilled promises, as well as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.all()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.race()&lt;/code&gt; to consume multiple promises.  These names are the same in most promise implementations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more details, consult the documentation for your specific implementation.&lt;/p&gt;

&lt;h2 id=&quot;availability-2&quot;&gt;Availability&lt;/h2&gt;
&lt;p&gt;There are no standard APIs that return promises (yet).  However, there are a large variety of open-source libraries (for Node.js and the browser) that expose promise-based APIs.&lt;/p&gt;

&lt;p&gt;One of the advantages of the Promises/A+ standard is that all every standards-compliant implementation should be interoperable.  You can take a promise from one implementation, and return it from a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callback, or pass it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;all()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;race()&lt;/code&gt;, in a different implementation, and everything should work fine.  This is particularly useful when consuming a library that uses a different promise implementation than your code uses.&lt;/p&gt;

&lt;h1 id=&quot;jquery-promises&quot;&gt;jQuery Promises&lt;/h1&gt;
&lt;p&gt;jQuery introduced its own promise-like abstraction in version 1.5, &lt;a href=&quot;https://api.jquery.com/category/deferred-object/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.deferred()&lt;/code&gt;&lt;/a&gt;.  This feature was introduced before promises had a widespread standard API, and its API changed substantially in version 1.8 to align more closely with other promise libraries.  jQuery still has one notable difference; promise callbacks are always executed synchronously (directly within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resolve()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt;).  jQuery deferreds also support the notion of a progress callback, which can be invoked multiple times before the promise is resolved to report incremental progress.&lt;/p&gt;

&lt;h2 id=&quot;methods-4&quot;&gt;Methods&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;The then() method is &lt;a href=&quot;https://api.jquery.com/deferred.then/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt;&lt;/a&gt;, accepting optional success, failure, and progress callbacks and returning a chained promise of the callback’s return value.&lt;/li&gt;
  &lt;li&gt;Deferreds also have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;done()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fail()&lt;/code&gt; methods which add callbacks directly, &lt;strong&gt;but do not return a new promise&lt;/strong&gt;.  Instead, they return the original promise instance, like normal jQuery method chaining.  This means that they can be used to add multiple callbacks to the same original value.  However, the return value from the callback is completely ignored; to create further promises, you must use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.then()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.deferred()&lt;/code&gt; returns a mutable deferred object, which can be used to resolve the promise.  Call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.promise()&lt;/code&gt; on this object to get a readonly promise reflecting the result of the deferred.&lt;/li&gt;
  &lt;li&gt;jQuery has just one promise creation helper, &lt;a href=&quot;https://api.jquery.com/jQuery.when/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.when()&lt;/code&gt;&lt;/a&gt;.  You can pass a single value to this method to create a resolved promise of the value.  You can pass multiple jQuery promises (not other promise implementations) to this function to create a promise that waits for all of them.  Note that you cannot pass an array of promises (if you do, you’ll get a resolved promise of that array itself); you must pass the promises individually.  If you have an array, you can call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.when.apply(null, promisesArray)&lt;/code&gt; to pass each item in the array as a separate parameter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;availability-3&quot;&gt;Availability&lt;/h2&gt;
&lt;p&gt;jQuery returns promises from all of its asynchronous operations.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.ajax()&lt;/code&gt; (and equivalent helper methods like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.get()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.getJSON()&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$.getScript()&lt;/code&gt;) return jqXHR objects which are promises with additional AJAX-related properties.  jQuery objects also have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.promise()&lt;/code&gt; method which returns a promise of the end of the element(s)’s animation queue.  This lets you easily wait for animations to finish running before continuing with other code via promise chains.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2015-06-10/advanced-promise-usage&quot;&gt;&lt;em&gt;Next time: Advanced promise development guidelines&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-01-05-introducing-promises.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-01-05/introducing-promises/"/>
		<title type="text">Concurrency, part 3: Promises – Asynchronous programming made easy</title>
		<updated>2015-01-05T00:00:00+00:00</updated>
		<published>2015-01-05T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="async" term="async" />
		
		
		<category scheme="https://blog.slaks.net/#" label="promises" term="promises" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		
		
		<category scheme="https://blog.slaks.net/#" label="javascript" term="javascript" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2015-01-04/async-method-patterns&quot;&gt;Last time&lt;/a&gt;, I explored the two main options for writing asynchronous functions.  Now, I’ll describe promises in more depth.  The concept of a promise can be implemented in any language; the sample code is in Javascript, running in a Node-like environment.  Part 4 will describe the details promise frameworks in various other languages.&lt;/p&gt;

&lt;h1 id=&quot;basics&quot;&gt;Basics&lt;/h1&gt;
&lt;p&gt;A promise is an object that stores an asynchronously-computed value, or an error.   The only way to consume the promise’s value (or error) is to pass it a callback by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; (some frameworks have different names for the method).  A promise is always in one of three states: unresolved, fulfilled, or rejected.  An unresolved promise has no value yet, and instead stores a list of callbacks to run once the promise is resolved.  In this state, calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; will store the callback in this list to be consumed later.&lt;/p&gt;

&lt;p&gt;Once the operation backing the promise completes, the promise is resolved to a fulfilled or rejected state.  At this point, the resolved value is stored in the promise, and the list of callbacks is executed and cleared.  Once a promise has been resolved, it becomes completely immutable, storing only the result.  Passing a callback to a resolved promise will execute it immediately.  Some implementations will execute callbacks on resolved promises synchronously, making &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; a potentially reentrant call; others, including all standards-compliant Javascript promises, will always run it asynchronously in a threadpool or a later message loop).&lt;/p&gt;

&lt;p&gt;Note that a promise can only be resolved once.  Thus, promises are only useful for operations that complete once and result in a single value (or collection).  If you have an operation that can finish in multiple stages (eg, if it can have change events, or if different parts come in at different times), promises will not help; instead, you can use a stream abstraction, such as Node.js &lt;a href=&quot;https://github.com/substack/stream-handbook/blob/master/readme.markdown&quot;&gt;streams&lt;/a&gt; or .Net &lt;a href=&quot;https://rx.codeplex.com/&quot;&gt;Reactive Extensions&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;creating-promises&quot;&gt;Creating promises&lt;/h1&gt;
&lt;p&gt;Promises are created from asynchronous operations.  Modern asynchronous methods (eg, most new .Net APIs, and promise-aware Javascript libraries) will generally return promises themselves, letting you consume them without further effort.  You can also create promises from other things, as described below.&lt;/p&gt;

&lt;p&gt;Multi-threaded environments like C# or Java 8 also have thread pool APIs that run code in a background thread, then return a promise of the eventual result of the code.  This is particularly useful in UI applications, to run CPU-bound work in a background thread, then consume the result in the UI.&lt;/p&gt;

&lt;p&gt;You can also create promises manually.  This is necessary when calling older asynchronous APIs that accept callbacks instead of returning promises.  It can also be useful for more unusual asynchronous operations, such as creating a promise that will be resolved when a user clicks a button or closes a dialog, or waiting for specific commands to come in over a live network stream.&lt;/p&gt;

&lt;p&gt;To create a promise manually, you need functions to call to manually resolve or reject the new promise, passing the resolved value or the rejection error.  Your code would call these functions in the callback for the underlying operation, fulfilling the promise when the operation is finished.  You then need to get the resulting promise object, which, while itself readonly, will reflect the fulfillment from your function calls.&lt;/p&gt;

&lt;p&gt;Most promise frameworks provide this using a Deferred object (in .Net, this is called TaskCompletionSource), which is a mutable wrapper around a promise.  The deferred object exposes its promise as a readonly property, and provides resolve and reject as member functions.  Since deferred objects are mutable, they should not exposed publicly; instead, they should only be used to wrap the underlying asynchronous operation.  The public interface (property or return value) should only expose the generated promise.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFilePromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;deferred&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Deferred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;deferred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;deferred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;deferred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Modern Javascript promise implementations introduced a cleaner way to create a promise from an existing asynchronous method.  You can create a new promise and pass a resolver callback.  The promise constructor will this callback immediately, passing the resolve and reject functions as parameters.  This makes scope management easier; instead of having a separate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deferred&lt;/code&gt; variable, the mutable part of the promise is intrinsically scoped to the resolver callback.  This may not work for more advanced operations, such as resolving a promise based on a UI event or a particular network response, but it’s a much nicer way to wrap existing callback-based functions.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFilePromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;nx&quot;&gt;reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
				&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;More sophisticated promise libraries will also include helper methods to “promisify” existing async methods using standard callback patterns, such as Q’s &lt;a href=&quot;https://github.com/kriskowal/q#adapting-node&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nfcall&lt;/code&gt; &amp;amp; friends&lt;/a&gt; or C#’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskFactory.FromAsync&lt;/code&gt; overloads&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, most promise frameworks include convenience methods to create pre-resolved or pre-rejected promises from existing values (useful when an async API has a synchronous “fast path” such as a cache), as well as promises that will be resolved after a given delay.&lt;/p&gt;

&lt;h1 id=&quot;composing-promises&quot;&gt;Composing promises&lt;/h1&gt;
&lt;p&gt;The biggest advantage to promise-based asynchrony is the ease of composition.  You can return a value from a promise callback to get a new promise which will be resolved to that value once the original promise completes.  This lets you easily create functions that compute values based on the result of an existing async operation, simply by returning the computed value from the promise callback, and returning the resulting promise from the function.&lt;/p&gt;

&lt;p&gt;You can also return another promise from a promise callback, giving you a promise that will be resolved once this second operation finishes.  This lets you chain together a sequence of asynchronous by simply returning the promise for each new operation from successive then() callbacks on the resulting promises.  This is much nicer than nesting multiple completion callbacks from old-style async methods.  In effect, this lets the inner promise “escape” from the scope of its promise callback, giving you a promise of its result in the original function.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;initialPromise&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someAsyncFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;delayedPromise&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;initialPromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someOtherAsyncFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Returns a promise&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// delayedPromise will be resolved with the &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// result of someOtherAsyncFunction()&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;thirdPromise&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;delayedPromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// thirdPromise will be resolved with the&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// number of properties after the other 2&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// promises are resolved.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;More formally, this feature is an example of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Monad_%28functional_programming%29&quot;&gt;monad&lt;/a&gt; – a generic type that “amplifies” (adds more features to) any existing type.  Other examples of monads include collection types, which amplify an existing type to store a number of values of that type, and optional/maybe types, which store either a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt; placeholder or a single value.  The monadic &lt;em&gt;unit&lt;/em&gt; operation, which creates a promise from an existing value, is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.resolve()&lt;/code&gt; (or equivalents in other frameworks).  The mondaic &lt;em&gt;bind&lt;/em&gt; operation, which applies a function to the value in a monad and returns a new monad containing the function’s result(s), is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; method.  This is what makes promises chainable.  For a deeper introduction to monads, see &lt;a href=&quot;https://ericlippert.com/2013/02/21/monads-part-one/&quot;&gt;Eric Lippert’s excellent series of blog posts&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Promise frameworks will also include helper methods to consume collections of other promises.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.all()&lt;/code&gt; takes a collection of promises and returns a new promise of an array of the results of these promises, thus waiting for all of the promises to be resolved.  This can be used to kick off a number of asynchronous operations in parallel, then wait for them all to finish.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise.any()&lt;/code&gt; takes a collection of promises and returns a promise of the result of the first promise to be resolved (ignoring all of the other promises).&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getWeather&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Try 3 services and use whichever one answers first.&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;httpRequestPromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://weather1.example.com&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;city&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}),&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;httpRequestPromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://weather2.example.com&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;city&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}),&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;httpRequestPromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://weather3.example.com&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;city&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getTripWeather&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;itinerary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;itinerary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getWeather&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;error-handling&quot;&gt;Error handling&lt;/h1&gt;
&lt;p&gt;Promises can also store error state.  When an asynchronous operation fails, the resulting promise will be rejected rather than resolved, storing the error instead of the result.  When this happens, none of the regular &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callbacks will run; instead, the promises returned by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; calls will themselves be rejected.  Thus, errors will automatically propagate down a promise chain; once an error occurs, the rest of the handlers will be skipped, until the error is handled.  If an exception is thrown in any promise callback, the promise returned by that callback will also be rejected.  Similarly, if a promise callback returns a promise (from a chained async operation) which is rejected, the next promise returned by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; will also be rejected.&lt;/p&gt;

&lt;p&gt;To handle errors, you can add an error callback, typically either by passing a second callback to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; or by passing a callback to a separate function like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fail()&lt;/code&gt;.  These behave exactly the same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callbacks, except that they will only run when the promise is rejected.  A resolved (as opposed to rejected) promise will skip error callbacks, just like a rejected promise skips normal callbacks.&lt;/p&gt;

&lt;p&gt;Error callbacks will only handle errors from earlier in the promise chain.  If you add both a success callback and an error callback in the same call (by passing two callbacks to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt;), exactly one of the callbacks will run, depending on the fulfillment of the original promise.  In contrast, if you call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.then(callback).fail(otherCallback)&lt;/code&gt;, and the original promise succeeds, but the first callback throws its own error, both callbacks will run (since you’re calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fail()&lt;/code&gt; on the rejected promise from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callback).&lt;/p&gt;

&lt;p&gt;Error callbacks are still chainable; the promise returned by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fail()&lt;/code&gt; will be resolved to whatever the callback returns.  This means that once you add an error callback, the rest of the chain will keep running after an error occurs (error callbacks handle their errors).  To prevent this, you can simply rethrow the error from the error callback, causing the next promise to be rejected again.&lt;/p&gt;

&lt;p&gt;In effect, error callbacks behave exactly like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;catch&lt;/code&gt; blocks in imperative programming.  Just like classical exceptions, once an error occurs (whether the original operation failed, or whether a callback threw any kind of error), the rest of the code (other callbacks) until the “catch block” (error callback) will be skipped.  After the “catch block”, the rest of the code (further chained callbacks) will continue running, unless the “catch block” rethrows the error.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;someFailingFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Returns a rejected promise&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;// This code will not run&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Uh oh!&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Prints 3&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;// Had the original operation not failed, this &lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;// would have received the result of the first&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;// then() callback.&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;unit-testing&quot;&gt;Unit testing&lt;/h1&gt;
&lt;p&gt;Asynchronous functions create special challenges for unit tests.  A unit test for an asynchronous function must wait until the async part of the method finishes, and must catch exceptions or assertion failures that are raised in asynchronous continuations (in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callbacks).  A naive unit test that does not somehow wait for asynchronous operations to complete won’t actually test anything; if the asynchronous part fails, it will never be reported.&lt;/p&gt;

&lt;p&gt;To solve this issue, most modern test runners allow unit tests to return promises.  The test runner will check whether each test returns a promise, and, if it does, will wait for the promise to be resolved before concluding the test.  If the returned promise is rejected, it will mark the test as failed, with the rejection reason as the failure message.&lt;/p&gt;

&lt;p&gt;Therefore, you can call an asynchronous function in a test, add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; callback that makes assertions about the result, and simply return that promise chain.  If the assertions fail, the assert function will throw an exception, causing the promise returned by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; to fail, and failing the test.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;testSquareAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;squareAsync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// In real code, squaring a number should not be async&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To test that a method fails, you can add a success callback that throws an error and an error callback that handles the original error and asserts about it:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;testAjaxFailure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;httpRequestPromise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://cannot parse&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;assertFail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Expected failure but returned &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Bad URL&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;/2015-01-08/comparing-different-languages-promises-frameworks&quot;&gt;&lt;em&gt;Next time: Comparing promises in different languages&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2015-01-04-async-method-patterns.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2015-01-04/async-method-patterns/"/>
		<title type="text">Concurrency, part 2: Patterns for Asynchronous Methods</title>
		<updated>2015-01-04T00:00:00+00:00</updated>
		<published>2015-01-04T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="async" term="async" />
		
		
		<category scheme="https://blog.slaks.net/#" label="promises" term="promises" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		
		
		<category scheme="https://blog.slaks.net/#" label="javascript" term="javascript" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2014-12-23/parallelism-async-threading-explained&quot;&gt;Last time&lt;/a&gt;, I explained the basic concepts of asynchronous and multi-threaded programming.  As I explained there, the most efficient way to run non-CPU-bound operations concurrently is to call them asynchronously.  However, asynchronous code can be confusing and difficult to work with.  This post will explain different techniques for writing asynchronous functions.  The concepts described in this post apply to all languages; the sample code is in Javascript, running in a Node-like environment.&lt;/p&gt;

&lt;h1 id=&quot;the-problem&quot;&gt;The problem&lt;/h1&gt;
&lt;p&gt;An asynchronous method cannot simply return its value like any other method.  Since the result is computed asynchronously, the method will have already returned before the result arrives.  In multi-threaded languages, this issue can be overcome by synchronously waiting for an asynchronous operation to finish.  However, this completely defeats the point of asynchrony, which is to free up the thread while the operation is in progress.  In single-threaded languages like Javascript, this is completely impossible, since the result cannot even arrive while the method is waiting.&lt;/p&gt;

&lt;p&gt;Thus, an asynchronous method must use another approach to return its result.  Asynchrony is viral; any method that calls an asynchronous method must itself become asynchronous.  This effect will propagate up your entire call hierarchy, until the entry-point, which must either wait for the async operation (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Main()&lt;/code&gt; in C# or Java), or can ignore the operation’s result (eg, UI event handlers or network request handlers).&lt;/p&gt;

&lt;h1 id=&quot;continuation-passing-style-callbacks&quot;&gt;Continuation-passing style (callbacks)&lt;/h1&gt;
&lt;p&gt;The most basic approach to dealing with asynchronous operations is to use callbacks.  With this approach, each asynchronous function call takes a callback parameter, which will be called when the operation completes.  However, using callbacks in complex programs can be quite annoying.  Performing multiple operations in sequence requires ugly nested callbacks.  Every callback must explicitly check for and handle errors from the operation, with no equivalent of exceptions and catch blocks to handle control flow in error conditions.  There is no way to store an asynchronously-retrieved value before it is ready.&lt;/p&gt;

&lt;p&gt;In this approach, no function will ever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; a value (the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; statement is only used to exit a function early); instead, all execution flow between functions is handled using callbacks (to signal errors, return values, or just to wait for completion).  This approach is called &lt;a href=&quot;https://en.wikipedia.org/wiki/Continuation-passing_style&quot;&gt;continuation-passing style&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This approach also relies on convention to tell the callback whether an error occurred.  Node.js uses the convention that the callback’s first argument is always the error (or null if the operation succeeded); the result, if any, is passed as the second argument.  Other approaches include passing the callback a status object with a (nullable) error property, or accepting two callbacks; one for success and one for error.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;addHash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;computeHash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;writeFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.hash&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hash written&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// No error&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;promises&quot;&gt;Promises&lt;/h1&gt;
&lt;p&gt;Promises represent a much better way to handle asynchronous operations.  A promise is an object that represents a value that may arrive some time in the future.  To consume the value, you call a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;then()&lt;/code&gt; method and pass a callback, which will run when the value arrives, or immediately if the promise has already been fulfilled.  If this callback returns its own value, you will get a new promise of the resulting value, which you can then add more callbacks to later.  Thus, you can chain asynchronous operations without nesting anything.  If an error occurs, a promise can be resolved to an error state instead of a successful value, which will skip all success callbacks and instead return further rejected promises.  Thus, errors will propagate naturally along a promise chain of asynchronous operations, until they are finally handled by error callbacks later in the chain.  This is directly analogous to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;catch&lt;/code&gt; blocks in imperative programming.  Finally, because promises are regular objects, they can be stored in fields just like any other value.&lt;/p&gt;

&lt;p&gt;Using promises, the earlier example can be simplified to:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;addHash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;qfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;computeHash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;qfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.hash&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hash written&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;/2015-01-05/introducing-promises&quot;&gt;&lt;em&gt;Next time: More about promises&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-12-23-parallelism-async-threading-explained.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-12-23/parallelism-async-threading-explained/"/>
		<title type="text">Concurrency, part 1: Parallelism, Asynchrony, and Multi-threading Explained</title>
		<updated>2014-12-23T00:00:00+00:00</updated>
		<published>2014-12-23T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="async" term="async" />
		
		
		<category scheme="https://blog.slaks.net/#" label="multi-threading" term="multi-threading" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Concurrent programming techniques, such as multi-threading or asynchronous operations, are all the rage nowadays.  As Moore’s law begins to fail, the industry is turning to concurrency to deliver the next generation of performance boosts.  Every hot new development framework makes some kind of claim to run efficient asynchronous or multi-threaded code.  With all of this hype, however, it’s easy to get confused about the exact meanings of  “multi-threading”, “parallelism”, and “asynchrony”, and the difference between them.&lt;/p&gt;

&lt;h1 id=&quot;about-asynchrony&quot;&gt;About asynchrony&lt;/h1&gt;
&lt;p&gt;An asynchronous operation is an operation which continues in the background after being initiated, without forcing the caller to wait for it to finish before running other code.&lt;/p&gt;

&lt;p&gt;When calling a typical, synchronous function, your code calls the function, and does not continue until the function is completely finished.  If the function is CPU-bound (if all it does is compute values, without waiting for replies from anything else), this makes sense.  The CPU is busy computing the result of the function, and doesn’t have time to do anything else (at least, on that thread).&lt;/p&gt;

&lt;p&gt;However, if the function is not CPU-bound – if it simply waits for something else to finish – this makes less sense.  If all the function does is wait for a response from a hard drive or database or web service, or even from a camera or other USB device, there is no need to keep the CPU busy waiting for that response instead of doing useful work.  In that case, your entire program (or thread, in a multi-threaded environment) is stuck waiting for no reason.&lt;/p&gt;

&lt;p&gt;This is why asynchronous operations exist.  Instead of blocking the calling program (or thread) until a response arrives, an asynchronous (also called &lt;em&gt;non-blocking&lt;/em&gt;) implementation will send of a request to the database or web service or whatever, then return immediately, letting your program continue running other code while the remote service sends a reply.  Once the response arrives, the system will run a callback (either on your message loop or in a separate IO completion port thread, depending on the environment), letting your code handle the response.&lt;/p&gt;

&lt;h1 id=&quot;about-multi-threading&quot;&gt;About multi-threading&lt;/h1&gt;
&lt;p&gt;Multi-threading means running more than one thread of execution at a time.  In this model, all operations are still synchronous, but the CPU will execute multiple threads of synchronous operations at the same time.&lt;/p&gt;

&lt;p&gt;Multi-threading makes most sense when calling multiple (and independent) CPU-bound operations, on a multi-core processor.  For example, a program that independently analyzes every pixel in an image could divide the image into one strip for each CPU core, then analyze each strip in its own thread at the same time.&lt;/p&gt;

&lt;p&gt;Note that there is no advantage gained in running more threads than there are CPU cores.  Threads do not magically let CPUs do more work for free; once you run out of dedicated cores to run your threads, you’ll end up with single cores running bits of each thread in turn, adding additional context-switching overhead as the core switches to each thread, and not getting any performance benefit.&lt;/p&gt;

&lt;h1 id=&quot;how-to-best-manage-concurrent--parallel-operations&quot;&gt;How to best manage concurrent / parallel operations&lt;/h1&gt;
&lt;p&gt;Concurrency or parallelism simply mean running more than one operation at the same time.  This can be accomplished using either asynchrony or multi-threading.  However, different kinds of operations lend themselves towards different kinds of concurrency.&lt;/p&gt;

&lt;p&gt;If you have strictly CPU-bound work, multi-threading is your only option.  The whole point of asynchrony is to leave the CPU (and your thread(s)) free, so that you can run other code while waiting for the asynchronous operations to finish.  If your work is CPU-bound, running it asynchronously does not make sense.&lt;/p&gt;

&lt;p&gt;If you have non-CPU-bound work (disk IO, network requests, etc), both options can work.  However, running the operations asynchronously will offer better performance.  Parallelizing non-CPU-bound work using multiple threads means making each request on its own dedicated thread, blocking that thread until the response arrives.  However, threads are not cheap; creating each thread occupies a megabyte or more of memory for the stack and limited kernel resources, and also adds more context-switching overhead.  This places artificial limits on the total number of in-flight requests.&lt;/p&gt;

&lt;p&gt;In contrast, asynchrony offers much better parallelism with non-blocking operations.  You can kick off thousands of non-blocking requests from a single thread with very little per-request overhead, then schedule callbacks to run when each request finishes.  Your original thread is then free to do whatever else it likes (such as actual CPU-bound work, kicking off more async requests, or accepting incoming connections).&lt;/p&gt;

&lt;p&gt;Switching non-CPU-bound operations to non-blocking implementations can offer tremendous performance and scalability benefits, simply because threads are not cheap.  At a previous job, I rewrote a long-running and massively parallel network server to use non-blocking IO, and the throughput improved from 900 connections per server to over 9,000 connections per server.&lt;/p&gt;

&lt;h1 id=&quot;handling-ui-threads&quot;&gt;Handling UI threads&lt;/h1&gt;
&lt;p&gt;All of the discussion so far has been focused on raw performance for server-side or backend scenarios.  However, asynchrony and multi-threading have one other, unrelated use: UI threads.&lt;/p&gt;

&lt;p&gt;While your UI thread is busy (with any kind of operation), it will not accept any UI events, making your application hang and making users unhappy.  Therefore, it is extremely important that all potentially long-running operations, whether CPU-bound or IO-bound, not run on the UI thread.&lt;/p&gt;

&lt;p&gt;For CPU-bound operations, this simply means running them in a background thread; for non-CPU-bound operations, this can either mean running them in a blocking fashion in a background thread, or running them asynchronously from the UI thread.  Unlike server scenarios, scalability usually isn’t a concern for UI applications (there generally aren’t thousands of operations to worry about), so it doesn’t actually matter much which approach you choose.&lt;/p&gt;

&lt;h1 id=&quot;availability-in-programming-languages&quot;&gt;Availability in Programming Languages&lt;/h1&gt;
&lt;p&gt;As mentioned earlier, asynchrony and multi-threading are completely orthogonal concepts, and different languages expose different options.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Javascript is completely single-threaded (with the subtle exception of web workers, which still cannot share memory between threads), but also completely asynchronous.  With very few exceptions (deprecated synchronous &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XmlHttpRequest&lt;/code&gt;s in the browser, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fs.*sync()&lt;/code&gt; in  Node.js), every non-CPU-bound operation in Javascript is only available asynchronously.&lt;br /&gt;
  This is why Node.js is so popular and scalable.  Because everything runs asynchronously, as long as you don’t have any CPU-bound work, you can scale to thousands of concurrent requests with very little overhead (limited only by the memory consumed for each request).&lt;/li&gt;
  &lt;li&gt;Java, by contrast, is fully multi-threaded, with a powerful system of Executors and synchronization primitives, but has extremely little support for asynchronous operations.  Before Java 8, Java had no built-in asynchronous operations, although there are some third-party libraries that offer async network IO.&lt;/li&gt;
  &lt;li&gt;C# is both asynchronous and multi-threaded.  Like Java, it has a rich threading API, but unlike Java, it has had asynchronous IO methods since day 1.  .Net 4.5 introduced simple-to-use asynchronous IO methods using the TPL and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;, replacing the older, more cumbersome APM-based methods (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Begin*()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;End*()&lt;/code&gt;).  However, outside the core .Net framework, many third-party libraries still do not offer asynchronous versions of IO-bound operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;/2015-01-04/async-method-patterns&quot;&gt;&lt;em&gt;Next time: Patterns for Asynchronous Methods&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-11-16-mef2-roslyn-visual-studio-compatibility.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-11-16/mef2-roslyn-visual-studio-compatibility/"/>
		<title type="text">MEF v2, Roslyn, and Visual Studio: An Adventure in Compatibility</title>
		<updated>2014-11-16T00:00:00+00:00</updated>
		<published>2014-11-16T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Roslyn" term="roslyn" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;h1 id=&quot;background&quot;&gt;Background&lt;/h1&gt;
&lt;p&gt;Both Visual Studio and Roslyn use the &lt;a href=&quot;https://mef.codeplex.com/&quot;&gt;Managed Extensibility Framework&lt;/a&gt; (MEF) to build large applications out of decoupled, extensible components.  MEF allows different parts of these programs to talk to each-other using clearly defined interfaces, allowing different subsystems to be developed by different teams on different release cycles without breaking anything.&lt;/p&gt;

&lt;p&gt;MEF is also used for extensibility.  Visual Studio uses MEF to import services like syntax highlighting, IntelliSense providers, and other editor services; Roslyn uses MEF to import refactorings and diagnostics.  This allows you to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Export]&lt;/code&gt; these services in your own extensions, and have Visual Studio or Roslyn automatically import and use them just by loading your DLL.  This is the core of &lt;a href=&quot;/2013-11-10/extending-visual-studio-part-2-core-concepts/#editor-extensions&quot;&gt;modern Visual Studio extensibility&lt;/a&gt;.  Both Roslyn and Visual Studio also use MEF for hundreds of internal services, to help wire up core functionality even without being extensible.  The core compilation engine (including the syntax trees and semantic model) do not use MEF at all, but the workspaces layer uses it for services like caching layers, as well as formatting rules and language-specific APIs to create core concepts like compilations.  The Visual Studio &amp;amp; editor integration layers use hundreds of their own MEF services to wire up their own implementations.&lt;/p&gt;

&lt;h1 id=&quot;the-problem&quot;&gt;The Problem&lt;/h1&gt;
&lt;p&gt;The core parts of Roslyn (the compilers &amp;amp; syntax / semantic APIs, and the basic workspace services) were recently changed to be portable assemblies, allowing them to be used in Windows Store apps as well.  The original version of MEF (System.ComponentModel.Composition.dll) is not a portable assembly, so the Roslyn team switched to MEF v2 instead.  MEF v2 is released as a NuGet package named &lt;a href=&quot;https://www.nuget.org/packages/Microsoft.Composition&quot;&gt;Microsoft.Composition&lt;/a&gt;, which contains portable (PCL) assemblies that solve all of these problems.  You can see more details about the change in &lt;a href=&quot;https://roslyn.codeplex.com/SourceControl/changeset/e76a29a4&quot;&gt;this commit&lt;/a&gt;  (this commit &lt;a href=&quot;https://twitter.com/jasonmalinowski/status/533995505186271233&quot;&gt;broke Roslyn’s CodePlex sync tool&lt;/a&gt;, so you can only see the files changed on &lt;a href=&quot;https://github.com/mono/roslyn/commit/e76a29a4&quot;&gt;GitHub&lt;/a&gt; or a local clone).&lt;/p&gt;

&lt;p&gt;However, Visual Studio itself cannot move away from MEF v1, because that would break every existing extension that exports any service using the MEF v1 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Export]&lt;/code&gt; attribute.  Roslyn’s Visual Studio integration (especially editor services like syntax highlighting) need to export VS editor services (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IClassifierProvider&lt;/code&gt;) to Visual Studio’s MEF host, while at the same time importing Roslyn workspace services which are exported using MEFv2, and never the twain shall meet.&lt;/p&gt;

&lt;h1 id=&quot;the-solution&quot;&gt;The Solution&lt;/h1&gt;
&lt;p&gt;The Visual Studio 2015 Preview is the first public VS build to suffer from the problem (Dev14 CTP 4 was built before Roslyn switched to MEFv2).  To fix it, Visual Studio has its own internal version of MEF (internally referred to as MEFv3 or usually VsMEF), contained in Microsoft.VisualStudio.Composition.dll.  This version of MEF scans assemblies for both MEFv1 exports and MEFv2 exports, loading them all into a single unified container.&lt;/p&gt;

&lt;p&gt;This solves all of the problems.  This new MEF container picks up MEFv1-exported types from existing extensions and from the rest of Visual Studio, and can seamlessly mix them with MEFv2-exported types from Roslyn.  As long as you &lt;a href=&quot;http://source.roslyn.codeplex.com/#Roslyn.Diagnostics.Analyzers/Reliability/MixedVersionsOfMefAttributesAnalyzer.cs&quot;&gt;don’t mix attributes from different MEF versions&lt;/a&gt; in the same class, everything will just work.  This change is so transparent that extension developers won’t even notice it unless they go out and look for it; I only discovered it because it completely broke my out-of-process VS MEF host in &lt;a href=&quot;https://github.com/SLaks/VSEmbed&quot;&gt;VSEmbed&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This also means that you can use MEFv2 instead of MEFv1 in VS2015 extensions.  Note that older versions of Visual Studio will completely ignore MEFv2 attributes.  You can utilize this to create parts that will only be imported by VS2015, by using the MEFv2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Export]&lt;/code&gt; attribute on only those parts in a backwards-compatible extension.  Note, however, that you can’t use any new types or assemblies in class members, or older versions will fail to load them as MEFv1 tries to scan every type for its attributes.&lt;/p&gt;

&lt;p&gt;VS MEF also has other benefits, including &lt;a href=&quot;https://twitter.com/aarnott/status/534035831539785730&quot;&gt;faster loading&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/aarnott/status/534037832721911808&quot;&gt;better error messages&lt;/a&gt; when things go wrong.&lt;/p&gt;

&lt;p&gt;VS MEF will &lt;a href=&quot;https://twitter.com/aarnott/status/534036403022077953&quot;&gt;hopefully&lt;/a&gt; be published standalone on NuGet some time in the future.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-09-04-getting-started-with-visual-studio-themes.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-09-04/getting-started-with-visual-studio-themes/"/>
		<title type="text">Getting Started with the Visual Studio Theming Architecture</title>
		<updated>2014-09-04T00:00:00+00:00</updated>
		<published>2014-09-04T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Visual Studio 2010 rewrote the entire shell UI – the MDI tabs &amp;amp; tool windows, the toolbars, and the menus – in WPF.  This let it use WPF’s powerful resources / theming system to style all of the UI elements, which Microsoft took advantage of in VS 2012 to add multiple themes (Light, Dark, and, later, Blue).  This blog post will explore how this system is implemented.  Later blog posts will explain how to use VS theme colors &amp;amp; controls in your own VS extensions, and, later, how to use VS theming directly in your own standalone application (which will require an installed copy of VS to run).&lt;/p&gt;

&lt;p&gt;The theming system is built on WPF ResourceDictionaries, which contain reusable global resources such as colors, brushes, and control styles, keyed by strings or objects.  Visual Studio 2010 had a table of 463 theme colors in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.VsColors&lt;/code&gt; class (and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VsBrushes&lt;/code&gt; that return brushes instead of colors), containing every color that used by VS2010’s new UI theme.  Visual Studio 2012 then greatly expanded the theming system, theming the window chrome and introducing multiple color themes.  To power this, it replaced this table of theme colors with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.ThemeResourceKey&lt;/code&gt;, which contains a category and an entry name, and indicates whether it’s specifying the foreground or background color for that entry, and whether it refers to a color or a brush.  For compatibility, the older VsColors &amp;amp; VsBrushes still exist in later versions of VS as well, bound to the same colors as the corresponding ThemeResourceKeys in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EnvironmentColors&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;ThemeResourceKeys are broken down by category and contained by a number of classes (mostly) in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.PlatformUI&lt;/code&gt;, including &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EnvironmentColors&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommonControlsColors&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BackstageColors&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TreeViewColors&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StartPageColors&lt;/code&gt;, and more (some of these were introduced in VS2013 or Dev14).  For a complete list, open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.XX.0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.UI.Internal&lt;/code&gt; (beware that this is unversioned, so it’s trickier to use in cross-version extensions) in your favorite decompiler, then search for usages of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThemeResourceKey&lt;/code&gt; (which can be found in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.Immutable.11.0&lt;/code&gt;).  Note that ThemeResourceKeys are compared by value, so you can use keys from later VS versions without referencing their DLLs by copying the key declaration into your own code.  Obviously, such keys will not have any colors in earlier VS versions, but this lets you use them without causing compilation issues (as long as you check the runtime VS version before using them).  Note also that any keys declared in unversioned DLLs are not guaranteed to exist in future VS versions either.&lt;/p&gt;

&lt;p&gt;These keys (both the strings from VsColors &amp;amp; VsBrushes and the ThemeResourceKeys from later versions) are added to a global &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResourceDictionary&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.Windows.Application.Current&lt;/code&gt; by the internal &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResourceSynchronizer&lt;/code&gt; class when VS is launched, allowing them to be referenced in any WPF control anywhere in Visual Studio (in VS2012 and later, the same class will also repopulate this ResourceDictionary whenever you change the current theme).  This lets you easily use theme colors in your own extensions.&lt;/p&gt;

&lt;p&gt;To use theme colors in your own VS extension, you simply need to add a reference to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.XX.0&lt;/code&gt; (from the minimum VS version that you want to support) and declare it in your XAML.  You can then use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{DynamicResource}&lt;/code&gt;s with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{x:Static}&lt;/code&gt; declarations to use the keys, and they will pick up the correct theme colors at runtime.  For example:&lt;/p&gt;

&lt;div class=&quot;xaml&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;Control&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;x:class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;YourAddin.YourControl&quot;&lt;/span&gt;
		 &lt;span class=&quot;na&quot;&gt;xmlns=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;&lt;/span&gt;
		 &lt;span class=&quot;na&quot;&gt;xmlns:x=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;&lt;/span&gt;
		 &lt;span class=&quot;na&quot;&gt;xmlns:shell=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.10.0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;Border&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;x:Name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;NotificationWindow&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Loaded=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;NotificationWindow_Loaded&quot;&lt;/span&gt;
			&lt;span class=&quot;na&quot;&gt;Background=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{DynamicResource {x:Static shell:VsBrushes.CommandBarOptionsBackgroundKey}}&quot;&lt;/span&gt;
			&lt;span class=&quot;na&quot;&gt;TextElement.Foreground=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{DynamicResource {x:Static shell:VsBrushes.CommandBarTextActiveKey}}&quot;&lt;/span&gt;
			&lt;span class=&quot;na&quot;&gt;BorderBrush=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{DynamicResource {x:Static shell:VsBrushes.DropDownBorderKey}}&quot;&lt;/span&gt;
			&lt;span class=&quot;na&quot;&gt;BorderThickness=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
		...
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Border&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Control&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you aren’t targetting VS2010, you can also use other resources such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommonControlsColors&lt;/code&gt; inside the  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.PlatformUI&lt;/code&gt; namespace.  If you need colors rather than brushes (for example, if you’re constructing a gradient), use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VsColors&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VsBrushes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although this will work correctly at runtime within Visual Studio, you won’t see any colors in the XAML designer, since it doesn’t load any VS themes.  In part three of this series, I’ll explain how to load a VS theme into the XAML designer yourself, so you can make sure your colors match without having to build &amp;amp; run your extension after every change.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next time: Escaping the Box: Using VS themes outside Visual Studio&lt;/em&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-08-26-adding-menu-items-to-a-visual-studio-extension.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-08-26/adding-menu-items-to-a-visual-studio-extension/"/>
		<title type="text">How to add a menu item to a Visual Studio extension</title>
		<updated>2014-08-26T00:00:00+00:00</updated>
		<published>2014-08-26T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;As I’ve described &lt;a href=&quot;/2013-11-10/extending-visual-studio-part-2-core-concepts/&quot;&gt;earlier&lt;/a&gt;, when creating a Visual Studio extension, you are given a choice between the older VsPackage-style extension (using a wizard from the New Project dialog) and the newer, MEF-based extensions.&lt;/p&gt;

&lt;p&gt;Newer parts of VS, such as the editor, the Web Tools, or Roslyn, are built using MEF, so an extension simply needs to export MEF services for these components to import &amp;amp; run.  These MEF-based extensions are much simpler and easier to work with.  However, if you create an MEF-based extension (eg, from a Roslyn or editor extension template), there is no obvious way to add a menu command.  This blog post explains how to do that.&lt;/p&gt;

&lt;p&gt;You can follow along in your own extension, or you can see all of these changes in &lt;a href=&quot;https://github.com/SLaks/Ref12/commit/6ce75a4d7f4bfde5c3c51073ebb9db97baa67f42&quot;&gt;this commit&lt;/a&gt; in Ref12.&lt;/p&gt;

&lt;h1 id=&quot;1-add-a-menu-resource-file&quot;&gt;1. Add a menu resource file&lt;/h1&gt;
&lt;p&gt;Visual Studio menu commands are compiled from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vsct&lt;/code&gt; files, which are compiled by the VSCT compiler into embedded &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ctmenu&lt;/code&gt; resources within the DLL.  To get started, you need to add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vsct&lt;/code&gt; file (the name doesn’t matter) to your project as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;VSCTCompile&amp;gt;&lt;/code&gt; tag.  This must be done by editing the project file directly.  You’ll also need to add an empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSPackage.resx&lt;/code&gt; file for the VSCT compiler to embed the compiled resource into&lt;/p&gt;

&lt;p&gt;The project file modifications (add these into any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;ItemGroup&amp;gt;&lt;/code&gt;) are as follows:&lt;/p&gt;
&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;	&lt;span class=&quot;nt&quot;&gt;&amp;lt;VSCTCompile&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Include=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;YourName.vsct&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;ResourceName&amp;gt;&lt;/span&gt;Menus.ctmenu&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ResourceName&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/VSCTCompile&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;EmbeddedResource&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Include=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;VSPackage.resx&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;MergeWithCTO&amp;gt;&lt;/span&gt;true&lt;span class=&quot;nt&quot;&gt;&amp;lt;/MergeWithCTO&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;ManifestResourceName&amp;gt;&lt;/span&gt;VSPackage&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ManifestResourceName&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/EmbeddedResource&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The contents of the VSCT should look like this:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;CommandTable&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:xs=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://www.w3.org/2001/XMLSchema&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;Extern&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;stdidcmd.h&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;Extern&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;vsshlids.h&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;Commands&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;package=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PackageGuid&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;Buttons&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;Button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;guid=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CommandsGuid&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;MyCommand&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;priority=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0x0101&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Button&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;Parent&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;guid=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;guidSHLMainMenu&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;IDG_VS_CODEWIN_NAVIGATETOLOCATION&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;Icon&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;guid=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;guidObjectBrowserButtons&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;15&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

			&lt;span class=&quot;nt&quot;&gt;&amp;lt;Strings&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;ButtonText&amp;gt;&lt;/span&gt;My &lt;span class=&quot;ni&quot;&gt;&amp;amp;amp;&lt;/span&gt;Powerful Command&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ButtonText&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Strings&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Button&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Buttons&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Commands&amp;gt;&lt;/span&gt;

	&lt;span class=&quot;nt&quot;&gt;&amp;lt;Symbols&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;GuidSymbol&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PackageGuid&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;«Package GUID»&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

		&lt;span class=&quot;nt&quot;&gt;&amp;lt;GuidSymbol&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CommandsGuid&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;«Command Group GUID»&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;IDSymbol&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;MyCommand&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;/GuidSymbol&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Symbols&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/CommandTable&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The command ID values must be unique within each command group (GUID), so you don’t need to worry about colliding with other commands.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Parent&amp;gt;&lt;/code&gt; entry specifies the group that the command should be placed in; you can find all standard groups in Program Files\Microsoft Visual Studio 12.0\VSSDK\VisualStudioIntegration\Common\Inc\vsshlids.h.  You can also find a list of all command IDs in stdidcmd.h in the same folder.&lt;/p&gt;

&lt;h1 id=&quot;2-define-a-vspackage-for-visual-studio-to-load&quot;&gt;2. Define a VSPackage for Visual Studio to load&lt;/h1&gt;
&lt;p&gt;To make Visual Studio load your menu command file, you need to define a VSPackage within your addin and register it in the VSIX.&lt;/p&gt;

&lt;p&gt;First, add the following code file (the class name does not matter):&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Runtime.InteropServices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Microsoft.VisualStudio.Shell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;YourAddin.Namespace&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// These GUIDS and command IDs must match the VSCT.&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ProvideMenuResource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Menus.ctmenu&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Guid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;«Package GUID»&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;PackageRegistration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UseManagedResourcesOnly&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YourAddinPackage&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Package&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Guid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;«Command Group GUID»&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;YourAddinCommand&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;MyCommand&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The attributes in the package class tell CreatePkgDef.exe (which is invoked by the VSIX compiler) how to emit a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pkgdef&lt;/code&gt; file in your addin that sets he appropriate registry keys to register the addin and its menu resource with the Visual Studio’s native COM-based addin system.  This is completely independent of MEF registration for managed addins.&lt;/p&gt;

&lt;p&gt;The enum and its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Guid]&lt;/code&gt; attribute are simply a convenient way of keeping track of the GUID and command IDs; you’re free to use a different approach if you like.&lt;/p&gt;

&lt;p&gt;You also need to tell the VSIX to include the package and generate the pkgdef file by adding the following line to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Content&amp;gt;&lt;/code&gt; element in your source.extension.vsixmanifest:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;		&lt;span class=&quot;nt&quot;&gt;&amp;lt;VsPackage&amp;gt;&lt;/span&gt;|%CurrentProject%;PkgdefProjectOutputGroup|&lt;span class=&quot;nt&quot;&gt;&amp;lt;/VsPackage&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you’re using the designer (for version 2 VSIXes), go to the Assets section, click New, select &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.VsPackage&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A project in the current solution&lt;/code&gt;, and select your project.&lt;/p&gt;

&lt;h1 id=&quot;3-fix-the-project-configuration&quot;&gt;3. Fix the project configuration&lt;/h1&gt;
&lt;p&gt;Finally, you need to make a few changes to the csproj file to correctly register the addin:&lt;/p&gt;

&lt;p&gt;First, delete &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;GeneratePkgDefFile&amp;gt;false&amp;lt;/GeneratePkgDefFile&amp;gt;&lt;/code&gt; if it’s present to tell the VSIX compiler to generate a pkgdef to register your package.  Purely MEF-based addins don’t need pkgdef files at all, so most project templates will add that, but once you create a VSPackage, you need a pkgdef to register it with Visual Studio’s native COM addin loader.&lt;/p&gt;

&lt;p&gt;Next, add the following markup anywhere in the root element (not in an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;ItemGroup&amp;gt;&lt;/code&gt;):&lt;/p&gt;
&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;	&lt;span class=&quot;nt&quot;&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;UseCodebase&amp;gt;&lt;/span&gt;true&lt;span class=&quot;nt&quot;&gt;&amp;lt;/UseCodebase&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;VSPackages are actually COM DLLs that are registered in the Visual Studio hive’s config key and loaded using normal COM practices.  Managed VSPackages are loaded using COM interop; they are registered as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mscoree.dll&lt;/code&gt;, which then looks up a managed assembly to load from the same registry key.  By default, the PkgDef creator will emit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Assembly&quot;=&quot;YourAssemblyName&quot;&lt;/code&gt;, which mscoree will try to load using the standard .Net assembly loader.  Unless your VSPackage DLL is in the GAC (which it won’t be), this won’t work, and VS will silently refuse to load your package.  Setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;UseCodebase&amp;gt;&lt;/code&gt; in the project file will make it emit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;CodeBase&quot;=&quot;$PackageFolder$\YourAddin.dll&quot;&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PackageFolder$&lt;/code&gt; is substituted for the actual DLL path when the VSIX is installed), which tells mscoree the exact path to load it from.&lt;/p&gt;

&lt;h1 id=&quot;4-implement-the-command&quot;&gt;4. Implement the command&lt;/h1&gt;
&lt;p&gt;After doing all this, your command should show up when you hit F5 in the addin (running in the Experimental instance).  However, you still need to actually implement the command so that something will happen when the button is clicked.&lt;/p&gt;

&lt;p&gt;For global commands, the simplest way to do this is to override the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Initialize()&lt;/code&gt; method in your package and add code like the following:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mcs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OleMenuCommandService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IMenuCommandService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commandId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CommandID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;YourAddinCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GUID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;YourAddinCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mcs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MenuCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;MessageBox&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;You clicked me!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commandId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For commands that apply to the active editor window, I wrote a class which adds an interceptor to the editor’s command handler chain, and exposes a simple interface that accepts an entry in a command enum.  This can be used both to implement new commands and to intercept existing ones.
You can find it at &lt;a href=&quot;https://github.com/SLaks/Ref12/blob/master/Ref12/Commands/CommandTargetBase.cs&quot;&gt;CommandTargetBase.cs&lt;/a&gt;, and you can see sample usage &lt;a href=&quot;https://github.com/SLaks/Ref12/blob/master/Ref12/Commands/TextViewListener.cs&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-05-28-exploring-roslyn-part-3-breaking-changes.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-05-28/exploring-roslyn-part-3-breaking-changes/"/>
		<title type="text">Exploring Roslyn, part 3: Breaking Changes</title>
		<updated>2014-05-28T00:00:00+00:00</updated>
		<published>2014-05-28T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Roslyn" term="roslyn" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2014-05-21/exploring-roslyn-part-2-inside-end-user-preview/&quot;&gt;Last time&lt;/a&gt;, I talked about what the Roslyn End-User Preview does and how it works.  This time, I’ll talk about some subtle breaking changes in the compiler.&lt;/p&gt;

&lt;p&gt;The C# &amp;amp; VB teams have a very high bar for backwards compatibility; they try extremely hard to make sure that all of your existing code will compile identically in Roslyn.  However, in any project as large as a compiler, there are bound to be some changes in the way compilation works.  Some of these are flaws in the old compiler, for which Roslyn’s fixes can subtly break existing code; some of these are side-effects of the way the new pipeline works which were deemed not worth fixing.  All of these changes are in extremely subtle corner cases, and should not affect most normal codebases.&lt;/p&gt;

&lt;p&gt;Breaking changes come in two varieties: Forward-breaking changes, meaning that code that worked pre-Roslyn doesn’t work with Roslyn, and backward-breaking changes, meaning that code that works with the Roslyn compiler doesn’t work with the old compiler (these can cause problems with open source projects if different people use different VS versions).&lt;/p&gt;

&lt;p&gt;Obviously, all of the new language features in C# 6 are backwards-breaking changes; they will never work with older compilers.  However, there are some more subtle changes which can prevent projects from being compiled without Roslyn even if they don’t use any new features.&lt;/p&gt;

&lt;h1 id=&quot;lifting-variables-in-iterator-methods&quot;&gt;Lifting variables in iterator methods&lt;/h1&gt;
&lt;p&gt;When you write an iterator method (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yield return&lt;/code&gt;), the compiler will transform your method into a class which implements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;, turning your actual code into a state machine inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MoveNext()&lt;/code&gt;.  (See &lt;a href=&quot;https://csharpindepth.com/articles/chapter6/iteratorblockimplementation.aspx&quot;&gt;Jon Skeet’s blog post&lt;/a&gt; for a thorough explanation.)&lt;/p&gt;

&lt;p&gt;Local variables in your method become fields in the iterator class, so they can be persisted across calls to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MoveNext()&lt;/code&gt;.  The old compiler would always lift every single local variable to a field, even if it’s never used across a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yield return&lt;/code&gt; boundary (meaning that it doesn’t need to stick around between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MoveNext()&lt;/code&gt; calls).  The Roslyn compiler is smarter, and only lifts locals when it needs to (if the local is used both before and after a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yield return&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;This is a breaking change in both directions.  If your pre-Roslyn code declares something like a timer or file handle in an iterator (and doesn’t use it later), which it expects to survive until the iterator itself is collected, compiling with Roslyn will allow it to be GC’d much earlier that it would have been with the old compiler.  To fix this, simply add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GC.KeepAlive()&lt;/code&gt; call at the bottom of the iterator.  Since it is now used on both sides of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yield return&lt;/code&gt;, the compiler will be forced to lift it into a field in the iterator.&lt;/p&gt;

&lt;p&gt;This can also be a backwards-breaking change, for two reasons.  Most obviously, if your Roslyn code uses a very large object in only one place in an iterator, compiling it with the native compiler will cause that object to last much longer; potentially leaking large amounts of memory if your iterators are long-lived (which can happen in surprising ways with LINQ queries).&lt;/p&gt;

&lt;p&gt;More subtly, this means that code compiled with the native compiler can load types earlier than it would have with Roslyn.  This &lt;a href=&quot;https://github.com/SLaks/Ref12/commit/2c52df7be397c052980bf91d2bcc6a90eae9c926&quot;&gt;bit me&lt;/a&gt; in &lt;a href=&quot;https://visualstudiogallery.msdn.microsoft.com/f89b27c5-7d7b-4059-adde-7ccc709fa86e&quot;&gt;Ref12&lt;/a&gt;, where I had an iterator that used a type in an &lt;a href=&quot;/2014-02-26/extending-visual-studio-part-5-dealing-with-unversioned-assemblies&quot;&gt;unversioned assembly&lt;/a&gt; that I loaded using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AssemblyResolve&lt;/code&gt; handler.  With Roslyn, it worked fine.  Without Roslyn, that type ended up in a field in the iterator class, so MEF tried to load the type when inspecting the assembly (through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Assembly.GetTypes()&lt;/code&gt;), and the extension refused to load.&lt;/p&gt;

&lt;h2 id=&quot;demo&quot;&gt;Demo&lt;/h2&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MoveNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;GC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;GC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WaitForPendingFinalizers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	
	&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MoveNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;GC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;GC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WaitForPendingFinalizers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Alerter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Prevent the optimizer from removing the variable entirely&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Before return&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;After return&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;	
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Alerter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Alerter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Destroying!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When compiled with the native compiler, this code will print&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Before return&lt;br /&gt;
After return&lt;br /&gt;
Destroying!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Alerter&lt;/code&gt; instance is stored as a field in the iterator, it cannot be collected until the iterator is finished.&lt;/p&gt;

&lt;p&gt;With Roslyn, it prints&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Before return&lt;br /&gt;
Destroying!&lt;br /&gt;
After return&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since the variable is not referenced again after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yield return&lt;/code&gt;, Roslyn compiles it to a local variable within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MoveNext()&lt;/code&gt;, allowing it to be GC’d while the iterator is still alive.&lt;/p&gt;

&lt;h1 id=&quot;loading-indirectly-referenced-assemblies-during-compilation&quot;&gt;Loading indirectly referenced assemblies during compilation&lt;/h1&gt;
&lt;p&gt;This is a backwards-breaking change only; it will not break any code that already works with the pre-Roslyn compiler.&lt;/p&gt;

&lt;p&gt;When compiling code that uses types from a different assembly, the compiler needs to load that assembly to look up the metadata for those types.  It will also load assemblies for types that are related to methods you call directly, such as base types, or parameter types in other overloads.&lt;/p&gt;

&lt;p&gt;The native compiler took a heavy-handed approach; it always tried to load every base type and interface for any type that you use.  The Roslyn compiler is more frugal; it will only load base types and interfaces if it needs to.&lt;/p&gt;

&lt;p&gt;This means that projects created with Roslyn can easily be missing references that will prevent them from compiling with the native compiler.  In the worst case, there are types like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExtensionManagerService&lt;/code&gt; (in Visual Studio) that implement COM interop interfaces in private PIAs (Microsoft.Internal.VisualStudio.Shell.Interop.11.0.DesignTime.dll) that are not available outside Microsoft.  With Roslyn, you won’t even notice these; the IDE does not show them, and the compiler completely ignores them.  The native compiler, on the other hand, will completely refuse to compile code that uses these types, since it can’t load those base interfaces.  This bit me &lt;a href=&quot;https://github.com/SLaks/Root-VSIX/issues/1&quot;&gt;here&lt;/a&gt;.  (The solution is to create your own PIA assembly from the embedded interop types in ILSpy.)&lt;/p&gt;

&lt;h1 id=&quot;overload-resolution-with-conflicting-ambiguities&quot;&gt;Overload resolution with conflicting ambiguities&lt;/h1&gt;
&lt;p&gt;C# 6.0 has some subtle changes in the behavior of overload resolution with optional parameters.  Before Roslyn, if you had a method call that was ambiguous between two overloads, but one of those overloads had additional optional parameters, whereas the other didn’t, the native compiler would choose the first overload.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'c'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Prints 1, not 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Roslyn, this will no longer compile; the compiler will instead complain that it can’t figure out which overload to pick.  You can fix this by explicitly casting the initial parameter to the type used in either overload.&lt;/p&gt;

&lt;p&gt;Overload resolution is extremely complicated, but Roslyn’s behavior appears to be correct according to the spec (§7.5.3.2; emphasis added):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;For the purposes of determining the better function member, a stripped-down argument list A is constructed containing just the argument expressions themselves in the order they appear in the original argument list.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Parameter lists for each of the candidate function members are constructed in the following way:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;The expanded form is used if the function member was applicable only in the expanded form.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Optional parameters with no corresponding arguments are removed from the parameter list&lt;/strong&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;After removing omitted optional parameters, it is clear that neither of these overloads is better, so the ambiguity error is correct.&lt;/p&gt;

&lt;h1 id=&quot;other-bugfixes&quot;&gt;Other bugfixes&lt;/h1&gt;
&lt;p&gt;The Roslyn compiler also fixes a number of minor bugs in the old compiler, rejecting code that was incorrectly accepted by the native compiler (such as the overload resolution example above), or accepting code that the old compiler incorrectly reported an error for.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Roslyn will show a warning when comparing a non-null value type to null (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new DateTime() == null&lt;/code&gt;).&lt;br /&gt;
The native compiler missed this warning for non-primitives  (even though the optimizer collapsed it to a constant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Roslyn does not allow &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;as&lt;/code&gt; to be used with static types (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new object() is BitConverter&lt;/code&gt;).&lt;br /&gt;
The native compiler compiled this fine (it always evaluated to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; at runtime).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Roslyn allows structs to contain themselves as static nullable fields (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;struct MyStruct { static MyStruct? Value; }&lt;/code&gt;). 
The native compiler incorrectly complained about a cycle in the struct layout.&lt;br /&gt;
Note that there is currently a &lt;a href=&quot;https://roslyn.codeplex.com/workitem/185&quot;&gt;bug&lt;/a&gt; in Roslyn that makes this produce invalid IL.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Roslyn rejects parentheses around named parameters (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Console.WriteLine((value: 42))&lt;/code&gt;).&lt;br /&gt;
The native compiler incorrectly allowed this invalid syntax.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Roslyn &lt;a href=&quot;https://twitter.com/vreshetnikov/status/428259020437069824&quot;&gt;disallows&lt;/a&gt; method groups in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;as&lt;/code&gt; operators (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;.Any is byte&lt;/code&gt;).&lt;br /&gt;
The native compiler allowed this (although it did show a warning), and in fact didn’t even check extension method compatibility, allowing crazy things like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.Any is string&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDisposable.Dispose is object&lt;/code&gt;.  Interestingly, the spec explicitly allowed this (§7.10.10); the Roslyn team felt that that was not a good idea.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-05-21-exploring-roslyn-part-2-inside-end-user-preview.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-05-21/exploring-roslyn-part-2-inside-end-user-preview/"/>
		<title type="text">Exploring Roslyn, part 2: Inside the End-User Preview</title>
		<updated>2014-05-21T00:00:00+00:00</updated>
		<published>2014-05-21T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Roslyn" term="roslyn" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2014-04-07/exploring-roslyn-part-1-introduction/&quot;&gt;Last time&lt;/a&gt;, I described the basics of the Roslyn compiler platform, including how the different layers are built and interact with each-other.  In this post, I’ll talk about what the End-User Preview does and how it works.&lt;/p&gt;

&lt;p&gt;The Roslyn End-User Preview provides the first peek at the new Visual Studio managed editing &amp;amp; debugging experience, powered by Roslyn.  The preview completely replaces all of the native language services (the old DLLs will not be loaded at all) with the new Roslyn-powered versions.  These new DLLs are written entirely in C# and VB (except for a small C++ layer, used to hack around COM interop limitations when talking to other parts of VS).&lt;/p&gt;

&lt;p&gt;As you can imagine, replacing the native language services is a gargantuan task, especially since the team is maintaining its usual commitment of not breaking muscle memory.  All of the editing behaviors you’re used to, including snippets, IntelliSense preselection &amp;amp; commit patterns, and more, work as they did before, so your accustomed patterns of keyboard shortcuts will continue to work.  To make sure this is done perfectly, the Roslyn team wants your feedback.  Take out some time, &lt;a href=&quot;https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=52793&quot;&gt;download the preview&lt;/a&gt;, and enjoy the enhanced editor experience.  If you notice anything that doesn’t behave the way it used to, take out a but of time and &lt;a href=&quot;https://connect.microsoft.com/VisualStudio/feedback/CreateFeedbackForm.aspx?FeedbackFormConfigurationID=5494&amp;amp;FeedbackType=3&quot;&gt;file a bug&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;whats-in-the-preview&quot;&gt;What’s in the preview?&lt;/h1&gt;
&lt;p&gt;The preview has two components: the new Roslyn compilers, which replace the native compilers when building your projects, and the language services, which power the editor &amp;amp; debugger within Visual Studio.  Although these components sounds unrelated, they will not work in isolation; the debugger (in particular) makes very specific assumptions about the IL generated by the compiler, and won’t work properly with the wrong version of the compiler.&lt;/p&gt;

&lt;h2 id=&quot;compilers&quot;&gt;Compilers&lt;/h2&gt;
&lt;p&gt;The compiler preview is not hosted by Visual Studio.  Instead, the preview extension will edit the per-user MSBuild targets file (see &lt;a href=&quot;http://source.roslyn.codeplex.com/#CompilerPackage/CompilerPackage.cs&quot;&gt;source&lt;/a&gt;) and replace the definition of the native Csc and Vbc tasks with the new Roslyn-based versions (&lt;a href=&quot;http://source.roslyn.codeplex.com/#Roslyn.Compilers.BuildTasks/Csc.cs&quot;&gt;Csc&lt;/a&gt; and &lt;a href=&quot;http://source.roslyn.codeplex.com/#Roslyn.Compilers.BuildTasks/Vbc.cs&quot;&gt;Vbc&lt;/a&gt;).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This means that as soon as the Roslyn Preview package is loaded (when you create or open a C# or VB project), in any Visual Studio hive, the Roslyn compilers will become active for &lt;em&gt;all&lt;/em&gt; MSBuild v12 builds.  If you want to build anything using the native compilers, you can either use an earlier version of MSBuild (eg, the VS 2012 command prompt), or set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DisableRoslyn&lt;/code&gt; environment variable to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;.  To check whether you’re running the Roslyn compiler, look for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBCSCompiler.exe&lt;/code&gt; in Task Manager.&lt;br /&gt;
The command-line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vbc.exe&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;csc.exe&lt;/code&gt; are not affected.  (since this just modifies the MSBuild task definition)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The compiler preview uses an interesting approach to maximize performance.  Because it’s implemented as a per-user VS extension, the compiler assemblies cannot be NGen’d (since that requires that the assemblies be in the GAC, which needs admin privileges and is machine-wide).  Therefore, every time you start the compiler, the JITter must re-compile all of the compiler’s IL to machine code.  Since the compilers are enormously complicated, that would be a noticeable performance hit.  To avoid this delay, the actual compiler code runs in a shared compilation server process called &lt;a href=&quot;http://source.roslyn.codeplex.com/#VBCSCompiler&quot;&gt;VBCSCompiler&lt;/a&gt;.  When you first compile something (whether directly rcsc and rvbc command-line compilers, or whether through the MSBuild tasks), it will launch this compilation server (thus JITting all of the compilation code only once), then communicate over a named pipe (&lt;a href=&quot;http://source.roslyn.codeplex.com/#VBCSCompiler/BuildProtocol.cs&quot;&gt;source&lt;/a&gt;) to ask it to compile your project.  The server will then sit in  the background until you next compile something, which it will be able to do instantly.  Therefore, you may notice a delay the first time you compile something after rebooting your machine.&lt;/p&gt;

&lt;p&gt;This hack is only necessary because the preview is a per-user install.  The next release of Visual Studio will include the Roslyn-powered compilers as part of the machine-wide install of MSBuild v14, which will be properly NGen’d and won’t need to do any of this.&lt;/p&gt;

&lt;h2 id=&quot;language-services&quot;&gt;Language services&lt;/h2&gt;
&lt;p&gt;The second part of the preview is the new language services.  This part replaces the native language services packages with new Roslyn-powered versions.  Since this lives entirely within Visual Studio, the preview doesn’t need any ugly hacks to register itself.  The only difference between the preview package and the built-in Roslyn-based language services that will be released with Dev14 is that the preview package also disables the VS package for the native language services (in the VS registry hive).  Unlike the compilers, the language service has no effect outside the hive it was installed in.&lt;/p&gt;

&lt;p&gt;The Roslyn-based language services faithfully reproduce the complete development experience from the native language services (except a couple of features that have not yet been implemented), as well as a host of new features that were made possible by the newly well-organized codebase.  These new editor features fall into a couple of categories:&lt;/p&gt;

&lt;h2 id=&quot;syntax-highlighting&quot;&gt;Syntax Highlighting&lt;/h2&gt;
&lt;p&gt;Roslyn extends the editor’s existing syntax highlighting to a number of new areas.  Tooltips for types and method signatures now syntax-highlight the declarations, including keywords and separate colors for User Types (classes, structs, etc), if configured in Fonts &amp;amp; Colors options (the &lt;a href=&quot;https://visualstudiogallery.msdn.microsoft.com/dbcb8670-889e-4a54-a226-a48a15e4cace&quot;&gt;Productivity Power Tools&lt;/a&gt; extension also has this feature, but Roslyn’s works better).  It will even show syntax highlighting for members referenced in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;see /&amp;gt;&lt;/code&gt; tags in the doc comments!&lt;/p&gt;

&lt;p&gt;Tooltips for collapsed methods or regions now show a fully syntax-highlighted preview of the code in the block.&lt;/p&gt;

&lt;p&gt;References to members in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;see&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;param&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;typeparam&amp;gt;&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;exception&amp;gt;&lt;/code&gt; when editing XML doc comments tags are now syntax highlighted, and participate in identifier highlighting.&lt;/p&gt;

&lt;p&gt;The language service will also identify unnecessary qualifiers in your code (already-imported namespaces, inferable generic parameters, unnecessary casts, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this.&lt;/code&gt;, etc) and dim them in the editor to show that they can be removed.  (there is also a refactoring to automatically remove them)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2014/roslyn-syntax-highlighting.png&quot; alt=&quot;Roslyn Syntax Highlighting&quot; style=&quot;max-width:100%;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;intellisense&quot;&gt;IntelliSense&lt;/h2&gt;
&lt;p&gt;Roslyn also features a vastly improved IntelliSense engine.  The editor now knows exactly what keywords can appear in every context, giving you accurate IntelliSense for areas like property declaration blocks, class modifier keywords, preprocessor directives, and many other areas that the old C# language services fell short.&lt;/p&gt;

&lt;p&gt;The Roslyn editors also add IntelliSense for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;see /&amp;gt;&lt;/code&gt; (and similar) tags in XML doc comments, allowing you to select any type or member and insert the correct doc comment ID (with generics declared appropriately).  This makes it far more convenient to write correctly-structured XML comments, which is particularly useful when compiling them to documentation sites (eg, with Sandcastle).&lt;/p&gt;

&lt;h2 id=&quot;refactorings&quot;&gt;Refactorings&lt;/h2&gt;
&lt;p&gt;The crown jewel of the new language services is the all-new refactoring engine.  All of the existing refactorings have been reworked using the new semantic APIs to preserve the meaning of your code, even if you introduce conflicting names or other challenges.  To make this work, the refactoring engine checks which symbol every identifier resolves to, then makes sure that each one still refers to the same symbol after the rename is complete.  If it detects any changes, it will &lt;a href=&quot;http://source.roslyn.codeplex.com/#Microsoft.CodeAnalysis.Workspaces/Simplification/Simplifier.cs#18&quot;&gt;fully-qualify&lt;/a&gt; the offending reference to force it to refer to the correct (original) symbol (it will then &lt;a href=&quot;http://source.roslyn.codeplex.com/#Microsoft.CodeAnalysis.Workspaces/Simplification/Simplifier.cs#25&quot;&gt;reduce&lt;/a&gt; the reference to become as simple as possible).&lt;/p&gt;

&lt;p&gt;The rename refactoring in particular has been completely redone.  Instead of showing a modal dialog asking for a new name, the Roslyn-powered rename experience is completely inline.  After pressing F2, you can start typing the new name directly in the editor, and all references to the symbol you’re renaming will be updated in real-time, including surrounding changes to resolve conflicts.  (unresolvable conflicts will be highlighted in red)&lt;/p&gt;

&lt;p&gt;The refactorings (both old and new) are now exposed contextually.  Press Ctrl + Dot to see which refactorings are available for the current cursor or selection in a smart tag popup; the language service will even show a syntax-highlighted preview of the generated code for each action.&lt;/p&gt;

&lt;p&gt;The existing refactorings include&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Generate variable/type/method/enum/constructor&lt;/li&gt;
  &lt;li&gt;Fully qualify reference/Add import&lt;/li&gt;
  &lt;li&gt;Remove unused usings&lt;/li&gt;
  &lt;li&gt;Implement interface/abstract class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As well as the existing VB-specific syntactic fixes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;End&lt;/code&gt; statement&lt;/li&gt;
  &lt;li&gt;Fix &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Exit&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Continue&lt;/code&gt; loop keyword&lt;/li&gt;
  &lt;li&gt;Move &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Option&lt;/code&gt; statement to top of file&lt;/li&gt;
  &lt;li&gt;Identifier spell check&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are also a number of completely new refactorings:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Simplify type name (removes unnecessary suffixes or namespace qualifiers, and uses keywords or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;using&lt;/code&gt; aliases where applicable)&lt;/li&gt;
  &lt;li&gt;Remove unnecessary cast&lt;/li&gt;
  &lt;li&gt;Add missing assembly reference&lt;/li&gt;
  &lt;li&gt;Fix function return type (VB only)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/2014/roslyn-refactorings.png&quot; alt=&quot;Roslyn Refactorings&quot; style=&quot;max-width: 100%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2014-05-28/exploring-roslyn-part-3-breaking-changes/&quot;&gt;&lt;em&gt;Next time: Breaking Changes&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-04-07-exploring-roslyn-part-1-introduction.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-04-07/exploring-roslyn-part-1-introduction/"/>
		<title type="text">Exploring Roslyn, part 1: Introduction</title>
		<updated>2014-04-07T00:00:00+00:00</updated>
		<published>2014-04-07T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Roslyn" term="roslyn" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;The .Net Compiler Platform, codenamed “Roslyn”, is the most ambitious project from Microsoft’s Developer Division in recent years.  The C# and VB teams got together and rewrote the compilers and language services from scratch in managed code (a mix of C# and VB), replacing a horrible mess of C++ code (with some managed code) that had mutated over the past ten years into a complex and difficult-to-modify codebase.  Now, after over five years of work, Roslyn is almost finished, and is an integral part of Visual Studio 2015, &lt;a href=&quot;https://www.visualstudio.com/news/vs2015-vs&quot;&gt;now available in preview&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;why-rewrite-everything&quot;&gt;Why rewrite everything?&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;The native compilers were so complicated that even simple language changes required enormous amounts of work.  The new managed compilers have a much better design which makes changes easy.  (note that actually designing a good language feature still requires a great deal of design work)&lt;/li&gt;
  &lt;li&gt;The native compilers were completely closed, so that any other component that needed to parse C# or VB source (eg, the editor, the Razor compiler, and certain Visual Studio services) each required separate parsers for C# and VB.&lt;/li&gt;
  &lt;li&gt;Because C# and VB have different histories and origins, the implementations of the native compilers and IDE services were completely different, involving a lot of logic that needed to be written (and maintained) twice for no good reason.  This also led to lots of pointless inconsistencies in the way the editor behaves (eg, code snippets).&lt;/li&gt;
  &lt;li&gt;Because the compilers didn’t expose any information about what they were compiling, implementing VS refactoring tools required a great deal of work to reproduce the semantic meaning of the code being refactored.  This is the primary reason that VS has so few refactorings, and why they’ve barely changed in nearly ten years.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;whats-so-cool-about-roslyn&quot;&gt;What’s so cool about Roslyn?&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Roslyn is completely open source!  You can browse the complete source code for the compiler toolchain and Visual Studio integration at &lt;a href=&quot;http://source.roslyn.io/&quot;&gt;source.roslyn.io&lt;/a&gt;, and you can even make your own contributions &lt;a href=&quot;https://github.com/dotnet/roslyn&quot;&gt;on GitHub&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Roslyn exposes the compiler’s syntax tree and semantic model, allowing you to easily write C# (or VB) code that parses a source file and explores – or even rewrites – the code.  This makes it easy to build all sorts of tools to analyze or modify source code.&lt;/li&gt;
  &lt;li&gt;Roslyn exposes a simple extensibility model that lets you create your own warnings or refactorings that integrate seamlessly with Visual Studio.&lt;/li&gt;
  &lt;li&gt;Thanks to the newly accessible semantic information, Roslyn includes a number of new refactorings, and has vastly improved the existing ones (especially Rename).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;inside-roslyn&quot;&gt;Inside Roslyn&lt;/h1&gt;
&lt;p&gt;Roslyn has a number of layers, each of which consumes the previous layer and adds more features.   As of early February 2015, the entire Roslyn stack, including the Visual Studio &amp;amp; debugger integration layers, is open source on GitHub.&lt;/p&gt;

&lt;h2 id=&quot;core-compiler-models&quot;&gt;Core Compiler Models&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis/&quot;&gt;Microsoft.CodeAnalysis.dll&lt;/a&gt; contains the basic types and infrastructure used by both compilers, including the base types for the syntax and semantic trees, a large collection of utility classes, and the code-generation infrastructure.  This includes all parts of the compilation &amp;amp; analysis processes that are not language-specific.&lt;/p&gt;

&lt;p&gt;This layer, as well as the next three layers, is split into pairs of portable and desktop assemblies.  The portable versions are full PCL assemblies that can run on platforms that do not offer standard filesystem access, such as Windows Runtime or Windows Phone.  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.Desktop&lt;/code&gt; variants add filesystem-aware APIs, and can only run on full .Net platforms (including Mono).  For example, the portable assemblies will compile and expose syntax trees &amp;amp; semantic models, and the Desktop layer adds the ability to resolve referenced DLLs from disk.&lt;/p&gt;

&lt;h2 id=&quot;language-specific-compilation-code&quot;&gt;Language-specific compilation code&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.CSharp&quot;&gt;Microsoft.CodeAnalysis.CSharp.dll&lt;/a&gt; and &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.VisualBasic&quot;&gt;Microsoft.CodeAnalysis.VisualBasic.dll&lt;/a&gt; contain the concrete implementations of the VB &amp;amp; C# compilers, building on top of Microsoft.CodeAnalysis.dll to implement language-specific logic.  These assemblies include parsers, concrete syntax and semantic tree types, and all of the actual compilation logic for their respective languages.&lt;/p&gt;

&lt;p&gt;These assemblies are invoked by the compiler applications to actually compile code, and can be used directly to parse &amp;amp; explore the syntax or semantic trees.&lt;/p&gt;

&lt;h2 id=&quot;actual-compilers&quot;&gt;Actual compilers&lt;/h2&gt;
&lt;p&gt;This layer includes the applications that you actually invoke to compile code, including &lt;a href=&quot;http://source.roslyn.io/#csc&quot;&gt;csc.exe&lt;/a&gt; and &lt;a href=&quot;http://source.roslyn.io/#vbc&quot;&gt;vbc.exe&lt;/a&gt; (the Roslyn versions of the command-line compilers), &lt;a href=&quot;http://source.roslyn.io/#Microsoft.Build.Tasks.Roslyn&quot;&gt;Microsoft.Build.Tasks.Roslyn.dll&lt;/a&gt; (the MSBuild task invoked from project files), and &lt;a href=&quot;http://source.roslyn.io/#VBCSCompiler&quot;&gt;VBCSCompiler.exe&lt;/a&gt; (the compilation server process that actually runs the compilation code for the MSBuild task to avoid JIT delays; see part 2).&lt;/p&gt;

&lt;p&gt;These projects are surprisingly small.  Since all of the actual work happens in the previous layer, all they do is parse the command-line options, then invoke the compilation DLLs.  (The MSBuild task is more complicated, since it needs to launch &amp;amp; communicate with the compilation server)&lt;/p&gt;

&lt;h2 id=&quot;workspaces&quot;&gt;Workspaces&lt;/h2&gt;
&lt;p&gt;The previous layers take a very localized view of C# and VB code.  They don’t know anything about Visual Studio, or even project files; instead, they operate on raw source code, bundled in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Compilation&lt;/code&gt; object that keeps track of all of the code being compiled, as well as compiler-level options like referenced assemblies.&lt;/p&gt;

&lt;p&gt;The Workspaces layer (&lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces&quot;&gt;core&lt;/a&gt;, &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.CSharp.Workspaces&quot;&gt;C#&lt;/a&gt;, and &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.VisualBasic.Workspaces&quot;&gt;VB&lt;/a&gt;) implements the higher-level development experience expected from an IDE.  This includes a number of concepts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Immutable &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Workspace/Solution/Project.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Project&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Workspace/Solution/Solution.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Solution&lt;/code&gt;&lt;/a&gt; types, containing collections of source files and references, just like you work with in Visual Studio.&lt;/li&gt;
  &lt;li&gt;A &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Workspace/Workspace.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Workspace&lt;/code&gt;&lt;/a&gt; class that contains a solution and manages changes (this is the mutable outer shell around the immutable projects &amp;amp; syntax trees).&lt;/li&gt;
  &lt;li&gt;Support for workflow features that are not actually part of the languages, such as loading MSBuild project files and parsing compiled XML doc comment files.&lt;/li&gt;
  &lt;li&gt;Implementations of basic IDE-level services such as source formatting, Find All References, rename, syntax highlighting, recommendations (the core parts of IntelliSense), code formatting, and a few other basic services for code editing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most importantly, these assemblies are still completely decoupled from Visual Studio, so you can easily use them to create your own IDE-like experiences.&lt;/p&gt;

&lt;p&gt;This layer too is split into desktop and portable versions.  All of the basic functionality remains in the portable assemblies; the desktop layers adds &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces.Desktop/Workspace/MSBuild/MSBuildWorkspace.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MSBuildWorkspace&lt;/code&gt;&lt;/a&gt;, which reads MSBuild &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;csproj&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vbproj&lt;/code&gt; files, and a couple of other desktop-specific APIs.&lt;/p&gt;

&lt;p&gt;This layer, as well as all of the following layers, is wired together using &lt;a href=&quot;https://mef.codeplex.com/&quot;&gt;MEF v2&lt;/a&gt;.  MEF is used to provide implementations of Roslyn internal services (especially across layers), to provide services to the Visual Studio editor system, and to import user-provided extensions.  For example, writing a custom codefix simply involves inheriting &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/CodeFixes/CodeFixProvider.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CodeFixProvider&lt;/code&gt;&lt;/a&gt; and exporting it via MEF using &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/CodeFixes/ExportCodeFixProviderAttribute.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[ExportCodeFixProvider]&lt;/code&gt;&lt;/a&gt; (most user-provided services have custom &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Export]&lt;/code&gt; attributes that provide additional metadata).&lt;/p&gt;

&lt;p&gt;The one exception to this rule is custom diagnostic analyzers.  The core compilation process, which invokes analyzers, does not use MEF; instead, the host passes a collection of &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis/DiagnosticAnalyzer/AnalyzerReference.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnalyzerReference&lt;/code&gt;s&lt;/a&gt; to the compiler (these come from Roslyn’s built-in analyzers, as well as any analyzer references in your project).  Within Visual Studio, Roslyn’s VS layer has a (MEF-exported) &lt;a href=&quot;http://source.roslyn.io/#Microsoft.VisualStudio.LanguageServices/Implementation/Diagnostics/VisualStudioWorkspaceDiagnosticAnalyzerProviderService.cs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VisualStudioWorkspaceDiagnosticAnalyzerProviderService&lt;/code&gt;&lt;/a&gt; which scans for VS extensions that contain analyzers (as a separate VSIX content type), allowing you to “export” analyzers in VS extensions as if it were a MEF export.  However, since it isn’t actually MEF, you can’t &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Import]&lt;/code&gt; things in these analyzers&lt;/p&gt;

&lt;h2 id=&quot;features&quot;&gt;Features&lt;/h2&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;The Microsoft.CodeAnalysis.(CSharp&lt;/td&gt;
      &lt;td&gt;VisualBasic).Features assemblies implement advanced IDE features.  These include the public base types to implement refactorings and diagnostics, as well as (as internal types) all of the refactorings &amp;amp; quick fixes in Visual Studio.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;These assemblies are still decoupled from Visual Studio, so you can use them too in your own independent projects.  (using MEF to import all of the internal implementations of the built-in services)&lt;/p&gt;

&lt;h2 id=&quot;editorfeatures&quot;&gt;EditorFeatures&lt;/h2&gt;
&lt;p&gt;This layer actually connects all of the earlier layers to Visual Studio’s WPF editor system, implementing VS editor services and invoking all of the features from the previous layers.&lt;/p&gt;

&lt;p&gt;This layer also implements other Visual Studio editor-based features that are tied to the language services, such as Metadata As Source, Peek Definition, Call Hierarchy, and more.&lt;/p&gt;

&lt;p&gt;The only public APIs in this layer are a set of extension methods in &lt;a href=&quot;http://source.roslyn.io/#Microsoft.CodeAnalysis.EditorFeatures.Text&quot;&gt;Microsoft.CodeAnalysis.EditorFeatures.Text.dll&lt;/a&gt; that link Visual Studio APIs to Roslyn APIs, providing connections between VS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ITextBuffers&lt;/code&gt; and Roslyn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Document&lt;/code&gt;s.  If you want to write normal VS extensions that consume Roslyn APIs (as opposed to Roslyn extensions like refactorings or diagnostics, which aren’t tied directly to Visual Studio at all), you will need this assembly to bridge the gap.&lt;/p&gt;

&lt;p&gt;Unlike the previous layers, these assemblies aren’t standalone; they reference the (MEF-based) Visual Studio WPF editor APIs.  However, they do not reference the rest of Visual Studio at all.  In fact, you can set up the Visual Studio editor components in a MEF container and host the entire Roslyn editor (using this layer) outside of Visual Studio.  For more details, see &lt;a href=&quot;https://github.com/SLaks/VSEmbed&quot;&gt;VSEmbed&lt;/a&gt;, where I did exactly that.&lt;/p&gt;

&lt;h2 id=&quot;languageservices&quot;&gt;LanguageServices&lt;/h2&gt;
&lt;p&gt;Finally, Microsoft.VisualStudio.LanguageServices.dll (and the corresponding C# &amp;amp; VB DLLs) contains other, non-editor-related Visual Studio integrations, including the project system, debugger, object browser, options pages, and all of the other gory details of the various ways that Visual Studio interacts with the language services.&lt;/p&gt;

&lt;p&gt;This layer is even less externally useful than EditorFeatures; it’s entirely coupled to ugly VS implementation details.  Its only useful public type is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VisualStudioWorkspace&lt;/code&gt; class, which extends the core Roslyn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Workspace&lt;/code&gt; with VS-specific integration methods to get projects, display definition &amp;amp; FAR results, and a few other things.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2014-05-21/exploring-roslyn-part-2-inside-end-user-preview/&quot;&gt;&lt;em&gt;Next time: Inside the End-User Preview&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-04-01-programming-without-errors-errorfree.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-04-01/programming-without-errors-errorfree/"/>
		<title type="text">Programming without errors – ErrorFree</title>
		<updated>2014-04-01T00:00:00+00:00</updated>
		<published>2014-04-01T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="ErrorFree" term="errorfree" />
		
		
		<category scheme="https://blog.slaks.net/#" label="programming-languages" term="programming-languages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="april-fools" term="april-fools" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Errors are one of the most common and annoying problems in programming.  Whether it’s the parser, the compiler, the type system, the runtime, or even system memory, everything you work with seems to have some way of complaining to you and refusing to run your code.  Programmers spend more time fixing errors than any other task when developing applications &lt;sup&gt;[citation needed]&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;But what if you could program without any errors?&lt;/p&gt;

&lt;p&gt;To avoid these troublesome errors, I am proud to present a new language called &lt;strong&gt;ErrorFree&lt;/strong&gt;.  This language cannot have any errors, anywhere.  In fact, every possible file, of any length, is a valid ErrorFree program that can compile and run successfully.&lt;/p&gt;

&lt;h1 id=&quot;design-philosophy&quot;&gt;Design Philosophy&lt;/h1&gt;
&lt;p&gt;Avoiding every possible kind of error (while maintaining Turing-completeness) presents a number of challenges:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Syntax errors&lt;/strong&gt;&lt;br /&gt;
To avoid all syntax errors, ErrorFree cannot have any kind of syntax.  Syntactical constructions such as blocks, matching braces, or composite expressions all present opportunities for syntax errors, and are thus unacceptable.&lt;br /&gt;
Instead, ErrorFree is a stack-based language that is parsed one byte at a time.  Every byte, except for pre-defined operator bytes, will simply push its numeric value onto the stack.  Operator bytes will perform operations on the value(s) at the top of the stack.&lt;br /&gt;
For example, the ErrorFree program &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12+&lt;/code&gt; will push the values &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;49&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50&lt;/code&gt; on to the stack (remember that these are bytes, not ASCII or Unicode characters), then add them together, resulting in a stack containing the number &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;99&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Encoding errors&lt;/strong&gt;&lt;br /&gt;
To avoid errors from invalid Unicode characters or UTF8-encoded sequences, ErrorFree source files are never treated as strings.  Instead, each byte is interpreted directly.  For programmer convenience, operator bytes are chosen based on their ASCII characters, making it easy to write ErrorFree code in a hex editor.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Stack underflow errors&lt;/strong&gt;&lt;br /&gt;
Source files like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;, which tries to operate on an empty stack, must also be valid.  Therefore, the ErrorFree stack is pre-populated with an infinite number of zeros.  Thus, it is never possible to run out of numbers on the stack.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Arithmetic errors&lt;/strong&gt;&lt;br /&gt;
Since dividing by zero (including 0 ÷ 0) must also be valid, the ErrorFree stack is composed of double-precision floating-point numbers, so that these operations can simply return NaN or Infinity, instead of throwing an error.  In contexts where an integer is required (eg, jumps or heap addresses), regular numbers are truncated, and NaNs and infinities are converted to zero.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Memory errors&lt;/strong&gt;&lt;br /&gt;
Regrettably, most computers do not have unlimited storage capacity.  If the stack grows too large to store, ErrorFree implementations are allowed to drop values from the bottom of the stack (above the cushion of infinite zeros) to reclaim space.  Similarly, if the heap (see below) grows too large, the implementation is allowed to drop values from the farthest end from the address being inserted into.&lt;br /&gt;
Implementations are encouraged to avoid this behavior as much as possible.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;conventions&quot;&gt;Conventions&lt;/h1&gt;
&lt;p&gt;Every byte in an ErrorFree source file will either push a value to the stack (a &lt;em&gt;value byte&lt;/em&gt;) or will perform an operation involving the stack (an &lt;em&gt;operator byte&lt;/em&gt;).  For convenience, operator bytes are chosen based on their ASCII values.  Operator bytes fall into three categories:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Arithmetic operators use the appropriate symbols.  For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt; will divide the top two values on the stack and push the result.&lt;/li&gt;
  &lt;li&gt;Pure/idempotent operators (operators that only depend on or affect the stack, and do not have side-effects or depend on external state) use lowercase letters symbolizing the operation.  For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt; will transpose the top two values on the stack.&lt;/li&gt;
  &lt;li&gt;Impure operators (operators that have side-effects) use uppercase letters symbolizing the operation.  For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;J&lt;/code&gt; will jump a number of bytes (the top of the stack) from the current position in the source file, then continue executing from that byte.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to the stack, ErrorFree provides a heap, which can store numbers (double-precision floating point, just like the stack) at any positive or negative integral index.  The heap can be used to store data, allowing you to build arrays, linked lists, or other more complex data structures.  Like the stack, the heap is initialized to zero at every location.&lt;/p&gt;

&lt;p&gt;ErrorFree does not provide a memory manager, so blocks of heap memory must be tracked by hand.  Similarly, ErrorFree does not provide a call stack, so function calls and return pointers must also be tracked by hand in the heap.&lt;/p&gt;

&lt;p&gt;Since raw bytes cannot be displayed as-is, ErrorFree source code should be displayed for reading as sequences of bytes in hexadecimal.  For better readability, operator bytes should be displayed as their single ASCII representation, preceded by a space to maintain alignment.  Newlines are meaningless, and can be used to group operations.  For example:&lt;/p&gt;

&lt;div class=&quot;errorfree&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;01 02  +
04  * 05  *
0E  +
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This code pushes the number 74 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(((1 + 2) * 4) * 5) + 0xE = 0x4A&lt;/code&gt;) on to the stack. (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4A&lt;/code&gt; cannot be pushed directly, since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;J&lt;/code&gt; is an operator byte.)  A simpler alternative would be &lt;code&gt;4B 01&amp;nbsp;&amp;nbsp;-&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;operators&quot;&gt;Operators&lt;/h1&gt;
&lt;p&gt;ErrorFree supports the following operators:&lt;/p&gt;

&lt;h2 id=&quot;arithmetic-operators&quot;&gt;Arithmetic operators&lt;/h2&gt;
&lt;p&gt;All arithmetic operators will pop the values they read.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;&lt;br /&gt;
Adds the top two values on the stack and pushes the result to the stack.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;&lt;br /&gt;
Subtracts the top value on the stack from the second value on the stack, and pushes the result to the stack.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;&lt;br /&gt;
Multiplies the top two values on the stack and pushes the result to the stack.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;&lt;br /&gt;
Divides the second value on the stack by the top value on the stack, and pushes the result to the stack.  0/0 pushes NaN; dividing other numbers by zero pushes positive or negative infinity.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;&lt;br /&gt;
Pushes the second value on the stack modulo the top value.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;&lt;br /&gt;
Raises the second value on the stack to the power of the top value, and pushes the result.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;&lt;br /&gt;
Pushes one if the top two values on the stack are equal, or zero otherwise.  NaN does not equal itself.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt;&lt;br /&gt;
Pushes one if the second value on the stack is greater than the top value, or zero otherwise.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&lt;/code&gt;&lt;br /&gt;
Pushes one if the second value on the stack is less than the top value, or zero otherwise.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;pure-operators&quot;&gt;Pure operators&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt; (Duplicate)&lt;br /&gt;
Pushes a second copy of the top value on the stack.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt; (Transpose)&lt;br /&gt;
Transposes the top two values on the stack.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; (Absolute value)&lt;br /&gt;
Pops a value from the stack and pushes its absolute value.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt; (Sign)&lt;br /&gt;
Pops a value from the stack and pushes its sign (-1, 0, or 1, or NaN).&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r&lt;/code&gt; (square Root)&lt;br /&gt;
Pops a value from the stack and pushes its square root (or NaN if it’s negative).&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; (Log)&lt;br /&gt;
Pops a value from the stack and pushes its base-10 logarithm  (or NaN if it’s negative).&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; (Floor)&lt;br /&gt;
Pops a value from the stack and pushes the largest integer smaller than that value.  Leaves infinities and NaNs unchanged.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt; (Ceiling)&lt;br /&gt;
Pops a value from the stack and pushes the smallest integer larger than that value.  Leaves infinities and NaNs unchanged.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;impure-operators&quot;&gt;Impure Operators&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; (print Character)&lt;br /&gt;
Pops a value from the stack, and prints its Unicode codepoint to standard out.  Non-integer values are truncated, negative values are made positive, and infinity and NaN become 0.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; (print Number)&lt;br /&gt;
Pops a value from the stack and prints its numeric value to standard out.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; (the letter after C)&lt;br /&gt;
Reads a single character from standard in, and pushes its Unicode codepoint index on to the stack.  Pushes -1 for EOF.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O&lt;/code&gt; (the letter after N)&lt;br /&gt;
Reads a number from standard in (followed by a newline) and pushes it onto the stack.  The strings &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Infinity&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-Infinity&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NaN&lt;/code&gt; are parsed as-is; all other letters or invalid characters are stripped.  If there are no numeric characters to read (for example, if standard in is at EOF), this will push 0.&lt;br /&gt;
There is currently no way to determine whether a 0 was read because the user typed zero, because the user typed nothing but letters, or because standard in is at EOF.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; (Random)&lt;br /&gt;
Pushes a random number in the range [0, 1) on to the stack.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; (Timestamp)&lt;br /&gt;
Pushes the UNIX timestamp for the current time on to the stack.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;J&lt;/code&gt; (Jump)&lt;br /&gt;
Pops a value from the stack, and moves the execution pointer that number of bytes from the current location in the file (forward or backward); execution continues from that byte.  Non-integer numbers are truncated, NaNs and infinities are treated as zero, and indices wrap around after reaching either end of the file.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; (Save)&lt;br /&gt;
Pops two values from the stack, then stores the second popped value at the heap location of the first (topmost) value.  Heap locations are truncated to integers; NaNs and infinities become zero.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L&lt;/code&gt;  (Load)&lt;br /&gt;
Pops a value from the stack, then reads the heap location at that address and pushes its value to the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;sample-program&quot;&gt;Sample program&lt;/h1&gt;
&lt;p&gt;This 19-byte program prints the squares of numbers 1 through 0x42.  It stores the loop index at heap location 0.&lt;/p&gt;

&lt;div class=&quot;errorfree&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;00  L 01  + 00  S
00  L  d  *  N
00  L 42  &amp;lt;
01  +  J
00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is a line-by-line explanation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Increment the value at heap location 0.&lt;br /&gt;
(Load, add one, store)&lt;/li&gt;
  &lt;li&gt;Print the square of said value.
(Load, duplicate, multiply, print)&lt;/li&gt;
  &lt;li&gt;Compare the value at heap location 0 to 0x42, pushing 1 if it’s less than 42 and 0 if it’s greater or equal.
   (Load, push comparand, compare)&lt;/li&gt;
  &lt;li&gt;Jump forward by two bytes if it was less (thus wrapping around to the beginning of the file), or one byte if it’s time to exit the loop.
   (add one, jump)&lt;/li&gt;
  &lt;li&gt;A final byte to jump to to exit the program.  Without this byte, jumping forward would either wrap around (for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;) or remain at the jump instruction (for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;), so the program would hang after finishing the loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;other-notes&quot;&gt;Other notes&lt;/h1&gt;
&lt;p&gt;It is impossible to make non-breaking changes to the ErrorFree language once it’s released, since any new code you want to allow already has an existing meaning.  Instead, newer versions must be explicitly specified when invoking the compiler/interpreter.&lt;/p&gt;

&lt;p&gt;ErrorFree does not include a built-in syntax for comments.  Instead, you can simply write your comments directly in the code, then precede them by jump operators so that they don’t execute.  When displaying code, comments can be written like operators.  Care must be taken to ensure that no other jump instructions end up jumping into the middle of a comment.  For example:&lt;/p&gt;

&lt;div class=&quot;errorfree&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;01 02  +
12  J  T  h  i  s     i  s     a     c  o  m  m  e  n  t
04  +
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-03-30-farewell-mvp-hello-google.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-03-30/farewell-mvp-hello-google/"/>
		<title type="text">Farewell, MVP Program ... Hello, Google!</title>
		<updated>2014-03-30T00:00:00+00:00</updated>
		<published>2014-03-30T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="microsoft-mvp" term="microsoft-mvp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="google" term="google" />
		
		
		<category scheme="https://blog.slaks.net/#" label="career" term="career" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;After four exciting years, I am regretfully leaving the &lt;a href=&quot;https://mvp.microsoft.com/&quot;&gt;Microsoft MVP program&lt;/a&gt;.  I have greatly enjoyed meeting and interacting with the Roslyn team, as well as the other C# &amp;amp; VB MVPs, and I hope to be able to keep up these connections at technical conferences.  I’ve also savored the unique opportunity to discuss the future of C# and Visual Studio with the development teams at Microsoft, especially as new versions of these products are developed (I’ve filed over 150 bugs through the program).&lt;/p&gt;

&lt;p&gt;In brighter news, I’m proud to announce that I’ve been hired by Google, working within the Google Docs team in the &lt;a href=&quot;https://www.google.com/about/careers/locations/new-york/&quot;&gt;NYC office&lt;/a&gt;.  Working for Google involves a wonderful corporate culture and a fantastic suite of internal tools, and I’m excited to contribute to Google’s products and improve the web for everyone.&lt;/p&gt;

&lt;p&gt;Since Google and Microsoft are direct competitors, it &lt;a href=&quot;https://msmvps.com/blogs/jon_skeet/archive/2009/10/01/mvp-no-more.aspx&quot;&gt;is not possible&lt;/a&gt; to maintain both positions.  Nevertheless, I hope to continue contributing to the C# community on StackOverflow, and to continue my open-source work on &lt;a href=&quot;https://github.com/madskristensen/WebEssentials2013&quot;&gt;Visual&lt;/a&gt; &lt;a href=&quot;https://github.com/SLaks/Ref12&quot;&gt;Studio&lt;/a&gt; &lt;a href=&quot;https://github.com/SLaks/Rebracer&quot;&gt;extensions&lt;/a&gt; (depending on how much spare time I have).&lt;/p&gt;

&lt;p&gt;Keep coding!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-02-26-extending-visual-studio-part-5-dealing-with-unversioned-assemblies.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-02-26/extending-visual-studio-part-5-dealing-with-unversioned-assemblies/"/>
		<title type="text">Extending Visual Studio, part 5: Dealing with Unversioned Assemblies</title>
		<updated>2014-02-26T00:00:00+00:00</updated>
		<published>2014-02-26T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2014-02-25/extending-visual-studio-part-4-writing-cross-version-extensions/&quot;&gt;Last time&lt;/a&gt;, I talked about how to write Visual Studio extensions that work in multiple versions of Visual Studio, using the built-in assembly redirects.&lt;/p&gt;

&lt;p&gt;Using unversioned assemblies is trickier.  The most straightforward approach is to release a separate VSIX extension for each version of Visual Studio.  This is the approach taken by &lt;a href=&quot;http://vswebessentials.com/&quot;&gt;Web Essentials&lt;/a&gt;, especially because it tends to be very tightly coupled to version-specific features within the web tools assemblies.  However, this makes it much more annoying to maintain the separate codebases.&lt;/p&gt;

&lt;p&gt;An alternative approach, &lt;a href=&quot;https://github.com/jaredpar/VsVim/tree/master/Src/VsSpecific&quot;&gt;taken by VsVim&lt;/a&gt;, is to put all code that uses unversioned assemblies in a separate project for each supported VS version.  The primary extension assembly would then check the VS version at runtime to only load the wrapper assembly for the current version of Visual Studio.  This approach is most convenient if you’re only using small bits of functionality from the unversioned assemblies, so that they can easily be abstracted out.&lt;/p&gt;

&lt;p&gt;Finally, you can reference the assembly directly, then make sure that VS loads the correct version at runtime.  This requires careful extra work to make sure that VS always ends up loading the correct assembly.&lt;/p&gt;

&lt;p&gt;If you’re only using the unversioned assembly in XAML (eg, for themed controls from Microsoft.VisualStudio.Shell.ViewManager), you can simply reference the assemblies directly.  As long as you don’t specify the version when declaring the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xmlns:&lt;/code&gt; in your XAML file, the XAML will be compiled with a partial assembly name, and the runtime will bind it to the already-loaded assembly from the current VS version.&lt;/p&gt;

&lt;p&gt;If you’re using it in code, this technique will not work.  The C# compiler will always emit the full names of referenced assemblies, including the version number (taken from whatever version of the DLL the compiler loaded).  Instead, you can &lt;a href=&quot;/2013-12-25/redirecting-assembly-loads-at-runtime/&quot;&gt;redirect the assembly load at runtime&lt;/a&gt; with an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AssemblyResolve&lt;/code&gt; handler, allowing your code to handle the request for the mis-versioned assembly and load the correct version instead.&lt;/p&gt;

&lt;p&gt;The caveat in this approach is that you must add the AssemblyResolve handler before VS tries to load any type from the unversioned assembly.  Otherwise, the CLR assembly loader will try to load the wrong version before you can catch it, and nothing will work.  Even worse, because assembly loads are cached, once it fails to load the assembly once, it will never try again, even after you add your handler.&lt;/p&gt;

&lt;p&gt;In particular, if your VSIX DLL exports MEF types (A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MefComponent&lt;/code&gt; asset), the MEF catalog will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Assembly.GetTypes()&lt;/code&gt; to find all exports and imports, before any of your code has a chance to run.  If any type in your assembly (including compiler-generated types for lambda expressions or iterators) has a field with a type from an unversioned DLL, the CLR will try to load that DLL immediately, and everything will break.&lt;/p&gt;

&lt;p&gt;One way to solve this problem would be to write a module initializer that adds your handler as soon as your DLL is loaded, before the call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetTypes()&lt;/code&gt;.  Unfortunately, C# does not support module initializers, so unless you want to add a post-build step that uses ilasm &amp;amp; ildasm to inject an initializer by hand, this won’t help.&lt;/p&gt;

&lt;p&gt;Instead, you simply need to make sure that you don’t have any types that directly reference unversioned assemblies.  Make sure that you don’t have any fields (or base classes) of types from unversioned assemblies, and that you don’t use variables of those types in iterator methods or capture them in lambda expressions.  If you violate this rule, your extension will fail with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TypeLoadException&lt;/code&gt; on any version of Visual Studio other than the version of the unversioned assembly that you bound to.&lt;/p&gt;

&lt;p&gt;If you’re getting this exception, you can run the following code in &lt;a href=&quot;http://linqpad.net&quot;&gt;LINQPad&lt;/a&gt; to figure out what types are causing problems:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BasePath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;@&quot;C:\Path\To\Solution&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReferencesPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BasePath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;@&quot;References\v10.0\&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Load the assembly containing the types to test&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetAssembly&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;LoadFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BasePath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;@&quot;YourProject\bin\Debug\YourProject.dll&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Load the versioned assemblies that VS will load successfully&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;LoadFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReferencesPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Microsoft.VisualStudio.Shell.10.0.dll&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;LoadFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReferencesPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Microsoft.VisualStudio.Text.Data.dll&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;LoadFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReferencesPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Microsoft.VisualStudio.Text.UI.Wpf.dll&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successfulTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Try to load the types without the unversioned assemblies&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;targetAssembly&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Loaded types&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;All types loaded successfully!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReflectionTypeLoadException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;successfulTypes&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Types&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LoaderExceptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;Message&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;: &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;FusionLog&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FileNotFoundException&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Util&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;OnDemand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Full Log&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileNotFoundException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FusionLog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Load Errors&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Load the unversioned assemblies that will be handled by AssemblyResolve&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;LoadFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReferencesPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Microsoft.VisualStudio.CSharp.Services.Language.dll&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;targetAssembly&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Except&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;successfulTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Failed types&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that you will need to reset the query’s AppDomain (Ctrl+Shift+F5 in LINQPad) after you run it so that the unversioned assemblies won’t be loaded next time.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next time: Tips from the trenches&lt;/em&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-02-25-extending-visual-studio-part-4-writing-cross-version-extensions.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-02-25/extending-visual-studio-part-4-writing-cross-version-extensions/"/>
		<title type="text">Extending Visual Studio, part 4: Writing cross-version extensions</title>
		<updated>2014-02-25T00:00:00+00:00</updated>
		<published>2014-02-25T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2014-02-21/extending-visual-studio-part-3-assembly-versioning/&quot;&gt;Last time&lt;/a&gt;, I talked about the different approaches that Visual Studio takes toward assembly versioning.&lt;/p&gt;

&lt;p&gt;Navigating this mess and producing a version-independent VSIX requires careful planning.  The first step is to pick a minimum version of Visual Studio that you want to support.  Your extension cannot use features or APIs that were introduced after this version.  For most extensions, the minimum version would be VS2010, which introduced both the VSIX extension manager and the new WPF editor.  However, if you want to extend newer features, such as Peek, the new CSS or HTMLX editors, or Roslyn, you will need to require a later minimum VS version.&lt;/p&gt;

&lt;p&gt;Note that the format for VSIX manifests changed in VS2012.  If you want to create an extension using VS2012 or later, and have it work on VS2010, you will need to manually rewrite the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Source.extension.vsixmanifest&lt;/code&gt; file to use version 1 of the XML schema (this will break the designer).  You can do that by copying the manifest from an existing 2010-compatible extension, such as &lt;a href=&quot;https://github.com/SLaks/Rebracer&quot;&gt;mine&lt;/a&gt;, and editing all of the fields to match your extension.  In particular, make sure to change the unique extension ID (this does not actually need to be a GUID; it can be convenient to place your extension’s name before the GUID to gain both uniqueness and readability).&lt;/p&gt;

&lt;p&gt;Obviously, your extension cannot reference any immutable assemblies newer than your minimum VS target version.  However, it is perfectly fine to add a reference to an immutable assembly installed with a newer version of VS; they are, after all, immutable.&lt;/p&gt;

&lt;p&gt;When using versioned assemblies, you need to make sure that you don’t accidentally use APIs introduced in newer versions of VS.  You also need to make sure that your extension is compiled only against the minimum versions of versioned assemblies; otherwise, it will still fail to load on older VS versions  (the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;bindingRedirect&amp;gt;&lt;/code&gt;s only redirect older versions, not newer ones).&lt;/p&gt;

&lt;p&gt;To solve both of these problems, you need to make a local copy of the minimum versions of any versioned assemblies that you reference (typically in a folder named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;References&lt;/code&gt; under source control, and ideally in a subfolder by VS version for clarity).  You can find these assemblies from any installed copy of the older VS versions (including Express Edition), or online from other open source projects that maintain this kind of compatiblity, such as &lt;a href=&quot;https://github.com/jaredpar/VsVim&quot;&gt;VsVim&lt;/a&gt;, &lt;a href=&quot;https://nuget.codeplex.com&quot;&gt;NuGet&lt;/a&gt;, or &lt;a href=&quot;https://github.com/SLaks/Rebracer&quot;&gt;Rebracer&lt;/a&gt;.  You must also add that directory to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReferencePaths&lt;/code&gt; setting in the project to tell the compiler to find the DLLs from that folder; otherwise, the project won’t build if the older VS version isn’t installed.  You can do this &lt;a href=&quot;https://github.com/jaredpar/RoundTripVSIX/commit/12003ab6a13c2fa3d017863f2f11496b7cb04425&quot;&gt;directly in the csproj file&lt;/a&gt; or in Project Properties in the Reference Paths section.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2014-02-26/extending-visual-studio-part-5-dealing-with-unversioned-assemblies/&quot;&gt;&lt;em&gt;Next time: Using unversioned assemblies&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-02-24-dissecting-the-new-net-reference-source-browser.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-02-24/dissecting-the-new-net-reference-source-browser/"/>
		<title type="text">Dissecting the new .Net Reference Source Browser</title>
		<updated>2014-02-24T18:30:00+00:00</updated>
		<published>2014-02-24T18:30:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Roslyn" term="roslyn" />
		
		
		<category scheme="https://blog.slaks.net/#" label="reference-source" term="reference-source" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;The new &lt;a href=&quot;https://referencesource.microsoft.com/&quot;&gt;.Net Reference Source Browser&lt;/a&gt; (see my &lt;a href=&quot;/2014-02-24/inside-the-new-net-reference-source/&quot;&gt;previous post&lt;/a&gt;) is an excellent example of one of the less-obvious uses of the new &lt;a href=&quot;https://msdn.com/roslyn&quot;&gt;Roslyn&lt;/a&gt; toolchain.  &lt;a href=&quot;https://twitter.com/KirillOsenkov&quot;&gt;Kirill Osenkov&lt;/a&gt; used Roslyn to recreate Visual Studio’s source browser experience in a standalone webapp, allowing people to browse the .Net Reference Source from anywhere.  In this post, I will explore how this app is implemented.&lt;/p&gt;

&lt;p&gt;The source browser is generated by a conversion tool that uses Roslyn to parse every source file in the codebase and generate a massive collection of HTML files comprising the full browser.  Almost all of the work is done statically at build time; the only server-side code is the search engine.  This makes it much faster to use, at the cost of consuming hundreds of thousands of files and a couple of gigabytes of disk space.&lt;/p&gt;

&lt;h1 id=&quot;generating-output-files&quot;&gt;Generating output files&lt;/h1&gt;
&lt;p&gt;The converter tool loads every project file in the source release into a Roslyn workspace.  It then traverses through every file in every assembly found in the workspace to generate source.  As I mentioned last time, some of the projects reference assemblies that do not have source available (either because they’re written in managed C++ or for licensing reasons).  For these projects, the converter uses Roslyn’s generated Metadata As Source files (from Visual Studio’s Go to Definition feature).&lt;/p&gt;

&lt;p&gt;It runs each source file through the same Roslyn syntax highlighter used by VS itself, converting the result classification spans to HTML to generate the basic source code.&lt;/p&gt;

&lt;h1 id=&quot;symbol-navigation&quot;&gt;Symbol Navigation&lt;/h1&gt;
&lt;p&gt;For the navigation features, each member is assigned a unique identifier, based on the standard doc comment ID (the identifiers used in compiled XML doc comment files).   These identifiers are then hashed (using the first half of the MD5 to save space), and compiled into a giant index file (separate for each assembly) mapping the hash to the source file defining the member.&lt;/p&gt;

&lt;p&gt;The definition of each symbol then gets this hash in its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id=&quot;&quot;&lt;/code&gt;, so that adding the hash to the fragment in the URL to the source file will jump to the definition.  Some identifiers are the definition for more than one symbol (eg, type declarations are also the definitions of their default constructors); for these, the identifier is wrapped in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;span&amp;gt;&lt;/code&gt; tag so that it has two separate IDs.&lt;/p&gt;

&lt;p&gt;Javascript code in the index file looks up the hash from its URL fragment and redirects to the actual source file.  Thus, you can navigate to a URL like https://referencesource.microsoft.com/mscorlib/a.html#266f59a804f72937 and end up at the actual definition of the symbol with that hash.  This results in much shorter URLs than including the full path to the source file in the URL, and also allows other people to build URLs without knowing which file a symbol is defined (for example, this is how I built &lt;a href=&quot;https://github.com/SLaks/Ref12&quot;&gt;Ref12&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The converter runs each source file through the binding phase of the Roslyn compiler to get a semantic model mapping each source token to the symbol it refers to.  This model is used to wrap each symbol in an HTML hyperlink pointing to its hash in the index file.  When the link is clicked, Javascript code in the index file looks up the hash in the URL fragment, then redirects to the actual source file.&lt;/p&gt;

&lt;p&gt;Similarly, it generates a static HTML file for each symbol containing a list of references to that symbol (each a hyperlink to a line of source).  The converter wraps each member definition in a link to this file to power the Find All References feature.&lt;/p&gt;

&lt;p&gt;For members defined in the same assembly, it generates links pointing directly to the source file defining the member; this saves the step of redirecting to the index file.  For members defined in other assemblies, it links to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a.html&lt;/code&gt;, so that assemblies aren’t tightly coupled to other projects’ source code.&lt;/p&gt;

&lt;h1 id=&quot;search&quot;&gt;Search&lt;/h1&gt;
&lt;p&gt;The search engine is the only piece of server-side code in the entire browser.  It’s implemented using an ASP.Net Web API project, powered by the &lt;a href=&quot;https://referencesource.microsoft.com/System.Core/d.txt&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D.txt&lt;/code&gt; file&lt;/a&gt; for each assembly.  Typing into the search box sends an AJAX request to https://referencesource.microsoft.com/api/symbols/?symbol=text, which finds all symbols matching the search query and replies with a set of HTML links to the definitions (through the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a.html&lt;/code&gt; index mentioned above, so that the server doesn’t need to know where the results are defined in source).&lt;/p&gt;

&lt;p&gt;Occasionally, you might see a search come back with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Index is being rebuilt&lt;/code&gt;; this means that the API AppDomain was unloaded, so it needs to read the index data back into memory before it can perform any searches.&lt;/p&gt;

&lt;h1 id=&quot;icons&quot;&gt;Icons&lt;/h1&gt;
&lt;p&gt;The icons used in the Reference Source Browser are the same icons used by Visual Studio itself.  These are a numbered set of 236 different icons, and can be found at https://referencesource.microsoft.com/content/icons/0.png (through 235.png).  The names of these icons come from the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/microsoft.visualstudio.language.intellisense.standardglyphgroup&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StandardGlyphGroup&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/microsoft.visualstudio.language.intellisense.standardglyphitem&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StandardGlyphItem&lt;/code&gt;&lt;/a&gt; enums in Microsoft.VisualStudio.Language.Intellisense.dll.  The first 190 icons come in groups of 6; one icon for every possible accessibility (including both FamOrAssem, which corresponds to C#’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;internal&lt;/code&gt; keyword, and FamAndAssem, which is not supported by C#).
You can run the following code (which requires a reference to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Language.Intellisense&lt;/code&gt;) to download all of the icons with the correct names:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetDir&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Combine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetFolderPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SpecialFolder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyPictures&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;@&quot;Visual Studio Glyphs\&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Directory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CreateDirectory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;targetDir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StandardGlyphGroup&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StandardGlyphGroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StandardGlyphGroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GlyphGroupUnknown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;StartsWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;GlyphGroup&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;WebClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;DownloadFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
			&lt;span class=&quot;s&quot;&gt;&quot;https://referencesource.microsoft.com/content/icons/&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.png&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;targetDir&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; - &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Glyph&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.png&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;WebClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;DownloadFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
			&lt;span class=&quot;s&quot;&gt;&quot;https://referencesource.microsoft.com/content/icons/&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.png&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;targetDir&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; - &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;GlyphGroup&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;-&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StandardGlyphItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;GlyphItem&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.png&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-02-24-inside-the-new-net-reference-source.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-02-24/inside-the-new-net-reference-source/"/>
		<title type="text">Exploring the new .Net Reference Source Code</title>
		<updated>2014-02-24T18:23:00+00:00</updated>
		<published>2014-02-24T18:23:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="reference-source" term="reference-source" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Seven years ago, Microsoft released the .Net Reference Source Code, containing source for much of the .Net BCL for use with debugging.  You could configure Visual Studio to use Microsoft’s public symbols servers, then step into the actual source code of .Net methods and see what’s going on under the hood.&lt;/p&gt;

&lt;p&gt;While this was, in theory, extremely useful, it had a number of limitations that made it far less useful.  A number of the source files were blank, or simply missing entirely.  The source code had been filtered by a poorly-made scrubber to remove all employee names, including those in actual source, resulting in lines like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lock (s_[....])&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private void [....]()&lt;/code&gt;.  The debug symbols were rarely updated after .Net patches were released, so debugging wouldn’t work after installing updates to .Net.  Most annoyingly, there was no way easy to browse the code independently to read the source for a particular type.&lt;/p&gt;

&lt;p&gt;Today, thanks to the efforts of &lt;a href=&quot;https://twitter.com/KirillOsenkov&quot;&gt;Kirill Osenkov&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/AlokShriram&quot;&gt;Alok Shriram&lt;/a&gt;, and others, Microsoft has redone the source release and solved all of these problems.  The new version includes almost all of the actual source files, including things like attributes or enums that weren’t in the old releases.  The scrubbing process has been fixed to only process comments &amp;amp; string literals so that all of the source files are syntactically correct.  The release process has been simplified so that new symbols can be published for every .Net update.&lt;/p&gt;

&lt;p&gt;Most importantly, you can now download a &lt;a href=&quot;https://referencesource.microsoft.com/download.html&quot;&gt;50MB zip file&lt;/a&gt; containing all of the source files with their original structure, complete with csproj (and vbproj) files that you can open in VS.  For the first time, you can open the .Net source code in VIsual Studio, and browse the source with the full VS experience, including Go To Definition, Find All References, quick search, and all of the other VS browsing features.&lt;/p&gt;

&lt;p&gt;These are not the original csproj files from the actual BCL build, and nothing will actually compile.  The compilation process for the BCL projects is far more complicated than a simple build, including auto-generated localization (SR) files, bits of native code, and various other specialized tools, including MSBuild magic for &lt;a href=&quot;https://blogs.msdn.com/b/kirillosenkov/archive/2013/11/23/circular-assembly-references-in-the-net-framework.aspx&quot;&gt;cyclic dependencies&lt;/a&gt;.  However, they’re perfect for browsing within VS.&lt;/p&gt;

&lt;h1 id=&quot;reference-source-browser&quot;&gt;Reference Source Browser&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;/images/2014/reference-source-browser.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;/images/2014/reference-source-browser.png&quot; alt=&quot;Reference Source Browser&quot; style=&quot;float: right; max-width: 40%&quot; /&gt;&lt;/a&gt;
However, the crown jewel of the new release is the new online source browser.  Kirill Osenkov used Roslyn to create a wonderful web app that presents the full source code in an easily browsable fashion.  You can search for 
any class or function name, then see complete syntax-highlighted source code right in your browser, without having to download or install anything (the search engine is actually more powerful than that; see the &lt;a href=&quot;https://referencesource.microsoft.com/&quot;&gt;home page&lt;/a&gt; for more details).  You can explore more directly by clicking any project from the home page to see all of the source files in that project.  You can even see a full list of types in each project by namespace (click the namespace icon in any file), perfect for discovering little-known types in large assemblies.&lt;/p&gt;

&lt;p&gt;Inside a source file, the true power of this tool is available.  You can click any class or function name anywhere to immediately jump to its definition, allowing you to quickly discover exactly what obscure or internal methods do when trying to understand a complicated procedure (you can then hit Back to jump right back to where you were before).  If you’re looking at a definition, you can click on it to see all references to that class or member anywhere in all of the reference source, allowing you to quickly see how complicated features are used, or to see where an internal API is surfaced.  Because this is produced by the full Roslyn toolchain, this works with all references, including less-obvious ones like extension methods, type-inferred lambda expressions, etc.&lt;/p&gt;

&lt;p&gt;As you navigate through the source, the URL will update to point to that exact file, so that you can always share the URL with other people to show what you’re looking at (this also works for Find All References).  You can also click any line number to get a URL pointing that line (just like GitHub), perfect for showing people a particular piece of code.&lt;/p&gt;

&lt;p&gt;Some parts of the .Net framework do not have source code in the browser, either because they’re written in C++/CLI (which Roslyn cannot parse), or because the relevant product teams have not yet completed the bureaucracy required to release the source publicly.  To preserve Go to Definition within these projects, the source browser includes source from metadata for them (just like you get when from Go to Definition in Visual Studio, and in fact powered by the same Roslyn-based code).  In the list of projects on the homepage, the ones without actual source simply have the DLL file as the filename, instead of the path to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.csproj&lt;/code&gt; file.&lt;/p&gt;

&lt;h1 id=&quot;visual-studio-integration&quot;&gt;Visual Studio Integration&lt;/h1&gt;
&lt;p&gt;The one shortcoming of the new release is that you can’t jump directly to the source of a function from within Visual Studio.  When debugging, you can step into functions that you call using the debug symbols, but you cannot quickly see the source as you write code in the first place.&lt;/p&gt;

&lt;p&gt;To solve that, I created a Visual Studio extension called &lt;a href=&quot;https://visualstudiogallery.msdn.microsoft.com/f89b27c5-7d7b-4059-adde-7ccc709fa86e&quot;&gt;&lt;strong&gt;Ref12&lt;/strong&gt;&lt;/a&gt;.  After installing this extension on Visual Studio 2010 or later, you can press F12 on any function in the .Net class libraries and jump to the reference source for that function in your default browser (if you’re at Microsoft and actually working on the .Net framework, the extension won’t get in your way).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i1.visualstudiogallery.msdn.s-msft.com/f89b27c5-7d7b-4059-adde-7ccc709fa86e/image/file/125181/1/ref12%20screenshot.png&quot; alt=&quot;Ref12 Screenshot&quot; style=&quot;max-width: 100%&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;easter-eggs&quot;&gt;Easter Eggs&lt;/h1&gt;
&lt;p&gt;There are also a number of hidden URLs in the reference source containing additional stats:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;https://referencesource.microsoft.com/i.txt contains stats for the overall index, including line count (over 6 million!) and more&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/mscorlib/i.txt (and for other projects) contains those same stats for individual projects&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/Projects.txt contains a list of project files (or DLL files from assemblies without source) included&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/Assemblies.txt contains a list of all assemblies in the source, along with the index for that project in Projects.txt, and the number of other projects that reference it&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/TopReferencedAssemblies.txt lists the number of other projects that reference each project, in descending order&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/System.Core/References.txt lists the assemblies that each project references&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/System.Core/ReferencingAssemblies.txt lists the assemblies that reference each project&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/System.Core/BaseMembers.txt lists, for each overriding class member, the containing assembly and unique hash of the base member that it overrides or implements&lt;/li&gt;
  &lt;li&gt;https://referencesource.microsoft.com/System.Core/d.txt lists every member in each assembly, including the short name, unique hash, member type, full name, and glyph index&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of these files are useful for exploring the released source; most of the later ones are more useful for building new functionality on top of the source browser.  For more information, see my next post, which will explain in detail exactly how the source browser works.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2014-02-24/dissecting-the-new-net-reference-source-browser/&quot;&gt;&lt;em&gt;Next time: Under the hood&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-02-21-extending-visual-studio-part-3-assembly-versioning.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-02-21/extending-visual-studio-part-3-assembly-versioning/"/>
		<title type="text">Extending Visual Studio, part 3: Assembly Versioning</title>
		<updated>2014-02-21T00:00:00+00:00</updated>
		<published>2014-02-21T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio" term="visual-studio" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2013-11-10/extending-visual-studio-part-2-core-concepts/&quot;&gt;Last time&lt;/a&gt;, I talked about the core concepts and basic assemblies within Visual Studio that are used to create extensions.  This time, I’ll talk about how Microsoft manages these assemblies across Visual Studio.&lt;/p&gt;

&lt;p&gt;One of the complex aspects of writing Visual Studio extensions is dealing with versioning issues.  Each release of Visual Studio changes the major version of all of the VS DLLs.  Without some kind of work around, VS extensions would need a separate project for each version of Visual Studio, so that it can reference the correct versions of the VS DLLs.&lt;/p&gt;

&lt;p&gt;Visual Studio takes a number of different approaches to this problem:&lt;/p&gt;

&lt;h2 id=&quot;immutable-assemblies&quot;&gt;Immutable assemblies&lt;/h2&gt;
&lt;p&gt;Some VS assemblies simply never change, at all.  These assemblies have exactly one version – the version of VS that they were introduced in – and newer versions of VS continue to ship the exact same DLL.&lt;/p&gt;

&lt;p&gt;This is the simplest, most ideal solution to the problem; extensions can simply reference these DLLs directly, and they will continue to work with all future VS versions without any problems.  However, since most VS functionality is designed to change in future versions, few parts of VS can live in immutable DLLs.&lt;/p&gt;

&lt;p&gt;VS includes two categories of immutable DLLs: COM interop assemblies and immutable shell assemblies.&lt;/p&gt;

&lt;p&gt;COM interop assemblies (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.Interop.XX&lt;/code&gt; and similar) contain nothing but COM interfaces (including supporting enums and structures, but with no classes at all).  Since COM does not allow existing interfaces to change, immutability isn’t a problem here.  Instead, new functionality is added by creating new interfaces in newer COM assemblies (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsSolution&lt;/code&gt; through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsSolution5&lt;/code&gt;) with additional methods.&lt;/p&gt;

&lt;p&gt;Immutable shell assemblies (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.Immutable.XX&lt;/code&gt;) contain managed types that the shell team has decided will never need to change.  They mostly comtain simple types, such as interfaces, enums, and EventArgs classes, that are used by other parts of the shell.&lt;/p&gt;

&lt;h2 id=&quot;versioned-assemblies&quot;&gt;Versioned assemblies&lt;/h2&gt;
&lt;p&gt;Then there are assemblies that do change, but which Microsoft officially supports for use in extensions.  In order to make ordinary Visual Studio extensions work in future versions of Visual Studio, VS includes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;bindingRedirect&amp;gt;&lt;/code&gt;s in devenv.exe.config that redirect all older versions of these assemblies to the correct version for the installed copy of VS.  This way, extensions that reference older versions will be seamlessly redirected to the correct version on newer VS installations.&lt;/p&gt;

&lt;p&gt;In turn, Microsoft must make sure to never introduce breaking changes in these assemblies.  To facilitate this, versioned assemblies tend to mostly contain interfaces &amp;amp; officially-public APIs only; most of the implementations &amp;amp; internal APIs live in unversioned assemblies elsewhere (especially &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Platform.VSEditor.dll&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Versioned assemblies include all of the public APIs for the new WPF editors (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Text.*&lt;/code&gt;), the core shell APIs (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.Shell.XX&lt;/code&gt;), and a number of assemblies for specific features.  See devenv.exe.config in your VS installation directory for a full list.&lt;/p&gt;

&lt;h2 id=&quot;unversioned-assemblies&quot;&gt;Unversioned assemblies&lt;/h2&gt;
&lt;p&gt;Finally, there are DLLs with no compatibility guarantees whatsoever.  Most other VS assemblies, especially implementation assemblies and assemblies for smaller or younger teams, are unversioned assemblies, with a separate version for each VS release and no binding redirects at all.&lt;/p&gt;

&lt;p&gt;There is no simple way to use unversioned assemblies in a VSIX without requiring a separate project for each VS release.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2014-02-25/extending-visual-studio-part-4-writing-cross-version-extensions/&quot;&gt;&lt;em&gt;Next time: Writing cross-version extensions&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-01-27-creating-multiple-visual-studio-profiles-with-rootsuffix.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-01-27/creating-multiple-visual-studio-profiles-with-rootsuffix/"/>
		<title type="text">Creating multiple Visual Studio “Profiles” with RootSuffixes</title>
		<updated>2014-01-27T00:00:00+00:00</updated>
		<published>2014-01-27T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio-2013" term="visual-studio-2013" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;When using Visual Studio, it can ocasionally be useful to have separate “profiles” – to be able to start multiple instances of Visual Studio with independent settings.  For example, if you work on multiple projects that use very different source formatting settings, you can create a separate profile for each one, allowing you to open each project with the correct settings.  If you create books or blog posts that have screenshots of Visual Studio, you can create a separate profile with pristine settings and no extensions installed, so that you can create screenshots that match the out-of-box experience.&lt;/p&gt;

&lt;p&gt;Visual Studio supports this with the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/bb166560&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/RootSuffix&lt;/code&gt; switch&lt;/a&gt;.  If you launch Visual Studio with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/RootSuffix YourName&lt;/code&gt;, it will create new settings containers in the registry and in AppData with that name (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HKCU\Software\Microsoft\VisualStudio\12.0YourName&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;user profile&amp;gt;\AppData\Local\Microsoft\VisualStudio\12.0YourName&lt;/code&gt;).  It will then ask you to sign in to a Microsoft account (for VS2013) and to pick a base settings file, just like launching Visual Studio for the first time.&lt;/p&gt;

&lt;p&gt;To launch Visual Studio with a command-line parameter, type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;devenv /RootSuffix YourName&lt;/code&gt; into the Start menu / Start screen, then select the first result.  You can also use the command prompt (from the VS installation directory), or copy the shortcut to VS from the start menu and add the parameter to the Target field.&lt;/p&gt;

&lt;p&gt;This feature is usually used when testing Visual Studio extensions.  VS extension projects will automatically register themselves in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/RootSuffix Exp&lt;/code&gt; hive (the “Experimental Instance”), which is launched when you hit F5.  This allows you to test your extensions without interfering with normal development.&lt;/p&gt;

&lt;p&gt;Generally, new RootSuffixes behave identically to, and completely independent of, regular Visual Studio instances.  You can set all settings, maintain separate Recent lists, even install other extensions into the separate instance using Extension Manager.&lt;/p&gt;

&lt;p&gt;However, there is no way to install an extension directly from a VSIX file into a separate RootSuffix.  VSIX packages from the Visual Studio Gallery install as usual, but there is no way to install a VSIX from somewhere else (such as a private beta or a development version of an existing extension).  Double-clicking a VSIX will install it into the primary instance, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSIXInstaller&lt;/code&gt; command-line tool has no option to specify the RootSuffix, and you cannot install a VSIX by dragging it into Visual Studio.&lt;/p&gt;

&lt;p&gt;To solve this, I wrote a &lt;a href=&quot;https://github.com/SLaks/Root-VSIX&quot;&gt;command-line tool&lt;/a&gt; that installs VSIXs into any Visual Studio RootSuffix.  You can download Root-VSIX from &lt;a href=&quot;https://github.com/SLaks/Root-VSIX/releases&quot;&gt;here&lt;/a&gt;, then install a VSIX to any VS RootSuffix on the command line:&lt;/p&gt;

&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;Root&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;-VSIX &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&amp;lt;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;VS&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;RootSuffix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;Path&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;VSIX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2014-01-03-the-perils-of-uppercase-in-gmail-send-as.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2014-01-03/the-perils-of-uppercase-in-gmail-send-as/"/>
		<title type="text">The Perils of Uppercase in Gmail's Send As</title>
		<updated>2014-01-03T00:00:00+00:00</updated>
		<published>2014-01-03T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="gmail" term="gmail" />
		
		
		<category scheme="https://blog.slaks.net/#" label="bug" term="bug" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;One of Gmail’s many useful features is “Send mail as”, which lets you use a single Gmail account to send emails from multiple email addresses.  Especially when combined with email forwarding, this is a great way to manage multiple email accounts from a single Gmail tab.&lt;/p&gt;

&lt;p&gt;By default, this feature will send the email from Gmail’s regular SMTP servers, including your actual email address in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sender&lt;/code&gt; header to indicate the original sender of the email (without that, the email would be rejected as spam, since it isn’t coming from the correct SMTP servers for that domain).  To avoid this, you can select &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Send through my SMTP servers&lt;/code&gt; to tell Gmail to connect to your domain name’s actual SMTP servers and send the email directly through them.  This approach makes the email indistinguishable from regular emails sent from that domain name.  To make it work, you need to enter the SMTP server, username, and password for Gmail to connect with.&lt;/p&gt;

&lt;p&gt;However, when using this feature to send through a Google Apps domain with &lt;a href=&quot;https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail&quot;&gt;DKIM&lt;/a&gt; enabled, I noticed that every email I sent through Send as got an invalid DKIM signature, causing some email providers to reject it as spam (you can check this using &lt;a href=&quot;https://www.port25.com/support/authentication-center/email-verification/&quot;&gt;port25’s verifier&lt;/a&gt;).  Sending email directly over a regular SMTP client worked, but sending through Gmail’s Send As would break the DKIM signature.&lt;/p&gt;

&lt;p&gt;After corresponding with Gmail support, I discovered that this happens if you use uppercase characters in the email address to send from.  If you enter the address with only lowercase characters, it works fine; if you include uppercase characters, the signature breaks.
If you’re having this problem, you need to delete the Send As entry, then re-create in all-lowercase.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-12-25-redirecting-assembly-loads-at-runtime.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/"/>
		<title type="text">Redirecting Assembly Loads at Runtime</title>
		<updated>2013-12-25T00:00:00+00:00</updated>
		<published>2013-12-25T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="assemlies" term="assemlies" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;.Net’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/yx7xezcf&quot;&gt;assembly resolver&lt;/a&gt; (Fusion) takes assembly names (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MyCompany.MyProduct, Version=2.1.0.0, , Culture=neutral, PublicKeyToken=3bf5f017df1a30a5&lt;/code&gt;) and resolves them to actual DLL files, searching in the Global Assembly Cache, the folder containing the entry assembly, and any additional PrivateBinPaths registered for the AppDomain.&lt;/p&gt;

&lt;p&gt;You can change the way it resolves specific assemblies using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;bindingRedirect&amp;gt;&lt;/code&gt; tags in your App.config (or Web.config) file, giving it a different name to use instead if it tries to resolve a specific range of versions for an assembly.  This is useful if you want to support references to multiple versions of the same assembly (eg, from older plugins), but only want to use a single version at runtime.&lt;/p&gt;

&lt;p&gt;However, these redirects must be specified statically in the config file, and cannot be changed at runtime.  If you don’t control the config file (eg, if you’re writing a plugin), or if you don’t know what redirects you will want until runtime, this doesn’t help.&lt;/p&gt;

&lt;p&gt;Instead, you can handle the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppDomain.AssemblyResolve&lt;/code&gt; event&lt;/a&gt;, which lets you run your own code to load an assembly if the loader fails to find it.&lt;/p&gt;

&lt;p&gt;This event is meant to be used if you put your assemblies somewhere where the loader can’t find them (eg, as embedded resources in your EXE for a single-file application, in a subfolder, or even over HTTP).  However, you can also use this to catch loads of older versions of an assembly and load the newest version instead.&lt;/p&gt;

&lt;p&gt;There are a few traps to beware of when writing AssemblyResolve handlers:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;You must add your handler before the runtime tries to load the assembly.  In particular, note that the JITter will load all assemblies used by a method before the method starts executing, in order to properly JIT-compile the method itself.&lt;br /&gt;
This means that you must add your AssemblyResolve handler before calling any method that uses types in the assembly in question.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Similarly, when you first reference a type, its static initializer will run, and the JITter will load any assemblies used by its static constructor (in addition to assemblies containing types in method signatures or fields).&lt;br /&gt;
Therefore, you must also add your handler before loading any type that uses the assembly directly.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The assemly resolver will only raise this event if it fails to find the assembly on its own.  In particular, if you load an assembly that is in the GAC using its full name (including version and PublicKeyToken), the AssemblyResolve event will not be raised at all.  This limits the utility of this event for binding redirection.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If you call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Assembly.Load()&lt;/code&gt; in your handler with a more-precise assembly name, make sure to prevent stack overflows if that assembly name isn’t found either (the loader will raise the event again and re-enter your handler)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Once an assembly name fails to load, &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/aa98tba8&quot;&gt;the runtime will cache the binding failure&lt;/a&gt; and never try to load that name again.  In particular, if an assembly fails to load before you attach your handler, it’s too late for you to handle it next time.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I wrote code to handle &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AssemblyResolve&lt;/code&gt; and simulate a single binding redirect:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;///&amp;lt;summary&amp;gt;Adds an AssemblyResolve handler to redirect all attempts to load a specific assembly name to the specified version.&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;RedirectAssembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shortName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Version&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetVersion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;publicKeyToken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ResolveEventHandler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;// Use latest strong name &amp;amp; version when trying to load SDK assemblies&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requestedAssembly&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AssemblyName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;requestedAssembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shortName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Redirecting assembly load of &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;
					  &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;,\tloaded by &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RequestingAssembly&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;(unknown)&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RequestingAssembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

		&lt;span class=&quot;n&quot;&gt;requestedAssembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Version&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetVersion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;requestedAssembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;SetPublicKeyToken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AssemblyName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x, PublicKeyToken=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;publicKeyToken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetPublicKeyToken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;requestedAssembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CultureInfo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CultureInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InvariantCulture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;n&quot;&gt;AppDomain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CurrentDomain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AssemblyResolve&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;requestedAssembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;AppDomain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CurrentDomain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AssemblyResolve&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-12-10-syntax-highlighted-markdown-code-blocks-web-essentials.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-12-10/syntax-highlighted-markdown-code-blocks-web-essentials/"/>
		<title type="text">Syntax-highlighted Markdown Code Blocks in Web Essentials</title>
		<updated>2013-12-10T00:00:00+00:00</updated>
		<published>2013-12-10T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio-2013" term="visual-studio-2013" />
		
		
		<category scheme="https://blog.slaks.net/#" label="web-essentials" term="web-essentials" />
		
		
		<category scheme="https://blog.slaks.net/#" label="markdown" term="markdown" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;After over two months of work, I rewrote the Markdown editor in &lt;a href=&quot;http://vswebessentials.com/&quot;&gt;Web Essentials&lt;/a&gt; to support syntax highlighting &amp;amp; IntelliSense for embedded code blocks.&lt;/p&gt;

&lt;p&gt;If you add a GitHub-style &lt;a href=&quot;https://help.github.com/articles/github-flavored-markdown#syntax-highlighting&quot;&gt;fenced code block&lt;/a&gt; with a language identifier, Visual Studio will now provide full language services within that code block.
You get the full editing experience you’re used to in Visual Studio, including syntax highlighting, IntelliSense, outlining, error checking, code snippets, and Peek Definition.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2013/markdown-code-demo.png&quot; alt=&quot;Markdown Code Blocks&quot; /&gt;
&lt;img src=&quot;/images/2013/markdown-errors-intellisense.png&quot; alt=&quot;Markdown IntelliSense &amp;amp; Errors&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is perfect for writing Readmes or documentation for open-source projects on GitHub, or for any other Markdown files you may write.&lt;/p&gt;

&lt;h1 id=&quot;how-it-works&quot;&gt;How it works&lt;/h1&gt;
&lt;p&gt;Visual Studio 2010 rewrote the editor from the ground up in WPF.  This new editor has a powerful feature called &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd885240.aspx#projection&quot;&gt;Projection Buffers&lt;/a&gt;, which allow a single editor to seamlessly mix text from different sources.  This can be used to make a virtual editor that mixes code from different files (eg, &lt;a href=&quot;https://blogs.msdn.com/b/kaelr/archive/2012/03/10/code-canvas-vs-code-bubbles-vs-debugger-canvas.aspx&quot;&gt;Debugger Canvas&lt;/a&gt;), or to mix different languages within a single file (eg, Javascript and CSS blocks within an HTML file).&lt;/p&gt;

&lt;p&gt;My Markdown editor uses this feature to project code blocks in your Markdown files using Visual Studio’s built-in editor services&lt;/p&gt;

&lt;h1 id=&quot;what-languages-are-supported&quot;&gt;What languages are supported?&lt;/h1&gt;
&lt;p&gt;Any language that Visual Studio can highlight, including any custom extensions you’ve installed.&lt;/p&gt;

&lt;p&gt;All languages services built for the new WPF-based editor should work perfectly with projection buffers.  This includes HTML (sort of), CSS, Javascript, and any languages from extensions that only work in VS2010 or later.  The language service itself needs to work properly with projection buffers; if it makes unwarranted assumptions about text buffers always having files on disk or only one TextView, things may not work.&lt;/p&gt;

&lt;p&gt;If the language service needs to initialize things for its services to work (eg, a project system), I can still support it using an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICodeLanguageEmbedder&lt;/code&gt;, which Web Essentials will call to initialize whatever is necessary.  If you have such a language service, feel free to send a pull request.&lt;/p&gt;

&lt;p&gt;Before VS2010, an older system called &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb166334.aspx&quot;&gt;contained languages&lt;/a&gt; allowed editors to be mixed like this.  However, this system needed explicit integration from the language service being embedded.  As far as I know, the only langauge services that support contained languages are C# &amp;amp; VB.  I added an adapter layer to make these languages work as well (thanks to &lt;a href=&quot;https://twitter.com/jasonmalinowski&quot;&gt;Jason Malinowski&lt;/a&gt; for spending lots of time getting this to work).  If you have a different language that implements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsContainedLanguageFactory&lt;/code&gt;, get in touch with me &amp;amp; I’ll add support.&lt;/p&gt;

&lt;p&gt;To specify the language, use either the name of the ContentType or any extension (without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;) recognized by Visual Studio.  If there are other names that should be recognized, send a pull request to add them to the map in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContentTypeExtensions.ContentTypeAliases&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;known-issues&quot;&gt;Known Issues&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Fenced code blocks don’t work in the Preview pane&lt;br /&gt;
We use &lt;a href=&quot;https://code.google.com/p/markdownsharp/&quot;&gt;MarkdownSharp&lt;/a&gt; to compile Markdown to HTML for the preview pane, and it does not support fenced code blocks.  Until MarkdownSharp is upgraded (or until we find an alternative), we can’t support that.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Autoformat doesn’t work&lt;br /&gt;
  The HTML auto-format engine does not work properly with embedded code blocks.  This may be fixed in the next release of the web tooling.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Embedded XML, C++, and F# don’t get any syntax highlighting or IntelliSense&lt;br /&gt;
  These languages are built using the old &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsColorizer&lt;/code&gt; infrastructure and cannot work in ProjectionBuffers.  There is a separate service called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsContainedLanguage&lt;/code&gt; that could allow old language services to work, but AFAIK, it is only implemented by C# &amp;amp; VB.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Highlighted ranges don’t update correctly for certain kinds of edits&lt;br /&gt;
Keeping track of where code blocks are as you edit the document (without spending too much time parsing) is complicated.  If you notice it failing, &lt;a href=&quot;https://github.com/madskristensen/WebEssentials2013/issues/new&quot;&gt;file a bug&lt;/a&gt; and tell me exactly what you changed (please include the source file if you can).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Code blocks automatically indent too far&lt;br /&gt;
I’m not sure how fixable this is.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Background colors in syntax highlighting don’t appear&lt;br /&gt;
It looks like the background color for the Markdown code block overrides any background colors within the code itself.  Other than disabling the background color for code blocks (in Options), I’m not sure if there is any way to fix this.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;whats-next&quot;&gt;What's Next&lt;/h1&gt;
&lt;p&gt;So far, all I’ve done is provide the foundation for a modern Markdown editing experience.  There are lots of other features that would make the Markdown editor more complete.  If you would like to work on to any of these features, &lt;a href=&quot;https://twitter.com/Schabse&quot;&gt;tweet me&lt;/a&gt; and I’ll give you advice.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Toolbar buttons &amp;amp; shortcut keys to toggle bold, italic, quotes, headers, lists&lt;/li&gt;
  &lt;li&gt;Pre-typing list or indented-code-block prefixes when pressing enter&lt;/li&gt;
  &lt;li&gt;IntelliSense, URL tooltips, &amp;amp; clickable hyperlinks for reference-style links (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[text][link name]&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;Smart tags to toggle between inline links and reference links&lt;/li&gt;
  &lt;li&gt;IntelliSense for URL paths in links&lt;/li&gt;
  &lt;li&gt;Separate IntelliSense &amp;amp; highlighting for &lt;a href=&quot;https://jekyllrb.com/docs/frontmatter/&quot;&gt;YAML front-matter&lt;/a&gt; for Jekyll blog posts (&lt;a href=&quot;https://github.com/madskristensen/WebEssentials2013/issues/148#issuecomment-26823193&quot;&gt;#148&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Thumbnail tooltips for images&lt;/li&gt;
  &lt;li&gt;Separate config file with referenced assemblies &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;using&lt;/code&gt; statements for IntelliSense in C#/VB code blocks&lt;/li&gt;
  &lt;li&gt;Fix parser to code blocks in lists (which need 8 spaces)&lt;/li&gt;
  &lt;li&gt;Fix highlighting for quote blocks&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-12-01-creating-unused-events.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-12-01/creating-unused-events/"/>
		<title type="text">Creating unused events in C#</title>
		<updated>2013-12-01T00:00:00+00:00</updated>
		<published>2013-12-01T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="events" term="events" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;Some interfaces have events that most implementations will never raise.  For example, the WPF &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICommand&lt;/code&gt; interface has a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CanExecuteChanged&lt;/code&gt; event that should be raised when the command becomes enabled or disabled.  Most commands are always enabled, so this event is no needed.  However, the interface still requires you to implement this event.&lt;br /&gt;
Thus, most &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ICommand&lt;/code&gt; implementations will look something like this:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyCommand&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ICommand&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CanExecute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CanExecuteChanged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will generate a compiler warning, “The event ‘MyCommand.CanExecuteChanged’ is never used”.&lt;/p&gt;

&lt;p&gt;To understand why events give a warning where unused methods don’t, you must understand more about how event work.  &lt;a href=&quot;/2011/07/about-net-events.html&quot;&gt;.Net events are actually an accessor pattern&lt;/a&gt;, just like properties.  For example:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyProperty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The compiler transforms this code into something resembling&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyProperty_BackingField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_MyProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyProperty_BackingField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set_MyProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyProperty_BackingField&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add_MyEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Thread-safe version of MyEvent += value;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;remove_MyEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Thread-safe version of MyEvent -= value;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Just like the compiler generates a backing field when you create an auto-implemented property, the compiler also generates a backing field when you create a &lt;em&gt;field-like&lt;/em&gt; event.  Unlike properties, event accessors only allow you to add or remove handlers from the event.  To access the delegates currently in the event (eg, to call them and raise the event), you use the private backing field.  This is why you can’t raise an event from outside the class that defines it – the backing field is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt;.  To be more precise, the name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MyEvent&lt;/code&gt; resolves to the backing field when used inside the class, and refers to the event (the accessor pair) when used elsewhere.&lt;/p&gt;

&lt;p&gt;It is this auto-generated field the compiler is warning you about.  Unlike an empty method (or a non-auto property with empty accessors), this backing field will needlessly waste memory for every instance of the class that you create.  Therefore, just like any other field, the compiler will give you a warning if you never use the field.&lt;/p&gt;

&lt;p&gt;To solve this warning, you need to get rid of the field.  You can do that by explicitly defining event accessors that don’t do anything:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;remove&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that &lt;a href=&quot;https://blogs.msdn.com/b/samng/archive/2007/11/26/virtual-events-in-c.aspx&quot;&gt;you should not make a field-like event &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;virtual&lt;/code&gt;&lt;/a&gt;; the compiler does not handle overridden field-like events gracefully.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-11-10-extending-visual-studio-part-2-core-concepts.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-11-10/extending-visual-studio-part-2-core-concepts/"/>
		<title type="text">Extending Visual Studio 2013, Part 2: Core Concepts</title>
		<updated>2013-11-10T00:00:00+00:00</updated>
		<published>2013-11-10T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio-2013" term="visual-studio-2013" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2013-10-18/extending-visual-studio-part-1-getting-started/&quot;&gt;My previous post&lt;/a&gt; described how to get started writing Visual Studio extensions.  This post will introduce the basic concepts needed to work with Visual Studio’s extensibility APIs.&lt;/p&gt;

&lt;h1 id=&quot;creating-an-extension&quot;&gt;Creating an extension&lt;/h1&gt;

&lt;p&gt;The Visual Studio SDK includes a number of templates for creating extensions.  Which template you use depends on what parts of Visual Studio you want to extend.&lt;/p&gt;

&lt;h2 id=&quot;editor-extensions&quot;&gt;Editor Extensions&lt;/h2&gt;
&lt;p&gt;If you only want to extend the new WPF editor, you can create an editor extension.  Editor extensions allow you to add or change features (IntelliSense, highlighting, etc) for existing languages that use the new editor, as well as creating entirely new language service with their own syntax highlighting &amp;amp; IntelliSense.  These projects simply export MEF components to be consumed by the editor, without involving any COM interop at all.&lt;/p&gt;

&lt;p&gt;The SDK includes four different templates for editor extensions; these templates include sample code for specific editor features:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The Editor Classifier template contains a simple synax highlighting service for text files.  You will need to write a parser to figure out which spans to highlight&lt;/li&gt;
  &lt;li&gt;The Editor Margin template creates a WPF control which will be displayed below the text editor.  You can easily change where the margin will appear; you will need to write a useful UI to display in the margin.&lt;/li&gt;
  &lt;li&gt;The Editor Text Adornment template adds WPF visuals within the text, relative to individual characters.  You will need to write code to determine where to place visuals, and design visuals &amp;amp; interactivity to display.&lt;/li&gt;
  &lt;li&gt;Finally, the Editor Viewport Adornment template adds UI to the text editor itself.  You will need to figure out what UI to display, and what z-index it should get among the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.editor.predefinedadornmentlayers&quot;&gt;text editor’s existing layers&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these templates includes a MEF export of the appropriate editor service (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IClassifierProvider&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IWpfTextViewMarginProvider&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsTextViewCreationListener&lt;/code&gt;) for the editor to import and invoke.  Extensions can export as many components as you want; you can freely mix and match exports from different templates in your project.  If you want to extend a different part of the editor (eg, IntelliSense, outlining, or error checking), you can start from any of these templates, then delete the existing classes and start fresh.&lt;/p&gt;

&lt;h2 id=&quot;vspackage-extensions&quot;&gt;VsPackage extensions&lt;/h2&gt;
&lt;p&gt;If you want to extend other aspects of Visual Studio, you’ll need to dig into the older COM code and create a VsPackage project.  VsPackages can extend all other parts of Visual Studio, including tool windows, options pages, project system, and menu/toolbar commands.  They can also add event handlers to other parts of Visual Studio to further customize behavior.&lt;/p&gt;

&lt;p&gt;VsPackage extensions are created from the VsPackage project template, which will open a wizard allowing you to add sample functionality to your package.  You can create a sample menu command, a custom tool window, or a fully custom editor (hosting your own WinForms control such as a designer rather than the built-in WPF text editor), then edit the samples to include your actual functionality.&lt;/p&gt;

&lt;h2 id=&quot;more-advanced-extensions&quot;&gt;More advanced extensions&lt;/h2&gt;
&lt;p&gt;Visual Studio extensions are packaged in VSIX files, which are ZIP files containing DLLs and other files used by the extension.  When you install an extension, the VSIX installer will extract the VSIX to a folder for the extension, in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;user profile&amp;gt;\AppData\Local\Microsoft\VisualStudio\12.0\Extensions\&amp;lt;random characters&amp;gt;&lt;/code&gt;.  If your extension needs other files (eg, a separate EXE file), you can add the files to your project, then set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Include in VSIX&lt;/code&gt; to true in the Properties window.  You can get the installation directory in your code from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Path.GetDirectoryName(typeof(YourType).Assembly.Location)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The VSIX file also controls how your DLL is loaded by Visual Studio; this is specified by the Assets section &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/ee943167.aspx&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Source.extension.vsixmanifest&lt;/code&gt; file&lt;/a&gt; in the extension project.  To load a VsPackage from your project, register it as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.VsPackage&lt;/code&gt; asset; to load MEF exports, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Microsoft.VisualStudio.MefComponent&lt;/code&gt; (the project templates do this for you).  If you want to use both approaches in the same extension, you’ll need to register the project twice; once as each asset type.&lt;/p&gt;

&lt;h1 id=&quot;working-with-vspackages&quot;&gt;Working with VsPackages&lt;/h1&gt;

&lt;p&gt;VsPackage extensions must contain a class that implements &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivspackage&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsPackage&lt;/code&gt;&lt;/a&gt;, which Visual Studio will call to intialize the extension.  Most managed extensions will instead inherit the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.package&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Package&lt;/code&gt; helper class&lt;/a&gt;, which implements most of this and other basic interfaces for you, leaving you free to write actual extensions.  For more information, see &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/bb166209.aspx&quot; title=&quot;Managed VSPackages&quot;&gt;MSDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In your package class, you can apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Provide*]&lt;/code&gt; attributes to provide options pages, tool windows, project items, and other services.  See &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/Microsoft.VisualStudio.Shell%28v=vs.110%29.aspx#typeList&quot;&gt;here&lt;/a&gt; (scroll down to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Provide&lt;/code&gt;) for all available attributes.  You can also override the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Initialize()&lt;/code&gt; method to add menu commands (using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IMenuCommandService&lt;/code&gt; service) and register event handlers.&lt;/p&gt;

&lt;p&gt;The COM portions of Visual Studio are linked together using service providers.  To get an instance of a VS-provided COM interface, you need to call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetService()&lt;/code&gt; on a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ServiceProvider&lt;/code&gt; object and pass it the GUID for the service you need, from the GUID for the matching &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SVs*&lt;/code&gt; interface.  It will return a COM object implementing the appropriate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVs*&lt;/code&gt; interface.&lt;/p&gt;

&lt;p&gt;Your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsPackage&lt;/code&gt; will get an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IServiceProvider&lt;/code&gt; in its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SetSite()&lt;/code&gt; method, which Visual Studio will call as the extension is initialized.  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Package&lt;/code&gt; wrapper class implements this for you and saves a reference to the service provider, exposing a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetService()&lt;/code&gt; method for you to call:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;textManager&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IVsTextManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SVsTextManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The COM interop classes for the various services you can use are contained in assemblies with names ending in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.Interop&lt;/code&gt;; MSDN has a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/bb164686.aspx&quot;&gt;full list&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you need to interact with newer MEF components from a VsPackage, you need to grab Visual Studio’s global composition service from from the ServiceProvider:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;componentModel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IComponentModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SComponentModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;working-with-mef-extensions&quot;&gt;Working with MEF extensions&lt;/h1&gt;
&lt;p&gt;Using MEF is much simpler.  You can export components used by the editor by implementing the appropriate editor interface, then decorating your class with the MEF &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Exports]&lt;/code&gt; attribute.  In these classes, you can import other editor services by decorating a field with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Imports]&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;The new editor is built around ContentTypes, which map editor services to file types.  Each editor language (C#, HTML, CSS, etc) has a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContentType&lt;/code&gt; containing the name of the language, as well as a list of base ContentTypes that it inherits.  ContentType inheritance allows you to reuse editor service in more advanced languages; for example, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Razor&lt;/code&gt; ContentType inherits from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;htmlx&lt;/code&gt; ContentType  (HTMLX is the &lt;a href=&quot;https://madskristensen.net/post/my-road-to-visual-studio-2013&quot;&gt;new HTML editor&lt;/a&gt; that shipped in VS2013; the old HTML editor is not easily extensible).  You can create your own ContentTypes by applying attributes to MEF-exported static fields; see the documentation for &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd885244.aspx#sectionToggle0&quot;&gt;details&lt;/a&gt; and a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ee372313.aspx&quot;&gt;walkthrough&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Typically, you will only want to export services for certain ContentTypes, or for documents with certain roles (eg, read-only editors, editors that are backed by an actual file, as opposed to the output window, etc).  You can control this by applying filtering attributes like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[ContentType]&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[TextViewRole]&lt;/code&gt; to your exported class; most editor components will filter by these attributes when importing.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Export&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IVsTextViewCreationListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ContentType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;htmlx&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyHtmlTextViewCreationListener&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IVsTextViewCreationListener&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Import&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IVsEditorAdaptersFactoryService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EditorAdaptersFactoryService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Import&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IClassifierAggregatorService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Classifiers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Import&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ICompletionBroker&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CompletionBroker&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Import&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IQuickInfoBroker&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QuickInfoBroker&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Import&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SVsServiceProvider&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ServiceProvider&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;VsTextViewCreated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IVsTextView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;textViewAdapter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For a full list of importable service, see &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd885243.aspx&quot; title=&quot;Editor Imports&quot;&gt;MSDN&lt;/a&gt;.  Note that this list only includes services from the editor itself; other parts of Visual Studio, such as the web tooling, export their own services that are not documented anywhere.  To find these, use a decompiler.&lt;/p&gt;

&lt;p&gt;If you need to use a COM-based service, you can get a ServiceProvider by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Import]&lt;/code&gt;ing an instance of the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.svsserviceprovider.aspx&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SVsServiceProvider&lt;/code&gt; interface&lt;/a&gt;, as shown in the code above.&lt;/p&gt;

&lt;p&gt;Exportable editor services generally fall into two categories: listeners and providers.  Listeners are used to handle events from the editor system.  For example, you can export an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IVsTextViewCreationListener&lt;/code&gt; and the editor will call your function whenever a text view is created.  Similarly, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IWpfTextViewConnectionListener&lt;/code&gt; will notify you when a TextBuffer is created for your ContentType, including new buffers in existing views  (for &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd885240.aspx#projection&quot;&gt;projection buffers&lt;/a&gt;).  Other VS components, such as the web tooling, have their own listeners.&lt;/p&gt;

&lt;p&gt;Providers allow the editor to create instances of your services when creating text views of buffers.  Since all MEF-managed parts are essentially singletons and cannot have multiple instances, the editor will import a single instance of an &lt;code&gt;I&lt;i&gt;Whatever&lt;/i&gt;Provider&lt;/code&gt;.  This provider will expose a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Create()&lt;/code&gt; method that takes a text buffer and returns an  &lt;code&gt;I&lt;i&gt;Whatever&lt;/i&gt;&lt;/code&gt; for that document, allowing the editor to create as many instances as it needs.&lt;/p&gt;

&lt;p&gt;For more information on the various services you can provide for the editor, see &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd885244.aspx&quot; title=&quot;Editor Extension Points&quot;&gt;MSDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2014-02-21/extending-visual-studio-part-3-assembly-versioning/&quot;&gt;&lt;em&gt;Next time: How Visual Studio deals with assembly versioning&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-10-18-extending-visual-studio-part-1-getting-started.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-10-18/extending-visual-studio-part-1-getting-started/"/>
		<title type="text">Extending Visual Studio 2013, Part 1: Getting Started</title>
		<updated>2013-10-18T00:00:00+00:00</updated>
		<published>2013-10-18T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="visual-studio-2013" term="visual-studio-2013" />
		
		
		<category scheme="https://blog.slaks.net/#" label="vs-extensions" term="vs-extensions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;In addition to being an excellent development environment, Visual Studio also has a powerful extensibility system.  In this blog post, I will explain how to start writing Visual Studio extensions, so you can make the IDE work the way you want it to.&lt;/p&gt;

&lt;h1 id=&quot;getting-started&quot;&gt;Getting Started&lt;/h1&gt;
&lt;p&gt;To use or develop extensions, you need Visual Studio Professional or higher (Express Edition won’t work).&lt;/p&gt;

&lt;p&gt;First, download and install the &lt;a href=&quot;https://www.microsoft.com/visualstudio/eng/downloads#d-vs-sdk&quot;&gt;Visual Studio SDK&lt;/a&gt; (for VS2012, see &lt;a href=&quot;https://www.microsoft.com/en-us/download/details.aspx?id=30668&quot;&gt;here&lt;/a&gt;; this adds project types for Visual Studio extensions and is required in order to open or create any extension.&lt;/p&gt;

&lt;p&gt;Next, you need to decide whether to add features to an existing open source extension or create a brand new extension.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://vswebessentials.com/&quot;&gt;&lt;strong&gt;Web Essentials&lt;/strong&gt;&lt;/a&gt;&lt;br /&gt;
Web Essentials, by &lt;a href=&quot;https://twitter.com/mkristensen&quot;&gt;Mads Kristensen&lt;/a&gt;, is a collection of (mostly) web-related enhancements to Visual Studio.  It includes lots of new IntelliSense completions (especially for CSS), new BrowserLink features, automatic JSHint for Javascript files, new warnings for HTML and CSS, and many other features.  Any web-related functionality you want to add should probably go here.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://sidewaffle.com/&quot;&gt;&lt;strong&gt;SideWaffle&lt;/strong&gt;&lt;/a&gt;&lt;br /&gt;
SideWaffle is collection of code snippets and templates (both single files and entire projects) for popular frameworks (Chrome extensions, Angular apps, Azure interactions, etc…).  If you simply want to make a new template to create a preconfigured file or project, SideWaffle is the way to go.  See &lt;a href=&quot;https://youtu.be/h4VaORKgrOw&quot;&gt;this video&lt;/a&gt; for detailed instructions.&lt;/li&gt;
  &lt;li&gt;Your Name Here&lt;br /&gt;
If you want to create something that doesn’t fall into one of these categories, or if you’re creating something complex enough (or specialized enough) that it wouldn’t fit in Web Essentials, you can create your own extension, and upload it to the &lt;a href=&quot;https://visualstudiogallery.msdn.microsoft.com/&quot;&gt;Visual Studio Gallery&lt;/a&gt; yourself.&lt;br /&gt;
To start from scratch, click File, New Project, select, Visual C# (or VB), Extensibility, then create a VSIX project.  This will create an empty extension; you can also select one of the pre-built templates if they match your needs:&lt;br /&gt;
  &lt;br /&gt;
  &lt;img src=&quot;/images/2013/vs-new-extension-project.png&quot; alt=&quot;Extensibility Project Templates&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to extend an existing extension, you’ll need to create an account on &lt;a href=&quot;https://github.com&quot;&gt;GitHub&lt;/a&gt; and fork the project to your account so that you can push your changes.  Then, clone the project to your computer (&lt;a href=&quot;https://windows.github.com/&quot;&gt;GitHub for Windows&lt;/a&gt; is an easy way to do this; &lt;a href=&quot;https://www.sourcetreeapp.com/&quot;&gt;SourceTree&lt;/a&gt; is more powerful) and start making changes.  Make sure to commit to every time you finish working on something so you have a nice history; you can do this &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/hh850437&quot;&gt;directly from Visual Studio&lt;/a&gt; or in the aforementioned applications.  Finally, once your feature is finished and tested, open a pull request on GitHub to ask the project maintainer to merge in your changes.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;##Side Note: Tips for contributing with git
The pull request model work best with &lt;em&gt;feature branches&lt;/em&gt; – making a separate branch for each contribution.  Before starting each feature, you’ll want to create a new branch, reset that branch to the latest version of the original (upstream) project, then make changes from there.&lt;/p&gt;

  &lt;p&gt;This is easiest to do on the git command line.  First of all, if you aren’t already, use &lt;a href=&quot;https://dahlbyk.github.io/posh-git/&quot;&gt;posh-git&lt;/a&gt; to get nice tab completion for git commands and branch names.  (GitHub for Windows includes this if you use PowerShell; git bash has a similar feature)&lt;/p&gt;

  &lt;p&gt;First, you need to add the upstream repository as a remote (an external git repository that you can pull from) so that you can pull changes directly from the original project to your machine.  This only needs to be done once:&lt;/p&gt;

  &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git remote add upstream &amp;lt;url&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;upstream&lt;/code&gt; is the name of the new remote; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;git-url&amp;gt;&lt;/code&gt; is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://github.com/...&lt;/code&gt; URL of the &lt;em&gt;original&lt;/em&gt; repository.  You can find this URL on the right side of the project on GitHub.com:&lt;/p&gt;

  &lt;p&gt;&lt;img src=&quot;/images/2013/github-clone-url.png&quot; alt=&quot;GitHub clone URL&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Once you’ve added the remote, run the following commands to make a new branch and reset to the original state.&lt;br /&gt;
&lt;strong&gt;Warning&lt;/strong&gt;: This will blow away all uncommitted changes! Only do this after committing everything you’ve been working on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git fetch upstream
git checkout &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; my-awesome-feature
git branch reset upstream/master &lt;span class=&quot;nt&quot;&gt;--hard&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;You’re now ready to begin working on the new branch.  If the upstream project changes in the meantime, you can run the following commands to merge in the changes without breaking history: (make sure to commit everything first)&lt;/p&gt;

  &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git fetch upstream
git rebase upstream/master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git rebase&lt;/code&gt; will rewrite the history of your local branch to include the upstream changes before your new commits.  This way, when you make the pull request, all of your commits will extend the tip of the original repository, avoiding complex merge issues.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Since this rewrites history, if you’ve already pushed the branch to your fork on GitHub, your next push will need the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-f&lt;/code&gt; flag to overwrite the existing remote history.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1 id=&quot;visual-studio-extensibility-basics&quot;&gt;Visual Studio Extensibility Basics&lt;/h1&gt;
&lt;p&gt;Visual Studio is a 10+ year old codebase built on a mix of technologies new and ancient.  Most of the older portions of Visual Studio, including the project system and the command dispatcher, are written in native code and support extensibility using ugly COM and GUIDs.  Newer parts of Visual Studio are usually written in managed code (the big exception being the new Javascript language service, which is written in C++ around IE’s Chakra engine), and are thus easier to extend.&lt;/p&gt;

&lt;p&gt;In particular, the editor was completely rewritten in Visual Studio 2010, and is implemented entirely in C# code using WPF.  This new editor uses the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/vstudio/dd460648&quot;&gt;Managed Extensibility Framework (MEF)&lt;/a&gt; to load and execute all of its internal components, making it very friendly for extensions.  You can write classes that implement interfaces from the editor and export them using MEF, and the editor will automatically import and run them when it loads appropriate documents.  Similarly, you can import interfaces from the editor to gain access to existing services like colorization and error checking for use within your extension.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2013-11-10/extending-visual-studio-part-2-core-concepts/&quot;&gt;&lt;em&gt;Next time: Core concepts&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-10-11-threads-vs-tasks.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-10-11/threads-vs-tasks/"/>
		<title type="text">Threads vs. Tasks</title>
		<updated>2013-10-11T00:00:00+00:00</updated>
		<published>2013-10-11T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="async" term="async" />
		
		
		<category scheme="https://blog.slaks.net/#" label="multi-threading" term="multi-threading" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;.Net has three low-level mechanisms to run code in parallel: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadPool&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;.  These three mechanism serve different purposes.&lt;/p&gt;

&lt;h1 id=&quot;thread&quot;&gt;Thread&lt;/h1&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt; represents an actual OS-level thread, with its own stack and kernel resources.  (technically, a CLR implementation could use fibers instead, but no existing CLR does this)  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt; allows the highest degree of control; you can &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Abort()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Suspend()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Resume()&lt;/code&gt; a thread (though this is a very bad idea), you can observe its state, and you can set thread-level properties like the stack size, apartment state, or culture.&lt;/p&gt;

&lt;p&gt;The problem with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt; is that OS threads are costly.  Each thread you have consumes a non-trivial amount of memory for its stack, and adds additional CPU overhead as the processor &lt;a href=&quot;https://en.wikipedia.org/wiki/Context_switch&quot;&gt;context-switch&lt;/a&gt; between threads.  Instead, it is better to have a small pool of threads execute your code as work becomes available.&lt;/p&gt;

&lt;p&gt;There are times when there is no alternative &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt;.  If you need to specify the name (for debugging purposes) or the apartment state (to show a UI), you must create your own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt; (note that having multiple UI threads is generally a bad idea).  Also, if you want to maintain an object that is &lt;em&gt;owned&lt;/em&gt; by a single thread and can only be used by that thread, it is much easier to explicitly create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt; instance for it so you can easily check whether code trying to use it is running on the correct thread.&lt;/p&gt;

&lt;h1 id=&quot;threadpool&quot;&gt;ThreadPool&lt;/h1&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadPool&lt;/code&gt; is a wrapper around a pool of threads maintained by the CLR.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadPool&lt;/code&gt; gives you no control at all; you can submit work to execute at some point, and you can control the size of the pool, but you can’t set anything else.  You can’t even tell when the pool will start running the work you submit to it.&lt;/p&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadPool&lt;/code&gt; avoids the overhead of creating too many threads.  However, if you submit too many long-running tasks to the threadpool, it can get full, and later work that you submit can end up waiting for the earlier long-running items to finish.  In addition, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadPool&lt;/code&gt; offers no way to find out when a work item has been completed (unlike &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread.Join()&lt;/code&gt;), nor a way to get the result.  Therefore, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadPool&lt;/code&gt; is best used for short operations where the caller does not need the result.&lt;/p&gt;

&lt;h1 id=&quot;task&quot;&gt;Task&lt;/h1&gt;
&lt;p&gt;Finally, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; class from the Task Parallel Library offers the best of both worlds.  Like the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadPool&lt;/code&gt;, a task does not create its own OS thread.  Instead, tasks are executed by a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd997402.aspx&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskScheduler&lt;/code&gt;&lt;/a&gt;; the default scheduler simply runs on the ThreadPool.&lt;/p&gt;

&lt;p&gt;Unlike the ThreadPool, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; also allows you to find out when it finishes, and (via the generic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;) to return a result.  You can call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContinueWith()&lt;/code&gt; on an existing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; to make it run more code once the task finishes (if it’s already finished, it will run the callback immediately).  If the task is generic, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContinueWith()&lt;/code&gt; will pass you the task’s result, allowing you to run more code that uses it.&lt;/p&gt;

&lt;p&gt;You can also synchronously wait for a task to finish by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Wait()&lt;/code&gt; (or, for a generic task, by getting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Result&lt;/code&gt; property).  Like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread.Join()&lt;/code&gt;, this will block the calling thread until the task finishes.  Synchronously waiting for a task is usually bad idea; it prevents the calling thread from doing any other work, and can also lead to deadlocks if the task ends up waiting (even asynchronously) for the current thread.&lt;/p&gt;

&lt;p&gt;Since tasks still run on the ThreadPool, they should not be used for long-running operations, since they can still fill up the thread pool and block new work.  Instead, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; provides a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LongRunning&lt;/code&gt; option, which will tell the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskScheduler&lt;/code&gt; to spin up a new thread rather than running on the ThreadPool.&lt;/p&gt;

&lt;p&gt;All newer high-level concurrency APIs, including the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Parallel.For*()&lt;/code&gt; methods, PLINQ, C# 5 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt;, and modern async methods in the BCL, are all built on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;The bottom line is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; is almost always the best option; it provides a much more powerful API and avoids wasting OS threads.&lt;/p&gt;

&lt;p&gt;The only reasons to explicitly create your own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread&lt;/code&gt;s in modern code are setting per-thread options, or maintaining a persistent thread that needs to maintain its own identity.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-09-29-less-css-secrets-of-the-ampersand.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-09-29/less-css-secrets-of-the-ampersand/"/>
		<title type="text">LESS: Secrets of the Ampersand</title>
		<updated>2013-09-29T00:00:00+00:00</updated>
		<published>2013-09-29T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="LESS" term="less" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;One of the less-documented features of the &lt;a href=&quot;http://lesscss.org&quot;&gt;LESS language&lt;/a&gt; is the ampersand selector, which refers to the parent selector inside a nested selector.&lt;/p&gt;

&lt;p&gt;The ampersand selector is most commonly used when applying a modifying class or pseudo-class to an existing selector:&lt;/p&gt;

&lt;div class=&quot;less&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-scss highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;:hover&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The inner selector in this example compiles to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a:hover&lt;/code&gt;.  Without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt;, it would compile to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a :hover&lt;/code&gt; (a descendant selector that matches hovered elements inside of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags), 
  which is not what the author intended.&lt;/p&gt;

&lt;p&gt;However, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; has a variety of other uses.&lt;/p&gt;

&lt;h1 id=&quot;changing-state-based-on-parent-classes&quot;&gt;Changing state based on parent classes&lt;/h1&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; can be used to make a nested selector that only applies its rules when an &lt;em&gt;ancestor&lt;/em&gt; of the outer selector has a class.  For example:&lt;/p&gt;

&lt;div class=&quot;less&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-sass highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;purple&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;.QuietMode&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;black&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This compiles to&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;purple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;.QuietMode&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;black&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This allows a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QuietMode&lt;/code&gt; class to be applied to the root element to override loud styles, while keeping the quiet and “loud” (default) styles for each element in the same place.&lt;/p&gt;

&lt;p&gt;This technique is particularly useful in mixins; it allows a mixin to to wrap a larger selector around the selector it was called in.  For example:&lt;/p&gt;

&lt;div class=&quot;less&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-sass highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.MyBox&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(@&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4px&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;darken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;.SimpleMode&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2px&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;inherit&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Using a prefix with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; allows the mixin to emit rules that are not nested in the location where the mixin was invoked, allowing it to specify higher-level filters.&lt;/p&gt;

&lt;h1 id=&quot;filtering-a-nested-selector-to-only-match-certain-elements&quot;&gt;Filtering a nested selector to only match certain elements&lt;/h1&gt;
&lt;p&gt;Similarly to the previous example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; can be used to create a nested selector (in a mixin or in another selector) that only matches a specific element.  For example:&lt;/p&gt;

&lt;div class=&quot;less&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-scss highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.quoted-source&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;#fcc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;#fdc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This compiles to&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.quoted-source&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#fcc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;.quoted-source&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#fdc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This allows you to add speciallize a class for a specific element type, while keeping the specializations nicely nested within the rest of the class.&lt;/p&gt;

&lt;h1 id=&quot;avoiding-repetition-when-selecting-repeated-elements&quot;&gt;Avoiding repetition when selecting repeated elements&lt;/h1&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; can be used to repeatedly refer to the parent selector, while keeping your code &lt;a href=&quot;https://en.wikipedia.org/wiki/Don%27t_repeat_yourself&quot; title=&quot;Don't repeat yourself&quot;&gt;DRY&lt;/a&gt;.  For example:&lt;/p&gt;

&lt;div class=&quot;less&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-sass highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.btn.btn-primary.btn-lg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;margin-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This compiles to&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.btn.btn-primary.btn-lg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.btn.btn-primary.btn-lg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.btn.btn-primary.btn-lg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;margin-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This example will add space between consecutive runs of large disabled buttons, without repeating the entire selector three times.&lt;/p&gt;

&lt;h1 id=&quot;simplifying-combinatorial-explosions&quot;&gt;Simplifying combinatorial explosions&lt;/h1&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; can be used to easily generate every possible permutation of selectors for use with combinators.  For example:&lt;/p&gt;

&lt;div class=&quot;less&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-scss highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;gray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nl&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This code will select all paragraph-like elements and add an upper border, then remove that border from such elements that already have one immediately preceding it.&lt;/p&gt;

&lt;p&gt;The ampersands expand into all 16 possible combinations of these elements, without needing to type them by hand.  These 6 simple lines expand into 24 lines or raw CSS:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;gray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;blockquote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This technique works because LESS recognizes the comma combinator and expands nested selectors for each alternative.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-09-03-traditional-inheritance-in-javascript.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-09-03/traditional-inheritance-in-javascript/"/>
		<title type="text">Traditional OOP Inheritance in Javascript</title>
		<updated>2013-09-03T00:00:00+00:00</updated>
		<published>2013-09-03T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="oop" term="oop" />
		
		
		<category scheme="https://blog.slaks.net/#" label="inheritance" term="inheritance" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Node.js" term="node-js" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;em&gt;Javasript is not a traditionally object-oriented programming languague&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Wikipedia &lt;a href=&quot;https://en.wikipedia.org/wiki/Javascript&quot;&gt;describes Javascript&lt;/a&gt; as a ”scripting, object-oriented (prototype-based), imperative, functional“ language.  However, since most developers prefer to use classical (pun intended) object-oriented patterns, people have come up with ways to use Javascript with traditional OOP techniques, including classes and inheritance.&lt;/p&gt;

&lt;p&gt;Most Javascript developers are by now familiar with the standard technique for Javascript classes.  For example:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;legs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Leg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;LF&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Leg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;RF&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Leg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;LB&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Leg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;RB&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;summon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;ttsEngine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;speak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;walk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Complicated code involving this.legs()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myCow&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Betty&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The obvious way to add inheritance is to create a new function for the derived class, and set its prototype to an instance of the base class:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// This code is wrong!&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Abstract&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;legs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Child class must populate&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;walk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Complicated code involving this.legs()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;legs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pouch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;skippy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are a number of problems with this code:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kangaroo.prototype.constructor&lt;/code&gt; will be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kangaroo&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Had the base &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt; constructor taken parameters, this technique wouldn’t pass be able to those parameters on a per-instance basis&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Every instance of the derived class will share the same array instance.  After creating two more &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kangaroo&lt;/code&gt; instances, they will appear to have 6 legs each.&lt;br /&gt;
More generall, since the base constructor is only executed once, any mutable state that it creates will be shared by every derived instance.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first two problems are relatively easy to solve.&lt;/p&gt;

&lt;p&gt;To solve the first problem, we can simply add&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;However, this is not quite good enough.  This will create an &lt;em&gt;enumerable&lt;/em&gt; property, meaning that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for in&lt;/code&gt; loops over instance of the derived class will include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constructor&lt;/code&gt; property.  This behavior is contrary to the default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constructor&lt;/code&gt; property, and is usually unexpected.&lt;/p&gt;

&lt;p&gt;We can properly fix this by configuring the property using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.defineProperty()&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;defineProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
	&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
		&lt;span class=&quot;na&quot;&gt;configurable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;na&quot;&gt;enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;na&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.defineProperty()&lt;/code&gt; is &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Browser_compatibility&quot;&gt;not supported by IE 8&lt;/a&gt; (or earlier); this problem cannot be fixed in that browser.&lt;/p&gt;

&lt;p&gt;We can fix the second problem by explicitly calling the base constructor, making sure that it receives the correct &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; the Kangaroo&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To fix the third problem, we need to make a new object inheriting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal.prototype&lt;/code&gt; to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kangaroo&lt;/code&gt;’s prototype, without actually calling the constructor.  This is what the ES5 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.create()&lt;/code&gt; function does:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.create()&lt;/code&gt; takes an optional map of property descriptors, we can combine this with the second fix:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
			&lt;span class=&quot;na&quot;&gt;configurable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;na&quot;&gt;enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;na&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Kangaroo&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is now almost equivalent to the standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prototype&lt;/code&gt; created for each function, as detailed in the spec in &lt;a href=&quot;https://es5.github.io/#x13.2&quot; title=&quot;Creating Function Objects&quot;&gt;section 13.2&lt;/a&gt; (point 17).&lt;/p&gt;

&lt;p&gt;We can move all of this code into a helper function:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;superConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;subConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;superConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
				&lt;span class=&quot;na&quot;&gt;configurable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
				&lt;span class=&quot;na&quot;&gt;enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
				&lt;span class=&quot;na&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
				&lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;subConstructor&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We can then use it like this, making sure to call the superclass constructor inside the subclass constructor (that cannot be done automatically):&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// This is the right way&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Abstract&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;legs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;		&lt;span class=&quot;c1&quot;&gt;// Child class must populate&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;walk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Complicated code involving this.legs()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Octopus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; the Octopus&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;legs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Leg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Octopus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Octopus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Otto&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This function is exactly the Node.js &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;util.inherits()&lt;/code&gt; function does, except that it also adds a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super_&lt;/code&gt; (static) property to the subclass constructor.  For more details, see its &lt;a href=&quot;https://github.com/joyent/node/blob/546ae2ee/lib/util.js#L552-L575&quot;&gt;source&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This still leaves one more difference from the standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prototype&lt;/code&gt; property; namely, the spec defines the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prototype&lt;/code&gt; property as non-enumerable.  We can fix that with another call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;defineProperty&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;superConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;proto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;superConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
				&lt;span class=&quot;na&quot;&gt;configurable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
				&lt;span class=&quot;na&quot;&gt;enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
				&lt;span class=&quot;na&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
				&lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;subConstructor&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;defineProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
		&lt;span class=&quot;na&quot;&gt;configurable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;na&quot;&gt;enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;na&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;proto&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This function creates a new prototype that fully complies to the spec.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-08-09-jekyll-tag-was-never-closed.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-08-09/jekyll-tag-was-never-closed/"/>
		<title type="text">Jekyll bug: Tag was never closed</title>
		<updated>2013-08-09T00:00:00+00:00</updated>
		<published>2013-08-09T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Jekyll" term="jekyll" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Liquid" term="liquid" />
		
		
		<category scheme="https://blog.slaks.net/#" label="bugs" term="bugs" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;p&gt;After upgrading to Jekyll 1.1, you may notice that posts that used to work fine now give an error like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tag was never closed&lt;/code&gt;.  These errors can appear for no apparent reason; the tags will appear to be correctly closed.&lt;/p&gt;

&lt;p&gt;This error occurs if the first blank line in the post is inside a Jekyll block (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% highlight %}&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The bug is caused by a change in Jekyll’s post excerpt support.&lt;/p&gt;

&lt;p&gt;As of version 1.0, Jekyll creates an &lt;a href=&quot;https://github.com/mojombo/jekyll/issues/837&quot;&gt;excerpt&lt;/a&gt; for every post in your site, containing the first paragraph of text in that post (this allows post listings to display snippets of content).  By default, the excerpt contains all text until the first blank line (double-newline); this separator can be changed by setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;excerpt_separator&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_config.yml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The problem came in Jekyll 1.1.0, which &lt;a href=&quot;https://github.com/mojombo/jekyll/pull/1302&quot;&gt;parses Jekyll tags in post excerpts&lt;/a&gt;.  Now, after extracting the first paragraph of text, the excerpt is parsed as Liquid markup so that tags in the excerpt work correctly.  However, if you wrote posts without specifically designing them for excerpts, the excerpt can end in the middle of a Liquid tag, resulting in this error.&lt;/p&gt;

&lt;p&gt;Here is an example of the bug:&lt;/p&gt;

&lt;div class=&quot;language-liquid highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
title: &quot;Buggy post&quot;
---
This is an example of a post with some code:
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;highlight&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;html&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;endhighlight&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Jekyll will extract the following excerpt:&lt;/p&gt;

&lt;div class=&quot;language-liquid highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;This is an example of a post with some code:
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;highlight&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;html&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This excerpt will result in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Liquid Exception: highlight tag was never closed in buggy-post.txt&lt;/code&gt;, since the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% highlight %}&lt;/code&gt; tag is never closed.&lt;/p&gt;

&lt;p&gt;To fix this, you can either fix every post so that the excerpt is valid Liquid markup, or prevent Jekyll from generating the excerpts in the first place.&lt;br /&gt;
Jekyll does not &lt;a href=&quot;https://github.com/mojombo/jekyll/pull/1386&quot;&gt;yet&lt;/a&gt; have a way to disable excerpts entirely, so the next-best option is to configure it so that the excerpts are always valid Liquid.&lt;/p&gt;

&lt;p&gt;You can do this by setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;excerpt_separator&lt;/code&gt; to a nonsense string that never appears in your posts, so that the excerpt will include the entire post (which is already known to be valid markup).
Better yet, you can set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;excerpt_separator&lt;/code&gt; to an empty string, so that the excerpt will end immediately.  This will reduce the amount of work that Jekyll needs to do, making your site build slightly faster.&lt;/p&gt;

&lt;p&gt;In short, this bug can be fixed by adding the following line to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_config.yml&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;excerpt_separator&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Workaround for https://blog.slaks.net/2013-08-09/jekyll-tag-was-never-closed/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once &lt;a href=&quot;https://github.com/mojombo/jekyll/pull/1386&quot;&gt;#1386&lt;/a&gt; is released, this line will disable excerpts entirely, adding another miniscule performance boost.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-07-29-creating-lock-free-data-structures.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-07-29/creating-lock-free-data-structures/"/>
		<title type="text">Immutability, part 4: Building lock-free data structures</title>
		<updated>2013-07-29T00:00:00+00:00</updated>
		<published>2013-07-29T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="lock-free" term="lock-free" />
		
		
		<category scheme="https://blog.slaks.net/#" label="multi-threading" term="multi-threading" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;As I mentioned &lt;a href=&quot;/2013-07-22/thread-safe-data-structures/&quot;&gt;last time&lt;/a&gt;, the best way to create simple thread-safe lock-free data structures is with compare-and-swap loops.&lt;/p&gt;

&lt;p&gt;To recap, this technique works as follows:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;Fetch the current value of the field into a local variable&lt;/li&gt;
    &lt;li&gt;Run your actual logic to generate a new immutable object based on the current value (eg, push an item onto an immutable stack)&lt;/li&gt;
    &lt;li&gt;Use the atomic &lt;a href=&quot;https://en.wikipedia.org/wiki/Compare-and-swap&quot;&gt;compare-and-swap&lt;/a&gt; operation to set the field to the new value if and only if no other thread has changed it since step 1.&lt;/li&gt;
    &lt;li&gt;If a different thread has changed the object (if the compare-and-swap failed), go back to step 1 and try again.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;In C#, this algorithm looks like this:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;/// &amp;lt;summary&amp;gt;Holds a reference to an immutable class and updates it atomically.&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// &amp;lt;typeparam name=&quot;T&quot;&amp;gt;An immutable class to reference.&amp;lt;/typeparam&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AtomicReference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AtomicReference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initialValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initialValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;c1&quot;&gt;/// &amp;lt;summary&amp;gt;Gets the current value of this instance.&amp;lt;/summary&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	
	&lt;span class=&quot;c1&quot;&gt;/// &amp;lt;summary&amp;gt;Atomically updates the value of this instance.&amp;lt;/summary&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;/// &amp;lt;param name=&quot;mutator&quot;&amp;gt;A pure function to compute a new value based on the current value of the instance.&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;/// This function may be called more than once.&amp;lt;/param&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;/// &amp;lt;returns&amp;gt;The previous value that was used to generate the resulting new value.&amp;lt;/returns&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Mutate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mutator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseVal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newVal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mutator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;baseVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pragma&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warning&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disable&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;420&lt;/span&gt;	
			&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentVal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Interlocked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CompareExchange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pragma&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warning&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;restore&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;420&lt;/span&gt;

			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentVal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;			&lt;span class=&quot;c1&quot;&gt;// Success!&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;baseVal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Try again&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This class encapsulates a mutable reference to an inner type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;, which must be immutable.  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mutate()&lt;/code&gt; function can be called to assign a new value based on the previous value, and will keep calling its callback until the current value does not change underneath it, as described above.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#pragma warning&lt;/code&gt; directive is necessary to suppress a false positive about passing a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; field as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ref&lt;/code&gt; parameter.  Ordinarily, passing a field by reference will not preserve the field’s volatility, but the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Interlocked&lt;/code&gt; methods are an exception to this rule.  This is noted in the warning’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/4bw5ewxy&quot;&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Using this class with my earlier immutable stack, one can easily implement a thread-safe lock free mutable stack:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AtomicStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AtomicReference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; 
		&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AtomicReference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Mutate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oldStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Mutate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oldStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pop()&lt;/code&gt; methods call their respective counterparts on the inner immutable stack, wrapped in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mutate()&lt;/code&gt; operation so that they keep trying until the mutation succeeds.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Peek()&lt;/code&gt; simply gets the current value of the reference and peeks at the top of that stack.  It is important to note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stack.Value&lt;/code&gt; can change at any time.  Had this method needed to access the value twice, it would need to cache the current value of the field to prevent it from changing between two operations.&lt;br /&gt;
For example:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PeekTwice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Compare-and-swap loops are the most efficient way to implement simple atomic lock-free data structures.  However, writing thread-safe code is still intrinsically complicated; you still need to make sure to correctly use atomic operations and not operate on data that may change underneath you.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-07-22-thread-safe-data-structures.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-07-22/thread-safe-data-structures/"/>
		<title type="text">Immutability, part 3: Writing thread-safe data structures</title>
		<updated>2013-07-22T00:00:00+00:00</updated>
		<published>2013-07-22T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="thread-safety" term="thread-safety" />
		
		
		<category scheme="https://blog.slaks.net/#" label="lock-free" term="lock-free" />
		
		
		<category scheme="https://blog.slaks.net/#" label="multi-threading" term="multi-threading" />
		
		
		<category scheme="https://blog.slaks.net/#" label="concepts" term="concepts" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2013-06-23/creating-immutable-stack/&quot;&gt;Last time&lt;/a&gt;, I showed how to create a simple &lt;a href=&quot;/2013-06-28/covariant-immutable-stack/&quot;&gt;covariant&lt;/a&gt; immutable stack.&lt;/p&gt;

&lt;p&gt;One of the biggest attractions of immutable types is the ease with which they can be used to write thread-safe code.  However, in the real world, things usually need to mutate.&lt;/p&gt;

&lt;p&gt;How can this be done safely?&lt;/p&gt;

&lt;p&gt;For an object to be safe, it must not be possible to observe it in an inconsistent state (as long as you follow its documented rules).  For example, it should not be possible to see a collection with a hole in it from the middle of a resize.&lt;/p&gt;

&lt;p&gt;For non-thread-safe objects, this is not very hard.  As long as each function call leaves the object in a consistent state, it doesn’t take much effort to enforce this.  It does mean that you must only raise events (or fire callbacks) when the object is in a consistent state.  More subtly, you must only call virtual methods when in a consistent state, since a derived class can call arbitrary code outside the class.  (this is why people criticize Java’s approach of making all methods virtual by default).&lt;br /&gt;
Since the object is documented (or assumed to be non-thread-safe, there is no need to ensure that other threads always see a consistent state.&lt;/p&gt;

&lt;p&gt;For fully thread-safe objects, this requirement becomes much more difficult.  Because other threads can interact with your object at any time, it must &lt;em&gt;never&lt;/em&gt; be in an inconsistent state.  There are a number of ways to make this work.&lt;/p&gt;

&lt;h2 id=&quot;atomic-operations&quot;&gt;Atomic operations&lt;/h2&gt;
&lt;p&gt;Most instruction sets provide a couple of instructions which execute atomically (such as atomic increment or compare-and-swap).  These instructions are inherently thread-safe; the CPU guarantees that no other thread can see inconsistent results.  However, if your class is anything more complicated than a simple counter, this won’t help.&lt;/p&gt;

&lt;p&gt;If you’re writing higher-level code, you can also use existing higher-level atomic operations, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ConcurrentDictionary&lt;/code&gt;.  These operations are in turn implemented using the other techniques described in this article.&lt;/p&gt;

&lt;h2 id=&quot;locks&quot;&gt;Locks&lt;/h2&gt;
&lt;p&gt;This is the simplest approach.  If you wrap every public method in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lock()&lt;/code&gt; statement, other threads can never observe inconsistent state, since they can only start executing after all method calls finish.  This is the approach taken by Java’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Collections.synchronized*()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;However, this approach &lt;a href=&quot;https://en.wikipedia.org/wiki/Lock_%28computer_science%29#Disadvantages&quot;&gt;has a number of problems&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It’s slow.  To guarantee safety, every method must prevent every other method from running, even for methods that would be safe to run concurrently, in case a third, unsafe, method is running too.  This can be mitigated with ReaderWriterLocks, at the cost of additional complexity.&lt;br /&gt;
In addition, entering a lock is a kernel-level operation that comes with its own costs.&lt;/li&gt;
  &lt;li&gt;It’s prone to deadlocks if you lock on an externally visible object (such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;). &lt;br /&gt;
If thread A runs some other code that locks on the same object that you’re locking on, then starts waiting for some other lock, thread B can end up calling your function while holding that other lock.  Thread B will then wait for thread A to finish before it runs your code, which won’t happen because thread A is stuck waiting for the lock that thread B is already holding.&lt;/li&gt;
  &lt;li&gt;It’s very prone to deadlocks if your code is re-entrant.&lt;br /&gt;
If thread A calls your code, then tries to grab some other lock in a re-entrant callback, but thread B is already holding that other lock and is now trying to enter your code, you will get a deadlock.  This problem is difficult to fully prevent when writing reusable re-entrant code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;compare-and-swap-loops&quot;&gt;Compare-and-swap loops&lt;/h2&gt;

&lt;p&gt;To avoid locks, you must only use primitive atomic operations.  This is where immutable data structures really shine.  If you put all of the state you need to change in a single immutable object, you can make all of the changes from an ‘offline’ reference to the object, then atomically swap in your new copy if no-one else has changed it.  This technique works as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Fetch the current value of the field into a local variable&lt;/li&gt;
  &lt;li&gt;Run your actual logic to generate a new immutable object based on the current value (eg, push an item onto an immutable stack)&lt;/li&gt;
  &lt;li&gt;Use the atomic &lt;a href=&quot;https://en.wikipedia.org/wiki/Compare-and-swap&quot;&gt;compare-and-swap&lt;/a&gt; operation to set the field to the new value if and only if no other thread has changed it since step 1.&lt;/li&gt;
  &lt;li&gt;If a different thread has changed the object (if the compare-and-swap failed), go back to step 1 and try again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is important that the object being swapped be fully immutable.  Otherwise, changes to parts of the object from other threads can go unnoticed during the atomic replacement, leading to the &lt;a href=&quot;https://en.wikipedia.org/wiki/ABA_problem&quot;&gt;ABA problem&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Using deeply immutable objects prevents this from causing a problem.  If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; is truly immutable, there can be nothing wrong with missing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; that happened in the middle; if there was any change that wasn’t fully undone, the new value wouldn’t be the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This technique only works if the mutation you’re trying to perform (step 2) is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Pure_function&quot;&gt;pure function&lt;/a&gt;.  Since the operation can run more than once, any side effects that it has can happen multiple times, which will probably break your program.  Similarly, it must be thread-safe (which pure functions implicitly are), since nothing is preventing it from running on multiple threads concurrently.&lt;/p&gt;

&lt;p&gt;If the function does have side-effects, you can still use this technique by reverting those side effects if the compare-and-swap failed (in step 4).  This way, the side effects from each invocation will be cancelled before the next try.  However, this can only help if the function and its inverse (to revert changes) are both thread-safe, and if you can guarantee that the brief window of time before the side-effects of a failed attempt are reverted won’t cause trouble.  If not, the only solution is a normal lock.&lt;/p&gt;

&lt;h2 id=&quot;make-every-state-consistent&quot;&gt;Make every state “consistent”&lt;/h2&gt;

&lt;p&gt;Compare-and-swap loops are the ideal way to implement lock-free mutable data structures.  However, compare-and-swap can only operate on one thing at a time.  If you need to mutate two separate fields, you need more complicated techniques.&lt;/p&gt;

&lt;p&gt;If you’re mutating two independent fields, compare-and-swap can still be used.  You can simply put all of the state you’re modifying into a single immutable class, then perform all of the mutations together inside a single compare-and-swap loop.&lt;/p&gt;

&lt;p&gt;If you’re mutating two fields that are connected to each-other, this isn’t possible.  A lock-free queue is a good example of this.  A queue needs a head field that points to the first node (for dequeue), and a tail field that points to the last node (for enqueue).&lt;/p&gt;

&lt;p&gt;The dequeue operation can be built using an ordinary compare-and-swap loop just like a stack; create a new node that points to the current head and swap out the old head for it.&lt;/p&gt;

&lt;p&gt;However, enqueuing an item has two discrete steps.  First, you need to set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next&lt;/code&gt; pointer of the current tail node to point to the new node, so that it can be dequeued when the head reaches the current tail.  Then, you need to set the tail field to the new node, so that the next enqueue operation starts from the new node.&lt;/p&gt;

&lt;p&gt;These two operations cannot be done in the same compare-and-swap loop, since both fields need to be set atomically.  Otherwise, another thread can start inserting a second new element before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail&lt;/code&gt; is updated, and one of the nodes will be swallowed.&lt;/p&gt;

&lt;p&gt;The obvious way to fix this would be to use a lock to prevent other threads from enqueuing an item between these two steps.  However, you can also do this in a lock-free fashion.  You simply need to make sure that if another thread runs between the two operations, it will first finish the job that the previous thread started.  In other words, before inserting any new node, check whether there is a half-inserted node from another thread and finish inserting it first.&lt;/p&gt;

&lt;p&gt;This technique looks roughly like this&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use a compare-and-swap loop to set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail&lt;/code&gt; field to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail.next&lt;/code&gt;, to finish a half-done insertion from a different thread.&lt;/li&gt;
  &lt;li&gt;Use a compare-and-swap loop to set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail.next&lt;/code&gt; to the new node (only if no other thread has set it to a different node at the same time)&lt;/li&gt;
  &lt;li&gt;Use a compare-and-swap loop to set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail&lt;/code&gt; to the newly-inserted node, but only if no other thread did so in the meantime from step 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Steps 1 and 2 must be done in the same loop in case a third thread inserts a second item after this thread finishes an earlier pending insertion.&lt;/p&gt;

&lt;p&gt;The dequeue operation then becomes much more complicated, because it needs to work correctly in all three states.  For more information, see Julian Bucknall’s &lt;a href=&quot;http://www.boyet.com/Articles/LockfreeQueue.html&quot;&gt;detailed implementation of a lock-free concurrent queue&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The basic idea is to design the public methods to be able to run successfully from any intermediate state caused by another thread.  This technique can be very difficult to implement correctly for complex operations.&lt;/p&gt;

&lt;p&gt;As with simple compare-and-swap loops, the individual mutations must be either pure or safely revertible.&lt;/p&gt;

&lt;h1 id=&quot;final-notes&quot;&gt;Final notes&lt;/h1&gt;

&lt;p&gt;The idea behind all of these techniques is to either handle or prevent other threads that modify your object while your operation is running.  When using thread-safe objects, you must bear the same requirement in mind.  Any time you perform two operations on an object, it is your responsibility to ensure that nothing has changed between the two operations.&lt;/p&gt;

&lt;p&gt;To aid in this, well-designed concurrent classes will offer composite operations that perform common tasks atomically, such as .Net’s &lt;a href=&quot;https://https://msdn.microsoft.com/en-us/library/ee378677&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ConcurrentDictionary.GetOrAdd()&lt;/code&gt; method&lt;/a&gt;) (this is &lt;a href=&quot;https://stackoverflow.com/a/12182099/34397&quot;&gt;one of the problems&lt;/a&gt; with Java’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Collections.synchronized*()&lt;/code&gt; wrappers).  In the absence of such methods, you will still need to use locks to ensure that no other mutations interrupt your operation.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2013-07-29/creating-lock-free-data-structures/&quot;&gt;&lt;em&gt;Next time: How to build thread-safe lock-free data structures using compare-and-swap loops&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-06-28-covariant-immutable-stack.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-06-28/covariant-immutable-stack/"/>
		<title type="text">Immutability, part 2.5: Adding covariance to the immutable stack</title>
		<updated>2013-06-28T00:00:00+00:00</updated>
		<published>2013-06-28T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="variance" term="variance" />
		
		
		<category scheme="https://blog.slaks.net/#" label="oop" term="oop" />
		
		
		<category scheme="https://blog.slaks.net/#" label="thread-safety" term="thread-safety" />
		
		
		<category scheme="https://blog.slaks.net/#" label="immutability" term="immutability" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2013-06-23/creating-immutable-stack/&quot;&gt;Last time&lt;/a&gt;, I showed how to create a simple immutable stack.  However, this stack is not &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd799517.aspx&quot;&gt;covariant&lt;/a&gt;.  Ideally, we should be able (for example) to implicitly convert &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;string&amp;gt;&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;object&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Most collection types cannot be co-variant.  Had &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;string&amp;gt;&lt;/code&gt; been convertible to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;object&amp;gt;&lt;/code&gt;, you would then be able to add an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; (or any other type) to the converted &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;object&amp;gt;&lt;/code&gt;, even though it can’t fit in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;string&amp;gt;&lt;/code&gt; (which the casted instance actually is).  To be precise, covariance is only type-safe for immutable types.&lt;/p&gt;

&lt;p&gt;Since our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack&amp;lt;T&amp;gt;&lt;/code&gt; class is immutable, we should be able to simply change it to &lt;code&gt;public interface Stack&amp;lt;&lt;b&gt;out &lt;/b&gt;T&amp;gt;&lt;/code&gt; and get co-variance instantly.&lt;/p&gt;

&lt;p&gt;In practice, it’s not so simple.  Even though the class is immutable, it still has a method that takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; as a parameter (namely, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push(T)&lt;/code&gt;).  Even though the method doesn’t mutate anything, it still wouldn’t be type safe:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stringStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stringStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// This is PersistentStack&amp;lt;string&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This isn’t safe because covariance is a property of the &lt;em&gt;interface&lt;/em&gt;, not the implementing class.  Even when casted to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;object&amp;gt;&lt;/code&gt;, the instance is still an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;string&amp;gt;&lt;/code&gt;, and therefore its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push(T)&lt;/code&gt; method can still only take a string.  Similarly, the return value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; would still have been &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;string&amp;gt;&lt;/code&gt;, which would be implicitly converted to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;object&amp;gt;&lt;/code&gt; to fit in the variable.&lt;/p&gt;

&lt;p&gt;The actual behavior that we want for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; is a little more subtle.  If we have an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;string&amp;gt;&lt;/code&gt;, we should be able to push &lt;em&gt;any&lt;/em&gt; object onto it, resulting in a new stack whose type is the least common ancestor between the type of the original stack and the type that we pushed.  For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack&amp;lt;Button&amp;gt;.Push(new TextBox())&lt;/code&gt; should return a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack&amp;lt;Control&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, the desired signature for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; would look like this:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// Other members...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In other words, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; can return a stack of any supertype of our element type.  Unfortunately, this won’t work; C# (unlike Java) does not support &lt;em&gt;upper&lt;/em&gt; bounds on a generic type parameter.  You can write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&amp;lt;T&amp;gt; where T : SomeType&lt;/code&gt;, but not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&amp;lt;T&amp;gt; where SomeType : T&lt;/code&gt;.&lt;br /&gt;
Java, by contrast, would allow us to write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack&amp;lt;U&amp;gt; &amp;lt;U super T&amp;gt; push(U element);&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, all is not lost.  Although we can’t constrain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; based on the parent type’s type parameter, we can constrain it on &lt;em&gt;its own&lt;/em&gt; type parameter.  Specifically, we can make it an extension method:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In fact, once we move it out of the class itself, we don’t even need a second type parameter.  Since the stack is covariant, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;T&amp;gt;&lt;/code&gt; parameter is convertible to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;U&amp;gt;&lt;/code&gt;, so we can simplify it to take just one generic parameter:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This way, you can write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stringStack.Push(new object())&lt;/code&gt;, and the compiler will infer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt;, then covariantly convert the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;string&amp;gt;&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&amp;lt;object&amp;gt;&lt;/code&gt; to pass as the first parameter.&lt;/p&gt;

&lt;p&gt;Note that C#’s type inference algorithm will not attempt to find the least-common-ancestor when presented with two types (unless one is a descendant of the other), so you would need to specify the type parameter explicitly: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStack.Push&amp;lt;Control&amp;gt;(new TextBox())&lt;/code&gt;.  Rather, it can only infer ancestral relationships can be inferred, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStack.Push(new Control())&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The only remaining problem is that the extension method must be specific to one implementation.  It can extend (and return) the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IStack&lt;/code&gt; interface, but it must create a specific implementing type.  There is no elegant type-safe way for it to return whatever implementing type was passed to it.&lt;/p&gt;

&lt;p&gt;Here is the code for a fully covariant immutable stack:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PersistentStackExtensions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TNew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TNew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TNew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TNew&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TNew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;LinkNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Empty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;EmptyNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EmptyNode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InvalidOperationException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Stack is empty&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
			&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InvalidOperationException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Stack is empty&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;internal&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LinkNode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LinkNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stringStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stringStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stringStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stringStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The private &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PersistentStack&amp;lt;T&amp;gt;.LinkNode&lt;/code&gt; class now needs to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;internal&lt;/code&gt; so that the extension method can instantiate it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2013-07-22/thread-safe-data-structures/&quot;&gt;&lt;em&gt;Next time: Swapping immutable objects without losing thread-safety&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-06-23-creating-immutable-stack.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-06-23/creating-immutable-stack/"/>
		<title type="text">Immutability, part 2: Creating a simple immutable stack</title>
		<updated>2013-06-23T00:00:00+00:00</updated>
		<published>2013-06-23T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="oop" term="oop" />
		
		
		<category scheme="https://blog.slaks.net/#" label="thread-safety" term="thread-safety" />
		
		
		<category scheme="https://blog.slaks.net/#" label="immutability" term="immutability" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;/2013-06-11/readonly-vs-immutable/&quot;&gt;Last time&lt;/a&gt;, I explained the basic meaning of immutability.  The simplest useful example of an immutable class is an immutable stack.  Immutable stacks work just like regular stacks – with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pop()&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Peek()&lt;/code&gt; methods – except that instead of mutating the original instance, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pop()&lt;/code&gt; return a new, modified, instance.&lt;/p&gt;

&lt;p&gt;In code, that looks like&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;myStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;myStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Each implementation of this interface would supply a singleton &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt; instance; since they are immutable; there is no need to have more than one empty instance.  Thus, there is no need for a constructor.  Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pop()&lt;/code&gt; needs to return the new stack, it cannot return the removed item; therefore, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Peek()&lt;/code&gt; is the only way to get the item.&lt;/p&gt;

&lt;p&gt;As a side benefit, this pattern naturally enables and encourages &lt;a href=&quot;https://en.wikipedia.org/wiki/Fluent_interface&quot;&gt;fluent interfaces&lt;/a&gt;, since each mutation methods returns a new instance.  (eg, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myStack.Push(1).Push(2).Push(3)&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;The naive implementation of this interface would use a list, and copy the list when creating a new stack:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NaiveStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReadOnlyCollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NaiveStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReadOnlyCollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NaiveStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Empty&lt;/span&gt; 
		&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NaiveStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NaiveStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InvalidOperationException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Stack is empty&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;RemoveAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NaiveStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
		&lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The problem with this approach is that each mutation requires an O(n) copy to create the list behind the new instance.  This makes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pop()&lt;/code&gt; awfully slow, and vastly increases the memory footprint until the intermediate instances can be GCd.&lt;/p&gt;

&lt;p&gt;To solve these problems, we can design the stack as a &lt;a href=&quot;https://en.wikipedia.org/wiki/Persistent_data_structure&quot;&gt;persistent data structure&lt;/a&gt;.  Instead of copying anything when pushing on to a stack, we cab make the new stack instance hold only the newly added item, and maintain a reference to the previous instance storing the rest of the items.  Popping from the stack can then simply return the previous instance.  Basically, the stack becomes an immutable single-linked list.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Empty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;EmptyNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LinkNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EmptyNode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InvalidOperationException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Stack is empty&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
			&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InvalidOperationException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Stack is empty&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LinkNode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersistentStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LinkNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Peek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsEmpty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this implementation, the empty and non-empty nodes are different enough that I decided to use polymorphism rather than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; statements.  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PersistentStack&amp;lt;T&amp;gt;&lt;/code&gt; type contains the only common logic (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt;); everything else is implement by the two concrete classes.&lt;/p&gt;

&lt;p&gt;Here, the empty stack truly is a singleton; only one instance will ever be created for each element type.  It can only be used as a starting point to create new stacks; the other methods simply throw exceptions.  It also serves as the final node in the chain for all non-empty stacks.&lt;/p&gt;

&lt;p&gt;Pushing onto any stack (empty or not) returns a new node that holds the new item and points to the previous stack; popping a non-empty stack simply returns that reference.&lt;/p&gt;

&lt;p&gt;Like other linked lists, this approach implements both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Push()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pop()&lt;/code&gt; in O(1) in both memory and time.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2013-06-28/covariant-immutable-stack/&quot;&gt;&lt;em&gt;Next time: Adding covariance&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-06-11-readonly-vs-immutable.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-06-11/readonly-vs-immutable/"/>
		<title type="text">Immutability, part 1: Read-only vs. Immutable</title>
		<updated>2013-06-11T00:00:00+00:00</updated>
		<published>2013-06-11T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="oop" term="oop" />
		
		
		<category scheme="https://blog.slaks.net/#" label="thread-safety" term="thread-safety" />
		
		
		<category scheme="https://blog.slaks.net/#" label="immutability" term="immutability" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;A &lt;em&gt;read-only&lt;/em&gt; object is an object that does not expose any way to change it.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReadOnlyCollection&amp;lt;T&amp;gt;&lt;/code&gt; (returned by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AsReadOnly()&lt;/code&gt;) is a good example).  However, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; is also read-only.&lt;br /&gt;
The important distinction is that read-only objects are allowed to change.&lt;/p&gt;

&lt;p&gt;If you write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list.Add(new Test())&lt;/code&gt;, your read-only collection (which just wraps &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt;) will have changed.&lt;/p&gt;

&lt;p&gt;Read-only collections are useful for designing safe APIs, where only the owner of the collection is allowed to change it.&lt;br /&gt;
However, it won’t do any good for thread-safety.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;An &lt;strong&gt;immutable&lt;/strong&gt; object is an object that cannot change &lt;em&gt;at all&lt;/em&gt;, no matter what happens (Reflection doesn’t count).  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; is an excellent example of an immutable class; the value of an existing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; instance can never change, no matter what happens.  (barring reflection, unsafe code, and certain marshalling tricks)&lt;/p&gt;

&lt;p&gt;.Net does not have any built-in immutable collections, but the CLR team &lt;a href=&quot;https://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable-collections-released-on-nuget.aspx&quot;&gt;released a library of immutable collection types on NuGet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Truly immutable objects are intrinsically thread-safe.  Since there is no way to modify them, there is no chance that other threads will observe an inconsistent instance.&lt;/p&gt;

&lt;p&gt;In summary, there are four kinds of “unchangeable” things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readonly&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReadOnly&lt;/code&gt; in VB.Net; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;final&lt;/code&gt; in Java) fields.&lt;br /&gt;
These fields cannot be re-assigned to point to a different instance.   However, nothing prevents you from mutating the instance pointed to by the field.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Read-only classes&lt;br /&gt;
These classes do not expose any APIs to mutate their contents, but can change by other means (typically, by changes made directly to the mutable objects they wrap, or in response to events handled within the class).&lt;br /&gt;
Examples include &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms132474.aspx&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReadOnlyCollection&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;API-enforced immutable classes&lt;br /&gt;
These classes are not intrinsically immutable, but have an API design that guarantees that mutations will not happen.  In other words, they may contain mutable state, but they will never actually mutate that state.
Examples include delegates (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MulticastDelegate&lt;/code&gt; has an array of handlers, which it will never mutate).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Compiler-enforced immutable classes&lt;br /&gt;
These classes only contain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readonly&lt;/code&gt; fields that are themselves immutable.  This means that the compiler will report an error if the developer mistakenly tries to mutate it.  This provides an additional layer of defence against potential mistakes.&lt;br /&gt;
Examples include most &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EventArgs&lt;/code&gt; classes.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, an object is &lt;em&gt;deeply&lt;/em&gt; immutable (or read-only) if both it and all of its properties are also deeply immutable (or read-only).  In other words, the entire graph of objects reachable from it must be immutable (or read-only).&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2013-06-23/creating-immutable-stack/&quot;&gt;&lt;em&gt;Next time: Creating a simple immutable stack&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-06-10-jekyll-endraw-in-code.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-06-10/jekyll-endraw-in-code/"/>
		<title type="text">Writing the endraw tag in Jekyll code blocks</title>
		<updated>2013-06-10T00:00:00+00:00</updated>
		<published>2013-06-10T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Jekyll" term="jekyll" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Liquid" term="liquid" />
		
		
		<category scheme="https://blog.slaks.net/#" label="jekyll-hacks" term="jekyll-hacks" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;p&gt;&lt;a href=&quot;/2013-06-09/writing-about-jekyll-in-jekyll/&quot;&gt;Last time&lt;/a&gt;, we saw how to write about Jekyll tags in Jekyll-based blog posts, using HTML entities or the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;These techniques cannot be used in syntax-highlighted code blocks (Jekyll’s &lt;code&gt;&amp;#123;% higlight %}&lt;/code&gt; tag or a Markdown code block), since such blocks are always HTML-escaped.  Instead, you can wrap all of the code in the block with a Liquid &lt;code&gt;&amp;#123;% raw %}&lt;/code&gt; tag.  Since the Liquid tag is processed before Markdown or syntax highlighting, this works perfectly.&lt;/p&gt;

&lt;div class=&quot;language-liquid highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
Liquid uses tags like {% if %} or {% for %}.
It also supports variable interpolation: {{ someVariable }}
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;endraw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This approach works wonderfully, until you try to put the &lt;code&gt;&amp;#123;% endraw %}&lt;/code&gt; tag in a code block.  You can’t put that in a &lt;code&gt;&amp;#123;% raw %}&lt;/code&gt; block, because it will close the block.  You can’t use entities, because text within highlighted code blocks is always HTML-escaped.&lt;/p&gt;

&lt;p&gt;One potential option would be to break apart the tag; something like&lt;/p&gt;

&lt;div class=&quot;language-liquid highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
This is how you show the termination of the `{% raw %}` tag inside itself: 
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;endraw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, due to &lt;a href=&quot;https://github.com/Shopify/liquid/issues/204&quot;&gt;a bug in Liquid&lt;/a&gt;, this doesn’t work correctly.  This markup is supposed to mean the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Write the literal text &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{%&lt;/code&gt; within the &lt;code&gt;&amp;#123;% raw %}&lt;/code&gt; block&lt;/li&gt;
  &lt;li&gt;Terminate the &lt;code&gt;&amp;#123;% raw %}&lt;/code&gt; block with &lt;code&gt;&amp;#123;% endraw %}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Write the rest of the of the tag (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;endraw %}&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;Re-enter the  &lt;code&gt;&amp;#123;% raw %}&lt;/code&gt; block to continue with other markup&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What actually happens is that Liquid ignores the actual &lt;code&gt;&amp;#123;% endraw %}&lt;/code&gt; command, and treats the entire line as raw text.  In other words, the code you see in the above example is (incorrectly) rendered exactly as written.  The literal text &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{%&lt;/code&gt; before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% endraw %}&lt;/code&gt; causes Liquid to ignore the tag completely, breaking this technique.&lt;/p&gt;

&lt;p&gt;To work around this bug, we need to put the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{%&lt;/code&gt; text outside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; block.  However, Liquid does not have any way to escape a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&lt;/code&gt;, and we can’t use HTML escaping inside the highlighted code block, so there is no obvious way to write the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{%&lt;/code&gt;  without breaking the parser.&lt;/p&gt;

&lt;p&gt;Variables can help here.  We can create a Liquid variable that holds the literal text &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{%&lt;/code&gt;, then interpolate this variable outside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; block.
For example:&lt;/p&gt;

&lt;div class=&quot;language-liquid highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;assign&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;openTag&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'{%'&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
This is how you show the termination of the `{% raw %}` tag inside itself: 
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;endraw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}{{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;openTag&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}}&lt;/span&gt; endraw %}&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
This content is back inside the {% raw %} block
&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;endraw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This code is parsed like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Terminate the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; block with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% endraw %}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Write the value of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;openTag&lt;/code&gt; variable (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{%&lt;/code&gt;) with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{{ openTag }}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Write the remainder of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% endraw %}&lt;/code&gt; tag as literal text with &lt;code&gt;&amp;nbsp;endraw %}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Re-enter the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; block for additional raw content&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This technique can also be used to write tags in inline code: &lt;code&gt;&amp;#96;{{ openTag }} sometag %}&amp;#96;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If these workarounds seem complicated, writing this post itself (also in Liquid Markdown!) was even more complicated.&lt;br /&gt;
See the &lt;a href=&quot;https://raw.github.com/SLaks/SLaks.Blog/gh-pages/_posts/2013-06-10-jekyll-endraw-in-code.md&quot;&gt;source&lt;/a&gt; to see how I did it.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-06-09-writing-about-jekyll-in-jekyll.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-06-09/writing-about-jekyll-in-jekyll/"/>
		<title type="text">Writing about Jekyll in Jekyll</title>
		<updated>2013-06-09T00:00:00+00:00</updated>
		<published>2013-06-09T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Jekyll" term="jekyll" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Liquid" term="liquid" />
		
		
		<category scheme="https://blog.slaks.net/#" label="jekyll-hacks" term="jekyll-hacks" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;&lt;a href=&quot;https://jekyllrb.com&quot;&gt;Jekyll&lt;/a&gt; is a very nice system for writing blogs.  However, it does have some shortcomings, particularly in the Liquid templating engine.  In this post, I will talk about how to write about Liquid tags within a Liquid file (such as a Jekyll blog post).  The problem is that writing Liquid syntax such as tags or variables in the content will cause Liquid to interpret them as commands, and break evertything.&lt;/p&gt;

&lt;p&gt;This problem can occur when writing a blog post about Liquid itself (such as this one), or when writing a blog post about code that generates Liquid markup (like I did &lt;a href=&quot;/2013-06-02/migrating-syntax-highlighting-to-jekyll/&quot;&gt;earlier&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;You may want to mention Liquid syntax in two ways: inline code (mentioning a &lt;code&gt;&amp;#123;% tag %}&lt;/code&gt; in a sentence), and multi-line syntax highlighted code blocks (including a whole chunk of Liquid markup in a post).&lt;/p&gt;

&lt;p&gt;Liquid includes a tag specifically designed to solve this problem: the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; tag.  For example, the Liquid Markdown used to produce the preceding sentence is:&lt;/p&gt;

&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;

&lt;div class=&quot;language-liquid highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Liquid includes a tag specifically designed to solve this problem: 
the `&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;{% raw %}&lt;span class=&quot;p&quot;&gt;{%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;endraw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%}&lt;/span&gt;` tag.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I wrap the text I’m trying to produce (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt;) in &lt;code&gt;&amp;#123;% raw %}...&amp;#123;% endraw %}&lt;/code&gt; tags to prevent liquid from treating the content itself as a tag.  Since Liquid processes the text before the Markdown parser sees it, the resulting Markdown source is simply &lt;code&gt;&amp;#96;&amp;#123;% raw %}&amp;#96;&lt;/code&gt;, which is exactly what I want.&lt;/p&gt;

&lt;p&gt;All that overhead is very annoying when writing a simple tag.  We can make it simpler by writing part of the tag as an HTML entity, so that Liquid doesn’t recognize it as a tag: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;#123;% tag %}&lt;/code&gt;.  The browser will display the HTML entity as a regular &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&lt;/code&gt;, but Liquid won’t recognize it.  However, this does mean that we can’t use Markdown &lt;code&gt;&amp;#96;&lt;/code&gt; blocks to create the code block, since that will escape the HTML and make it display a literal &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;#123;&lt;/code&gt;.&lt;br /&gt;
Thus, the final approach is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;code&amp;gt;&amp;amp;#123;% tag %}&amp;lt;/code&amp;gt;&lt;/code&gt;.  Although this isn’t much shorter than the Liquid raw tag, I find it more readable, since it doesn’t nest Liquid content within Liquid commands.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2013-06-10/jekyll-endraw-in-code/&quot;&gt;&lt;em&gt;Next time: How can you do this inside a syntax-highlighted code block?&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-06-02-migrating-syntax-highlighting-to-jekyll.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-06-02/migrating-syntax-highlighting-to-jekyll/"/>
		<title type="text">Migrating client-side syntax highlighting to Jekyll</title>
		<updated>2013-06-02T00:00:00+00:00</updated>
		<published>2013-06-02T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Jekyll" term="jekyll" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;The next step in &lt;a href=&quot;/2013-05-31/migrating-from-blogger-to-jekyll/&quot;&gt;migrating my blog to Jekyll&lt;/a&gt; was to convert the code blocks to use Jekyll’s &lt;code&gt;&amp;#123;% highlight %}&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Since Blogger has no support for server-side syntax highlighting, all of the code blocks in my HTML are implemented as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;pre class=&quot;brush: someLanguage&quot;&amp;gt;...&amp;lt;/pre&amp;gt;&lt;/code&gt;, with HTML-escaped code inside the tag.  (the class name is used by &lt;a href=&quot;https://alexgorbatchev.com/SyntaxHighlighter/&quot;&gt;SyntaxHighlighter&lt;/a&gt;)  I needed to convert that to Liquid tags with raw (non-escaped) code inside of them.&lt;/p&gt;

&lt;p&gt;To do this, I wrote a small C# script:&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PostsFolder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;@&quot;.../_posts&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;langMappings&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;{&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;vb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;vb.net&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetLanguage&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lang&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;langMappings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;TryGetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MatchEvaluator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Replace&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replacement&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ManifestResourceInfo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ManifestResourceInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replacement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;regexes&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MatchEvaluator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&quot;\s*\&amp;lt;pre\s*class=&quot;&quot;brush:\s*(\w+);?\s*&quot;&quot;&amp;gt;\n*(.*?)\n*&amp;lt;/pre&amp;gt;\s*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RegexOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;\n{% endraw %}\n{% highlight &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetLanguage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; %}\n&quot;&lt;/span&gt; 
			&lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebUtility&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;HtmlDecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
			&lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;\n{% endhighlight %}\n{% raw %}\n&quot;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&quot;{% raw %}\s*{% endraw %}\s*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RegexOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&quot;&amp;lt;font face=&quot;&quot;Courier New&quot;&quot;&amp;gt;([^&amp;lt;]+)&amp;lt;/font&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RegexOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;code&amp;gt;$1&amp;lt;/code&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Directory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;EnumerateFiles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PostsFolder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;*.html&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteAllText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;regexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Aggregate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ReadAllText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;txt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Item1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;txt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Item2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(This kind of script is most easily run in &lt;a href=&quot;https://linqpad.net&quot;&gt;LINQPad&lt;/a&gt; or &lt;a href=&quot;https://scriptcs.net/&quot;&gt;scriptcs&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;This code loops through every HTML file in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostsFolder&lt;/code&gt; and runs the text through a series of regular expressions (yes, evil) to update it for Jekyll.&lt;/p&gt;

&lt;p&gt;The first, and biggest, regex matches &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;pre class=&quot;brush: someLanguage&quot;&amp;gt;...&amp;lt;/pre&amp;gt;&lt;/code&gt;, and convers each to Jekyll &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% highlight someLanguage %}&lt;/code&gt; blocks.  Since SyntaxHighlighter uses different language names than &lt;a href=&quot;https://pygments.org/docs/lexers/#lexers-for-net-languages&quot;&gt;Pygments’&lt;/a&gt;, I have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;langMappings&lt;/code&gt; that maps language names from the HTML to language names for the Jekyll output.  I also un-HTML-escape the contents of each code block.  Finally, because I modified blogger2jekyll to wrap each post in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; tag, it terminates the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; and re-enters it after the code.&lt;/p&gt;

&lt;p&gt;Depending on what your code blocks look like, you may want to change it to wrap the contents of each &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% highligh %}&lt;/code&gt; tag in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; block too.&lt;/p&gt;

&lt;p&gt;The next regex strips empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{% raw %}&lt;/code&gt; blocks in case there are two code blocks in a row (which I had &lt;a href=&quot;/2011/09/clarifying-boolean-parameters-part-2.html&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The last regex replaces bad non-semantic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;font&amp;gt;&lt;/code&gt; tags created by Windows Live Writer with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;code&amp;gt;&lt;/code&gt; tags.&lt;br /&gt;
If your imported HTML has similar issues, you can add more regexes to correct them.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next time: Preserving the RSS feed&lt;/em&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-05-31-migrating-from-blogger-to-jekyll.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-05-31/migrating-from-blogger-to-jekyll/"/>
		<title type="text">Migrating from Blogger to Jekyll</title>
		<updated>2013-05-31T00:00:00+00:00</updated>
		<published>2013-05-31T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Jekyll" term="jekyll" />
		
		
		<category scheme="https://blog.slaks.net/#" label="blogger" term="blogger" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Node.js" term="node-js" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;The first step in my migration to Jekyll was to import my old posts into the Jekyll site.  To do this, I used &lt;a href=&quot;https://github.com/coolaj86/blogger2jekyll&quot;&gt;blogger2jekyll&lt;/a&gt;, a wonderful open-source Node.js script that does exactly that.&lt;/p&gt;

&lt;p&gt;Using this tool is very simple.  First, log into Blogger’s admin panel, got to Settings, Other, and click Export blog to download a giant XML file with all of your posts.&lt;/p&gt;

&lt;p&gt;Next, install and run the script: (you’ll need to install &lt;a href=&quot;https://nodejs.org&quot;&gt;Node.js&lt;/a&gt; first)&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; blogger2jekyll
blogger2jekyll  /path/to/blog-dd-mm-yyyy.xml ./_posts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you aren’t running it from the directory containing your Jekyll site, you’ll need to specify the full path to Jekyll’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;This script will create HTML files with the contents of each post from the exported blog, ready for Jekyll to serve.  It will include &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layout: &quot;post&quot;&lt;/code&gt; in the Jekyll &lt;a href=&quot;https://jekyllrb.com/docs/frontmatter/&quot;&gt;front matter&lt;/a&gt;; if you have a different layout name, you’ll need to do a bulk replace within the resulting files.  It will also set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;permalink&lt;/code&gt; for each post so that existing posts keep their old URLs (even if the Jekyll blog hasa different URL scheme).&lt;/p&gt;

&lt;p&gt;I had a couple of problems with this script:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;When running on Windows, the generated permalink URLs use backslashes rather than forward slashes&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Posts that contain Liquid-like markup will break Jekyll with a parse error&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The script also imports comments directly into the resulting HTML, which is rarely a good idea.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I fixed all of these issues in a &lt;a href=&quot;https://github.com/coolaj86/blogger2jekyll/pull/7&quot;&gt;pull request&lt;/a&gt;, which has been merged and pushed to npm.&lt;/p&gt;

&lt;p&gt;I changed it to wrap the contents of each post in a Liquid &lt;code&gt;&amp;#123;% raw %}&lt;/code&gt; tag.  I also added an internal option to skip comments.  However, I didn’t add a command-line interface for the comment option; to use it, you’ll need to manually add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;, skipComments: true&lt;/code&gt; to the &lt;a href=&quot;https://github.com/SLaks/blogger2jekyll/blob/master/bin/blogger2jekyll.js#L46&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parse()&lt;/code&gt; call&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2013-06-02/migrating-syntax-highlighting-to-jekyll/&quot;&gt;&lt;em&gt;Next time: Converting Code Blocks&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-05-27-about-this-design.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-05-27/about-this-design/"/>
		<title type="text">About the new design</title>
		<updated>2013-05-27T00:00:00+00:00</updated>
		<published>2013-05-27T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Jekyll" term="jekyll" />
		
		
		<category scheme="https://blog.slaks.net/#" label="LESS" term="less" />
		
		
		<category scheme="https://blog.slaks.net/#" label="DRY" term="dry" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;My new design is powered by Jekyll and LESS (the LESS does as much or more as the Jekyll).&lt;/p&gt;

&lt;p&gt;When implementing the design, I had the following goals in mind:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;No costs&lt;/li&gt;
  &lt;li&gt;I use GitHub Pages for completely free hosting (other than the cost of the domain name)&lt;/li&gt;
  &lt;li&gt;This means that I cannot use Octopress or Jekyll plugins&lt;/li&gt;
  &lt;li&gt;No build step&lt;/li&gt;
  &lt;li&gt;I want to be able to edit posts from anywhere, without having to install Ruby or Grunt.js and run any kind of build process before pushing&lt;/li&gt;
  &lt;li&gt;I do use pre-compiled LESS, since Jekyll on GitHub Pages cannot compile LESS, and I really want to use LESS.  (also, since LESS needs to be edited far less often than post content, and since my editor &lt;a href=&quot;http://vswebessentials.com/features/less&quot;&gt;automatically compiles LESS files on save&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;No Javascript&lt;/li&gt;
  &lt;li&gt;Especially with the power of CSS selectors, there should be no reason to use Javascript for static content&lt;/li&gt;
  &lt;li&gt;If I add comments, I will have to relax this restriction (since the site will no longer be purely static content)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Don%27t_repeat_yourself&quot; title=&quot;Don't repeat yourself&quot;&gt;DRY&lt;/a&gt; implementation&lt;/li&gt;
  &lt;li&gt;Repetition of data or rules is a problem not only in large programs, but also in HTML templating engines or style definitions.  LESS in particular is very helpful in getting rid of unnecessary repetition in CSS.&lt;/li&gt;
  &lt;li&gt;Rich category support&lt;/li&gt;
  &lt;li&gt;I feel that category listing pages are a great way to make posts more discoverable&lt;/li&gt;
  &lt;li&gt;Clean, minimalist, design&lt;/li&gt;
  &lt;li&gt;One of the mistakes of &lt;a href=&quot;http://old-blog.slaks.net&quot;&gt;my old design&lt;/a&gt; was that it put too little focus on content (both in size and in coloring).&lt;/li&gt;
  &lt;li&gt;Easy navigation within series of multiple posts&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Coming soon&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;For now, the new sidebar in post pages (see left side) provides this to a limited extent&lt;/li&gt;
  &lt;li&gt;Excessive use of bulleted lists&lt;/li&gt;
  &lt;li&gt;Bulleted lists are an excellent way to concisely present structured data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Next time: &lt;a href=&quot;/2013-05-31/migrating-from-blogger-to-jekyll&quot;&gt;How I migrated from Blogger to Jekyll&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>https://blog.slaks.net_posts/2013-05-26-relaunch.md</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2013-05-26/relaunch/"/>
		<title type="text">Relaunch!</title>
		<updated>2013-05-26T00:00:00+00:00</updated>
		<published>2013-05-26T00:00:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Jekyll" term="jekyll" />
		
		
		<category scheme="https://blog.slaks.net/#" label="LESS" term="less" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;p&gt;After nearly a year of inactivity, I have finally returned to my blog.&lt;/p&gt;

&lt;p&gt;I had neglected it for so long primarily because I don’t like Blogger’s compose options.  After spending so much time on StackOverflow and GitHub, I find Markdown far more convenient than Windows Live Writer or Blogger’s compose window, especially when writing about code.  I also was never very happy with the design I originally created, and doing raw HTML / CSS design in Blogger is painful.&lt;/p&gt;

&lt;p&gt;To solve these problems, I just finished porting my blog to &lt;a href=&quot;https://jekyllrb.com&quot;&gt;Jekyll&lt;/a&gt; on &lt;a href=&quot;https://pages.github.com/&quot;&gt;GitHub Pages&lt;/a&gt;.  Now that I can write posts in my &lt;a href=&quot;http://vswebessentials.com/&quot;&gt;favorite Markdown editor&lt;/a&gt;, I hope to end up writing much more.&lt;/p&gt;

&lt;p&gt;In the next few posts, I’ll write about &lt;a href=&quot;/2013-05-27/about-this-design/&quot;&gt;how I designed the new blog&lt;/a&gt; and how I migrated my existing content.&lt;/p&gt;

&lt;p&gt;My old blog is still up at &lt;a href=&quot;http://old-blog.slaks.net&quot;&gt;old-blog.slaks.net&lt;/a&gt;.  However, all of the content has been migrated to the new one (with the same URLs), so that should not be necessary.  If you see anything wrong with the new site, let me know &lt;a href=&quot;https://twitter.com/Schabse&quot;&gt;on Twitter&lt;/a&gt; or &lt;a href=&quot;mailto:Blog@SLaks.net?subject=Blog+Migration+Problem&quot;&gt;by email&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-9147342851980251040</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2012/06/visual-studio-2012-and-webpagesversion.html"/>
		<title type="text">Visual Studio 2012 and webpages:Version</title>
		<updated>2012-06-11T22:33:00+00:00</updated>
		<published>2012-06-12T12:32:59+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Visual-Studio-2012" term="visual-studio-2012" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net MVC" term="asp-net-mvc" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;If you open an older ASP.Net MVC3 project in Visual Studio 2012, you may see lots of errors in the Razor views, along the lines of “The name 'model' does not exist in the current context”, and similar errors whenever you try to use MVC features like HTML helpers or ViewContext (eg, “System.Web.WebPages.Html.HtmlHelper does not contain a definition for TextBoxFor”).&lt;/p&gt;  &lt;p&gt;This happens if there is no &lt;code&gt;&amp;lt;add key=&amp;quot;webpages:Version&amp;quot; value=&amp;quot;1.0&amp;quot; /&amp;gt;&lt;/code&gt; in the &amp;lt;appSettings&amp;gt; element in Web.config.&lt;/p&gt;  &lt;p&gt;Without this element, Visual Studio will assume that you’re using the latest version of Razor and the WebPages framework.&amp;#160; Until VS2012, this wasn’t a problem, since there was only one version.&amp;#160; However, since VS2012 ships with ASP.Net WebPages 2.0, the IDE will load this version by default.&amp;#160; Since you’ve specified the MVC integration &amp;amp; &amp;lt;configSection&amp;gt; for 1.0 (in Views/Web.config, since all of the assembly references specify Version=1.0.0.0), the language services won’t load the MVC settings.&amp;#160; Therefore, the &lt;code&gt;@model&lt;/code&gt; directive will not work, and the view will inherit the standard &lt;code&gt;WebPage&lt;/code&gt; base class rather than the MVC &lt;code&gt;WebViewPage&lt;/code&gt; base class (which contains the MVC HTML helpers)&lt;/p&gt;  &lt;p&gt;This issue has no effect at runtime because the server won’t load any version 2.0 assemblies.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8396183916466972553</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2012/03/exploring-caller-info-attributes.html"/>
		<title type="text">Exploring Caller Info Attributes</title>
		<updated>2012-03-01T06:57:00+00:00</updated>
		<published>2012-03-01T06:57:54+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="caller-info-attributes" term="caller-info-attributes" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C# 5" term="csharp-5" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Last year, Microsoft announced a simple new feature in C# 5: &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/hh534540%28VS.110%29.aspx&quot;&gt;Caller Info Attributes&lt;/a&gt;.&amp;#160; These attributes let you to create methods with optional parameters and tell the compiler to pass the caller’s filepath, line number, or member name instead of the parameter’s default value.&amp;#160; This allows you to create logging methods that automatically know where they’re being called.&lt;/p&gt;  &lt;p&gt;When the feature was announced, I wrote a &lt;a href=&quot;/2011/10/subtleties-of-new-caller-info.html&quot;&gt;couple&lt;/a&gt; of &lt;a href=&quot;/2011/10/subtleties-of-c-5s-new-callerlinenumber.html&quot;&gt;blog&lt;/a&gt; &lt;a href=&quot;/2011/10/subtleties-of-c-5s-new-callermembername.html&quot;&gt;posts&lt;/a&gt; that delved into some of the corner cases of the new feature.&amp;#160; At the time, there was no public implementation, so they were pure conjecture.&lt;/p&gt;  &lt;p&gt;This morning, Microsoft released the beta of &lt;a href=&quot;https://www.microsoft.com/visualstudio/11/en-us&quot;&gt;Visual Studio 11&lt;/a&gt;, which is the first public build supporting these attributes.&amp;#160; Now, I can finally test my theories.&amp;#160; Here are the results:&lt;/p&gt;  &lt;p&gt;Although these classes are new to the .Net Framework 4.5, you can still use this feature against older framework versions by creating your own classes in the System.Runtime.CompilerServices namespace.&amp;#160; However, the feature will only work if the code calling the method is compiled with the C# 5 compiler; older compilers will ignore the attributes and simply pass the parameters’ default values.&lt;/p&gt;  &lt;p&gt;All of the attributes can only be applied to arguments of types that have standard (not custom) implicit conversions to int or string.&amp;#160; This means that it isn’t practical to overflow [CallerLineNumber] (the compiler ran out of memory first), so I can’t test how that behaves.&lt;/p&gt;  &lt;p&gt;Using [CallerMemberName] on field initializers passes the field name, and on static or instances constructors passes the string &lt;code&gt;&amp;quot;.cctor&amp;quot;&lt;/code&gt; or &lt;code&gt;&amp;quot;.ctor&amp;quot;&lt;/code&gt; (as &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/hh534540%28VS.110%29.aspx#sectionToggle1&quot;&gt;documented&lt;/a&gt;)&amp;#160; In indexers, it passes &lt;code&gt;&amp;quot;Item&amp;quot;&lt;/code&gt;.&lt;/p&gt;  &lt;p&gt;If a class has a constructor that takes only caller info attribute parameters, and you create another class that inherits it and does not declare a constructor (thus implicitly passing optional parameters), it passes the line number and file name of the class keyword in the derived class, but leaves the declared default for the member name (I suspect that’s a bug).&amp;#160; &lt;/p&gt;  &lt;p&gt;If you do declare a constructor, it passes the string &lt;code&gt;&amp;quot;.ctor&amp;quot;&lt;/code&gt; as the member name for the implicit &lt;code&gt;base()&lt;/code&gt; call (just like a normal method call from inside a constructor) and the line number of the beginning of the constructor declaration.&amp;#160; If you actually write a &lt;code&gt;base()&lt;/code&gt; call, it passes the line number of the &lt;code&gt;base&lt;/code&gt; keyword.&lt;/p&gt;  &lt;p&gt;If a call spans multiple lines, [CallerLineNumber] passes the line containing the openning parenthesis.&lt;/p&gt;  &lt;p&gt;Delegates are fully supported; if you call a delegate that has an argumented annotated with a caller info attribute, the compiler will insert the correct value, regardless of the method you’re actually calling (which the compiler doesn’t even know).&lt;/p&gt;  &lt;p&gt;LINQ query comprehension syntax is not supported at all; if you create a (for example) &lt;code&gt;Select()&lt;/code&gt; method that contains a caller info attribute, then call it from a LINQ query (not lambda syntax), the compiler will crash (!).&amp;#160; (they will fix that)&lt;/p&gt;  &lt;p&gt;Expression trees do not support optional parameters at all, so that corner case is irrelevant.&lt;/p&gt;  &lt;p&gt;Attributes are the most interesting story.&amp;#160; What should happen if you declare a custom attribute that takes parameters with caller info attributes, then apply that attribute in various cases?&amp;#160; This could potentially be very useful, since there is currently no way for an attribute to know what it’s being applied to. (I hadn’t thought of this usage when I wrote the original blog post)&lt;/p&gt;  &lt;p&gt;The documentation &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/hh534540%28VS.110%29.aspx#sectionToggle1&quot;&gt;says&lt;/a&gt; that this will work in all cases, and that [CallerMemberName] will pass whatever the attribute is being applied to.&amp;#160; However, in the beta build, this doesn’t always work.&lt;/p&gt;  &lt;p&gt;Attributes applied to method arguments or return values do not pass any caller info at all.&amp;#160; Attributes applied to types or generic type arguments do not pass member names (this is very disappointing)&lt;/p&gt;  &lt;p&gt;Hopefully, those will be fixed before release.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8782643458723994748</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2012/02/aspnet-mvc-unobtrusive-validation-bug.html"/>
		<title type="text">ASP.Net MVC Unobtrusive Validation Bug</title>
		<updated>2012-02-21T18:15:00+00:00</updated>
		<published>2012-02-21T18:15:54+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="bugs" term="bugs" />
		
		
		<category scheme="https://blog.slaks.net/#" label="jQuery" term="jquery" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net MVC" term="asp-net-mvc" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;If you use the ASP.Net MVC 3 &lt;code&gt;[Compare]&lt;/code&gt; validation attribute on a model property, then include that model as a property in a parent model (so that the field name becomes &lt;code&gt;Parent.ChildProperty&lt;/code&gt;), the built-in unobtrusive client validation will choke, and will always report the field as having an error.&lt;/p&gt;  &lt;p&gt;This is due to a bug on line 288 of jquery.validate.unobtrusive.js: &lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;adapters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;equalto&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getModelPrefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;fullOtherName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;appendModelPrefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;:input[name=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fullOtherName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// Bug&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;setValidationValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;equalTo&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Because the value of the &lt;code&gt;name&lt;/code&gt; attribute selector is not quoted, this fails if the name contains a &lt;code&gt;.&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The simplest fix is to add quotes around the concatenated value.&amp;#160; However, the jQuery selector there is overkill.&amp;#160; HTML form elements have properties for each named input, so you can do this instead:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;adapters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;equalto&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getModelPrefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;fullOtherName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;appendModelPrefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fullOtherName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fullOtherName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; not found&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//If there are multiple inputs with that name, get the first one&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;        
        &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;setValidationValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;equalTo&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;/div&gt;</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4405908569432660011</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2012/01/protecting-against-csrf-attacks-in.html"/>
		<title type="text">Protecting against CSRF attacks in ASP.Net MVC</title>
		<updated>2012-01-25T23:18:00+00:00</updated>
		<published>2012-01-25T23:18:59+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="security" term="security" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net MVC" term="asp-net-mvc" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Cross-site_request_forgery&quot;&gt;CSRF attacks&lt;/a&gt; are one of the many security issues that web developers must defend against.&amp;#160; Fortunately, ASP.Net MVC makes it easy to defend against CSRF attacks.&amp;#160; Simply slap on [ValidateAntiForgeryToken] to every POST action and include @Html.AntiForgeryToken() in every form, and your forms will be secure against CSRF.&lt;/p&gt;  &lt;p&gt;However, it is easy to forget to apply [ValidateAntiForgeryToken] to every action.&amp;#160; To prevent such mistakes, you can create a unit test that loops through all of your controller actions and makes sure that every [HttpPost] action also has [ValidateAntiForgeryToken].&amp;#160; &lt;/p&gt;  &lt;p&gt;Since there may be some POST actions that should not be protected against CSRF, you’ll probably also want a marker attribute to tell the test to ignore some actions.&lt;/p&gt;  &lt;p&gt;This can be implemented like this:&lt;/p&gt;  &lt;p&gt;First, define the marker attribute in the MVC web project.&amp;#160; This attribute can be applied to a single action, or to a controller to allow every action in the controller.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;///&amp;lt;summary&amp;gt;Indicates that an action or controller deliberately &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// allows CSRF attacks.&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;///&amp;lt;remarks&amp;gt;All [HttpPost] actions must have &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// [ValidateAntiForgeryToken]; any deliberately unprotected &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// actions must be marked with this attribute.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// This rule is enforced by a unit test.&amp;lt;/remarks&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AttributeUsage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AttributeTargets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AttributeTargets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AllowCsrfAttacksAttribute&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Attribute&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then, add the following unit test:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CheckForCsrfProtection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controllers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MvcApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAssignableFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controllers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsDefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AllowCsrfAttacksAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postActions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetMethods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                                &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ContainsGenericParameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                                &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsDefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ChildActionOnlyAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
                                &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsDefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NonActionAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
                                &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetParameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsOut&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ParameterType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsByRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
                                &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsDefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpPostAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postActions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//CSRF XOR AntiForgery&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsTrue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsDefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AllowCsrfAttacksAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IsDefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ValidateAntiForgeryTokenAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; is [HttpPost] but not [ValidateAntiForgeryToken]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

	&lt;p&gt;&lt;code&gt;typeof(MvcApplication)&lt;/code&gt; must be any type in the assembly that contains your controllers.&amp;#160; If your controllers are defined in multiple assemblies, you’ll need to include those assemblies too.&lt;/p&gt;


  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8770635603144624525</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/12/dark-side-of-covariance.html"/>
		<title type="text">The Dark Side of Covariance</title>
		<updated>2011-12-15T04:14:00+00:00</updated>
		<published>2011-12-15T04:17:32+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="covariance" term="covariance" />
		
		
		<category scheme="https://blog.slaks.net/#" label="extension-methods" term="extension-methods" />
		
		
		<category scheme="https://blog.slaks.net/#" label="generics" term="generics" />
		
		
		<category scheme="https://blog.slaks.net/#" label="bugs" term="bugs" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="LINQ" term="linq" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;What’s wrong with the following code?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HashSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StringComparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqlCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ExecuteScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This&amp;#160; code is intended to check whether the result of a SQL query is contained in a case-insensitive collection of names.&amp;#160; However, if you run this code, the resulting check will be case-sensitive.&amp;#160; Why?&lt;/p&gt;

&lt;p&gt;As you may have guessed from the title, this is caused by covariance.&amp;#160; In fact, this code will not compile at all against .Net 3.5.&amp;#160; &lt;/p&gt;

&lt;p&gt;The problem is that &lt;code&gt;ExecuteScalar()&lt;/code&gt; returns &lt;code&gt;object&lt;/code&gt;, not &lt;code&gt;string&lt;/code&gt;.&amp;#160; Therefore, it doesn’t call &lt;code&gt;HashSet&amp;lt;string&amp;gt;.Contains(string)&lt;/code&gt;, which is what it’s intending to call (and which uses the HashSet’s comparer).&amp;#160; Instead, on .Net 4.0, this calls the&amp;#160; &lt;code&gt;Enumerable.Contains&amp;lt;object&amp;gt;(IEnumerable&amp;lt;object&amp;gt;, string)&lt;/code&gt; extension method, using the covariant conversion from &lt;code&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/code&gt; to &lt;code&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/code&gt;.&amp;#160; Covariance allows us to pass &lt;code&gt;object&lt;/code&gt; to the &lt;code&gt;Contains&lt;/code&gt; method of any strongly-typed collection (of reference types).&lt;/p&gt;

&lt;p&gt;Still, why is it case-sensitive?&amp;#160; As Jon Skeet &lt;a href=&quot;https://msmvps.com/blogs/jon_skeet/archive/2011/01/12/reimplementing-linq-to-objects-part-32-contains.aspx&quot;&gt;points out&lt;/a&gt;, the LINQ Contains() method is supposed to call any built-in Contains() method from ICollection&amp;lt;T&amp;gt;, so it should still use the HashSet’s case-insensitive Contains().&lt;/p&gt;

&lt;p&gt;The reason is that although HashSet&amp;lt;String&amp;gt; implements ICollection&amp;lt;string&amp;gt;, it does not implement ICollection&amp;lt;object&amp;gt;.&amp;#160; Since we’re calling Enumerable.Contains&amp;lt;object&amp;gt;, it checks whether the sequence implements ICollection&amp;lt;object&amp;gt;, which it doesn’t.&amp;#160; (ICollection&amp;lt;T&amp;gt; is not covariant, since it allows write access)&lt;/p&gt;

&lt;p&gt;Fortunately, there’s a simple fix: just cast the return value back to string (and add a comment explaining the problem).&amp;#160; This allows the compiler to call &lt;code&gt;HashSet&amp;lt;string&amp;gt;.Contains(string)&lt;/code&gt;, as was originally intended.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//Call HashSet&amp;lt;string&amp;gt;.Contains(string), not the&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//covariant Enumerable.Contains(IEnumerable&amp;lt;object&amp;gt;, object)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//https://blog.slaks.net/2011/12/dark-side-of-covariance.html&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqlCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ExecuteScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

(I discovered this issue in my &lt;a href=&quot;/2011/09/using-default-controller-in-aspnet-mvc.html&quot;&gt;StringListConstraint for ASP.Net MVC&lt;/a&gt;)  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4207385259454427160</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/12/captchas-do-not-mitigate-xss-worms.html"/>
		<title type="text">CAPTCHAs do not mitigate XSS worms</title>
		<updated>2011-12-08T02:32:00+00:00</updated>
		<published>2011-12-08T02:32:19+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="CAPTCHA" term="captcha" />
		
		
		<category scheme="https://blog.slaks.net/#" label="XSS" term="xss" />
		
		
		<category scheme="https://blog.slaks.net/#" label="security" term="security" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;One &lt;a href=&quot;https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29_Prevention_Cheat_Sheet#No_Cross-Site_Scripting_.28XSS.29_Vulnerabilities&quot;&gt;common&lt;/a&gt;&amp;#160;&lt;a href=&quot;https://www.processor.com/editorial/article.asp?article=articles%2Fp3207%2F33p07%2F33p07.asp&quot;&gt;misconception&lt;/a&gt; about web security is that protecting important actions with CAPTCHAs can prevent XSS attacks from doing real damage.&amp;#160; By preventing malicious code from scripting critical tasks, the idea goes, XSS injections won’t be able to accomplish much.&lt;/p&gt;  &lt;p&gt;This idea is dangerously wrong.&amp;#160; &lt;/p&gt;  &lt;p&gt;First of all, this should not even be considered except as a defense-in-depth mechanism.&amp;#160; Regardless of whether the actions you care about are protected by CAPTCHAs, XSS attacks can create arbitrary UI on your pages, and can thus make “perfect” phishing attacks.&lt;/p&gt;  &lt;p&gt;Also, even with CAPTCHAs, an XSS injection can wait until the user performs the critical action, then change the submitted data to the attacker’s whim.&lt;/p&gt;  &lt;p&gt;For example, if Twitter took this approach to prevent XSS injections from sending spammy tweets, the attacker could simply wait until the user sends a real tweet, then silently append advertising to the tweet as the user submits it and fills out the CAPTCHA.&lt;/p&gt;  &lt;p&gt;However, there is also a more fundamental issue.&amp;#160; Since the injected Javascript is running in the user’s browser, it simply display the CAPTCHA to the user and block all page functionality until the user solves the CAPTCHA.&amp;#160; The attacker can even put his own text around the CAPTCHA to look like a legitimate security precaution, so that the (typical) user will not realize that the site has been compromised.&amp;#160; (that could be prevented by integrating a description of the action being performed into the CAPTCHA itself in a way that the attacker can’t hide)&lt;/p&gt;  &lt;p&gt;I haven’t even mentioned the inconvenience of forcing all legitimate, uncompromised users to fill out CAPTCHAs every time they do anything significant.&lt;/p&gt;  &lt;p&gt;In summary, CAPTCHAs should only be used to prevent programs from automatically performing actions (eg, bulk-registering Google accounts), and as a rate-limiter if a user sends too many requests too quickly (eg, getting a password wrong too many times in a row).&lt;/p&gt;  &lt;p&gt;XSS can only be stopped by &lt;em&gt;properly&lt;/em&gt; encoding all user-generated content that gets concatenated into markup (whether HTML, Javascript, &lt;a href=&quot;https://stackoverflow.com/q/3607894/34397&quot;&gt;or CSS&lt;/a&gt;)&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8726791969814095130</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/11/one-of-most-useful-additions-to.html"/>
		<title type="text">About Concurrent Collections</title>
		<updated>2011-11-20T18:42:00+00:00</updated>
		<published>2011-11-20T18:42:58+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="multi-threading" term="multi-threading" />
		
		
		<category scheme="https://blog.slaks.net/#" label="thread-safety" term="thread-safety" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;One of the most useful additions to the .Net 4.0 base class library is the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx&quot;&gt;System.Collections.Concurrent namespace&lt;/a&gt;, which contains an all-new set of lock-free thread.&lt;/p&gt;  &lt;p&gt;However, these collections are noticeably different from their classical counterparts.&amp;#160; There is no simple ConcurrentList&amp;lt;T&amp;gt; that you can drop into your code so that it will become thread-safe.&amp;#160; Instead, the new namespace has a queue, a stack, and some new thing called a &lt;em&gt;bag&lt;/em&gt;, as well as ConcurrentDictionary&amp;lt;TKey, TValue&amp;gt; that largely resembles classical dictionaries.&amp;#160; It also has a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd997371.aspx&quot;&gt;BlockingCollection&amp;lt;T&amp;gt; class&lt;/a&gt; that wraps a concurrent collection and blocks until operations can succeed.&lt;/p&gt;  &lt;p&gt;Many people have complained that Microsoft chose to provide an entirely new set of classes, rather than adding synchronized versions of the existing collections.&amp;#160; &lt;/p&gt;  &lt;p&gt;In truth, however, creating this new set of classes is the correct – and, in fact, only – choice.&amp;#160; Ordinary synchronized are rarely useful and will not make a program thread-safe.&amp;#160; In general, &lt;strong&gt;slapping &lt;code&gt;lock&lt;/code&gt;s everywhere does not make a program thread-safe!&lt;/strong&gt;&amp;#160; Rather, that will either not help (if there aren’t enough locks) or, if there are enough locks, result in deadlocks or a program that never runs more than one thread at a time.&amp;#160; (depending on how many different objects get locked)&lt;/p&gt;  &lt;p&gt;Collections in particular have two fundamental issues when used on multiple threads.&lt;/p&gt;  &lt;p&gt;The first, and simpler, issue is that the collection classes themselves are not thread-safe.&amp;#160; If two threads add to a List&amp;lt;T&amp;gt; at the same exact time, one thread is likely to overwrite the other thread’s value.&amp;#160; A synchronized version of List&amp;lt;T&amp;gt; with a lock around every method would solve this problem.&lt;/p&gt;  &lt;p&gt;The bigger issue is that any code that uses a List&amp;lt;T&amp;gt; is unlikely to be thread-safe, even if the list itself is thread-safe.&amp;#160; For example. you can never enumerate over a multi-threaded list, because another thread may change the list at any time, invalidating the enumerator.&amp;#160; This issue could be solved by taking a read lock for the lifetime of the enumerator.&amp;#160; However, &lt;a href=&quot;https://stackoverflow.com/questions/2274664/partially-thread-safe-dictionary/2274773#2274773&quot;&gt;that is also a bad idea&lt;/a&gt;, since if any client code forgets to dispose the enumerator, the collection will deadlock when written to.&lt;/p&gt;  &lt;p&gt;You also cannot use indices.&amp;#160; It is never safe to get, set, or remove an item at an index, because another thread might remove that item or clear the entire collection between your index check and the operation.&lt;/p&gt;  &lt;p&gt;To solve all of these problems, you need thread-safe collections that provide atomic operations.&amp;#160; This is why all of the concurrent collections have such strange methods, including &lt;code&gt;TryPop&lt;/code&gt;, &lt;code&gt;AddOrUpdate&lt;/code&gt;, and &lt;code&gt;TryTake&lt;/code&gt;.&amp;#160; These methods perform their operations atomically, and return false if the collection was empty (as appropriate; consult the documentation for actual detail).&amp;#160; Thus, the new concurrent collections can be used reliably in actual multi-threaded code without a separate layer of locks.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2726234564275500643</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/11/beware-of-responseredirecttoroute-in.html"/>
		<title type="text">Beware of Response.RedirectToRoute in MVC 3.0</title>
		<updated>2011-11-09T04:00:00+00:00</updated>
		<published>2011-11-09T04:00:26+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="bugs" term="bugs" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net MVC" term="asp-net-mvc" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;ASP.Net MVC uses the new (to ASP.Net 3.5) Http*Base wrapper classes (HttpContextBase, HttpRequestBase, HttpResponseBase, etc) instead of the original Http* classes.&amp;#160; This allows you to create mock implementations that inherit the Http*Base classes without an actual HTTP request.&amp;#160; This is useful for unit testing, and for overriding standard behaviors (such as &lt;a href=&quot;https://stackoverflow.com/questions/4882324/how-can-i-check-if-a-route-asp-net-mvc-exists-for-a-given-path&quot;&gt;route checking&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;In ordinary MVC code, the HttpContext, Request, and Response properties will return Http*Wrapper instances that directly wrap the original Http* classes (eg, &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.httpcontextwrapper.aspx&quot;&gt;HttpContextWrapper&lt;/a&gt;, which wraps &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx&quot;&gt;HttpContext&lt;/a&gt;).&amp;#160; Most MVC developers use the HttpContext and related properties without being aware of any of this redirection.&lt;/p&gt;  &lt;p&gt;Until you call &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.httpresponse.redirecttoroute.aspx&quot;&gt;Response.RedirectToRoute&lt;/a&gt;.&amp;#160; This method, which is new to .Net 4.0, redirects the browser to a URL for a route in the new ASP.Net routing engine.&amp;#160; Like other HttpResponse methods, HttpResponseBase has &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.httpresponsebase.redirecttoroute.aspx&quot;&gt;its own version&lt;/a&gt; of this method for derived classes to override.&lt;/p&gt;  &lt;p&gt;However, in .Net 4.0, Microsoft forgot to override this method in the standard HttpResponseWrapper.&amp;#160; Therefore, if you call Response.RedirectToRoute in an MVC application (where Response is actually an HttpResponseWrapper), you’ll get a NotImplementedException.&lt;/p&gt;  &lt;p&gt;You can see this oversight in the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.httpresponsewrapper_methods.aspx&quot;&gt;methods list&lt;/a&gt; for HttpResponseWrapper.&amp;#160; Every method except for RedirectToRoute and RedirectToRoutePermanent are list as (Overrides HttpResponseBase.&lt;em&gt;MethodName(&lt;/em&gt;).); these methods are listed as (Inherited from HttpResponseBase.&lt;em&gt;MethodName(&lt;/em&gt;).)&lt;/p&gt;  &lt;p&gt;To work around this issue, you can either use the original HttpResponse by writing HttpContext.Current.Response.RedirectToRoute(…) or by calling Response.Redirect instead.&lt;/p&gt;  &lt;p&gt;Note that most MVC applications should not call Response.Redirect or Response.RedirectToRoute at all; instead, they should return ActionResults by calling helper methods like &lt;code&gt;return Redirect(…);&lt;/code&gt; or &lt;code&gt;return RedirectToAction(…);&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;In the upcoming ASP.Net 4.5 release, these methods have been &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.httpresponsewrapper.redirecttoroute%28v=vs.110%29.aspx&quot;&gt;properly overridden&lt;/a&gt;.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-6126349191789816440</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/10/caller-info-attributes-vs-stack-walking.html"/>
		<title type="text">Caller Info Attributes vs. Stack Walking</title>
		<updated>2011-10-23T23:55:00+00:00</updated>
		<published>2011-10-23T23:55:37+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="design" term="design" />
		
		
		<category scheme="https://blog.slaks.net/#" label="caller-info-attributes" term="caller-info-attributes" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C# 5" term="csharp-5" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;People sometimes wonder why C# 5 needs to add caller info attributes, when this information is already available by using the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx&quot;&gt;StackTrace class&lt;/a&gt;.&amp;#160; In reality, caller info attributes behave rather differently from the StackTrace class, for a number of reasons.&lt;/p&gt;  &lt;h2&gt;Advantages to Caller Info Attributes&lt;/h2&gt;  &lt;p&gt;The primary reason to use caller info attributes is that they’re &lt;em&gt;much&lt;/em&gt; faster.&amp;#160; Stack walking is one of the slowest internal (as opposed to network IO) things you can do in .Net (disclaimer: I haven’t measured).&amp;#160; By contrast, caller info attributes have exactly 0 performance penalty.&amp;#160; Caller info is resolved at compile-time; the callsite is compiled to pass a string or int literal that was determined by the compiler.&amp;#160; Incidentally, this is why C# 5 doesn’t have [CallerType] or [CallerMemberInfo] attributes; the compiler team wasn’t happy with the performance of the result IL and didn’t have time to implement proper caching to make it faster.&lt;/p&gt;  &lt;p&gt;Caller info attributes are also more reliable than stack walking.&amp;#160; Stack walking will give incorrect results if the JITter decides to inline either your method or its caller.&amp;#160; Since caller info attributes are resolved at compile-time, they are immune to inlining.&amp;#160; Also, stack walking can only give line number and filename info if the calling assembly’s PDB file is present at runtime, whereas [CallerFileName] and [CallerLineNumber] will always work.&amp;#160; &lt;/p&gt;  &lt;p&gt;Caller info attributes are also unaffected by compiler transformations.&amp;#160; If you do a stack walk from inside a lambda expression, iterator, or async method, you’ll see the compiler-generated method; there is no good way to retrieve the name of the original method.&amp;#160; Since caller info attributes are processed by the compiler, [CallerMemberName] should correctly pass the name of the containing method (although I cannot verify this).&amp;#160; Note that stack walking will retrieve the correct line number even inside lambda expressions (assuming there is a PDB file)&lt;/p&gt;  &lt;h2&gt;Advantages To Stack Walking&lt;/h2&gt;  &lt;p&gt;There are still a couple of reasons to use the StackTrace class.&amp;#160; Obviously, if you want to see more than one frame (if you want your caller’s caller), you need to use StackTrace.&amp;#160; Also, if you want to support clients in languages that don’t feature caller info attributes, such as C# 4 or F#, you should walk the stack instead.&lt;/p&gt;  &lt;p&gt;Stack walking is the only way to find the caller’s type or assembly (or a MemberInfo object), since caller info attributes do not provide this information.&lt;/p&gt;  &lt;p&gt;Stack walking is also the only choice for security-related scenarios.&amp;#160; It should be obvious that caller info attributes can &lt;em&gt;trivially&lt;/em&gt; be spoofed; nothing prevents the caller from passing arbitrary values as the optional parameter.&amp;#160; Stack walking, by contrast, happens within the CLR and cannot be spoofed.&amp;#160; &lt;/p&gt;  &lt;p&gt;Note that there are some other concerns with stack walking for security purposes. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Filename and line number information can be spoofed by providing a modified PDB file&lt;/li&gt;    &lt;li&gt;Class and function names are meaningless; your enemy can name his function whatever he wants to.&amp;#160; All you should rely on is the assembly name.&lt;/li&gt;    &lt;li&gt;There are various ways for an attacker to cause your code to be called indirectly (eg, DynamicInvoking a delegate) so that his assembly isn’t the direct caller.&amp;#160; In fact, if the attacker invokes your delegate on a UI thread (by calling BeginInvoke), his assembly won’t show up on the callstack at all.&amp;#160; The attacker can also compile a dynamic assembly that calls your function and call into that assembly.&lt;/li&gt;    &lt;li&gt;As always, beware of inlining&lt;/li&gt; &lt;/ul&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-5376149243877966549</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/10/subtleties-of-c-5s-new-callermembername.html"/>
		<title type="text">Subtleties of C# 5’s new [CallerMemberName]</title>
		<updated>2011-10-18T00:31:00+00:00</updated>
		<published>2012-03-01T07:02:51+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="design" term="design" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="caller-info-attributes" term="caller-info-attributes" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C# 5" term="csharp-5" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: Now that the Visual Studio 11 beta has shipped with this feature implemented, I wrote a &lt;a href=&quot;/2012/03/exploring-caller-info-attributes.html&quot;&gt;separate blog post&lt;/a&gt; exploring how it actually behaves in these corner cases.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;/2011/10/subtleties-of-c-5s-new-callerlinenumber.html&quot;&gt;Last time&lt;/a&gt;, I explored various pathological code samples in which the [CallerLineNumber] attribute does not have obvious behavior.&amp;#160; This time, I’ll cover the last of these new &lt;a href=&quot;/2011/10/subtleties-of-new-caller-info.html&quot;&gt;caller info attributes&lt;/a&gt;: [CallerMemberName].&lt;/p&gt;  &lt;p&gt;The [CallerMemberName] attribute tells the compiler to insert the name of the containing member instead of a parameter’s default value.&amp;#160; Unlike [CallerLineNumber] and [CallerFileName], this has no equivalent in C++; since the C / C++ versions of these features are in the preprocessor, they cannot be aware of member names.&lt;/p&gt;  &lt;p&gt;Most calls to methods with optional parameters take place within a named method, so the behavior of this attribute is usually obvious.&amp;#160; However, there are a couple of places where the exact method name is not so obvious.&lt;/p&gt;  &lt;p&gt;If you call a [CallerMemberName] method inside a property or event accessor, what name should the compiler pass?&amp;#160; Common sense indicates that it should pass the name of the property or event, since that’s the name you actually see in source code.&amp;#160; That would also allow this attribute to be &lt;a href=&quot;https://www.robfe.com/2011/09/raising-the-right-propertychanged-with-c-5s-caller-info-attributes/&quot;&gt;used for raising PropertyChanged events&lt;/a&gt;.&amp;#160; However, this option doesn’t pass enough information, since it would not be possible to determine whether it was called from the getter or the setter.&amp;#160; To expose the maximal amount of information, the compiler should pass the name of the actual method for the accessor – &lt;code&gt;get_Name&lt;/code&gt; or &lt;code&gt;set_Name&lt;/code&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;I would assume that the compiler only passes the property name, since that is what most people would probably expect.&lt;/p&gt;  &lt;p&gt;A less-trivial question arises when such a method is called from a constructor or static constructor.&amp;#160; Should the compiler just pass the name of the class, since that’s what the member is named in source code? If so, there would be no way to distinguish between an instance constructor and a static constructor.&amp;#160; Should the compiler pass the actual names of the CLR methods (&lt;code&gt;.ctor&lt;/code&gt; and &lt;code&gt;.cctor&lt;/code&gt;)?&amp;#160; If so, there would be no way to tell the class name, which is worse.&amp;#160; Should it pass both (&lt;code&gt;ClassName.ctor&lt;/code&gt;)? That would expose the maximal amount of information, but wouldn’t match the behavior in other members, which does not include the class name.&lt;/p&gt;  &lt;p&gt;On a related note, what about calls to &lt;code&gt;base&lt;/code&gt; or &lt;code&gt;this&lt;/code&gt; constructors that take [CallerMemberName] arguments? Is that considered part of the class’ constructor, even though the call is lexically scoped outside the constructor?&amp;#160; If not, what should it pass?&lt;/p&gt;  &lt;p&gt;A further related concern is field initializers. Since field initializers aren’t explicitly in any member, what should the compiler pass if you call a [CallerMemberName] method in a field initializer? I would assume that they’re treated like contructors (or static constructors for static field initializers)&lt;/p&gt;  &lt;p&gt;I would assume that a call to a [CallerMemberName] method from within an anonymous method or LINQ query would use the name of the parent method.&lt;/p&gt;  &lt;p&gt;The most interesting question concerns attributes.&amp;#160; What should happen if you declare your own custom attribute that takes a [CallerMemberName] parameter, then apply the attribute somewhere without specifying the parameter?&lt;/p&gt;  &lt;p&gt;If you place the attribute on a parameter, return value, or method, it would make sense for the compiler to pass the name of the method that the attribute was applied to.&amp;#160; If you apply the attribute to a type, it might make sense to pass the name of that type.&amp;#160; However, there is no obvious choice for attributes applied to a module or assembly.&lt;/p&gt;  &lt;p&gt;I suspect that they instead chose to not pass these caller info in default parameters for attribute declarations, and to instead pass the parameters’ declared default values.&amp;#160; If so, it would make sense to disallow caller info attributes in attribute constructor parameters.&amp;#160; However, this would also prevent them from being used for attributes that are explicitly instantiated in normal code (eg, for global filters in MVC).&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;/2011/10/caller-info-attributes-vs-stack-walking.html&quot;&gt;&lt;em&gt;Next Time:&lt;/em&gt; Caller Info Attributes vs. Stack Walking&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2928383119600063808</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/10/subtleties-of-c-5s-new-callerlinenumber.html"/>
		<title type="text">Subtleties of C# 5’s new [CallerLineNumber]</title>
		<updated>2011-10-07T15:20:00+00:00</updated>
		<published>2012-03-01T07:02:20+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="design" term="design" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="caller-info-attributes" term="caller-info-attributes" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C# 5" term="csharp-5" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: Now that the Visual Studio 11 beta has shipped with this feature implemented, I wrote a &lt;a href=&quot;/2012/03/exploring-caller-info-attributes.html&quot;&gt;separate blog post&lt;/a&gt; exploring how it actually behaves in these corner cases.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;This is part 2 in a series about C# 5’s new caller info attributes; see the &lt;a href=&quot;/2011/10/subtleties-of-new-caller-info.html&quot;&gt;introduction&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The [CallerLineNumber] attribute tells the compiler to use the line number of the call site instead of the parameter’s default value.&amp;#160; This attribute has more corner cases than [CallerFileName].&amp;#160; In particular, unlike the C preprocessor’s &lt;code&gt;__LINE__&lt;/code&gt; macro, the C# compiler inserts the line number of a parsed method call.&amp;#160; Therefore, it is not always clear which line a method call expression maps to.&lt;/p&gt;  &lt;p&gt;What should this call print:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Utils&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CallerLineNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Utils&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;GetLine&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Should it print the line number that the statement started? The line in which the call to &lt;code&gt;GetLine&lt;/code&gt; started? The line containing the parentheses for &lt;code&gt;GetLine&lt;/code&gt;? What if it’s in a multi-line lambda expression?&lt;/p&gt;

&lt;p&gt;There are also a few cases in which methods are called implicitly by the compiler without appearing in source code.&amp;#160; What should this code print?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Funny&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Funny&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CallerLineNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;: &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Funny&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
         &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; 
         &lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code contains two implicit calls to the &lt;code&gt;Select &lt;/code&gt;method that don’t have a clear source line (it gets worse for more complicated LINQ queries)&lt;/p&gt;

&lt;p&gt;In fact, it is possible to have an implicit method call with no corresponding source code at all.&lt;/p&gt;

&lt;p&gt;Consider this code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Loggable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Loggable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CallerLineNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Loggable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The compiler will implicitly generate a constructor for &lt;code&gt;SomeClass&lt;/code&gt; that calls the base &lt;code&gt;Loggable &lt;/code&gt;constructor with its default parameter value.&amp;#160; What line should it pass? In fact, if &lt;code&gt;SomeClass&lt;/code&gt; is a partial class that is defined in multiple files, it isn’t even clear what [CallerFileName] should pass.&lt;/p&gt;

&lt;p&gt;Also, what should happen in the unlikely case that a [CallerLineNumber] method is called on line 3 billion (which would overflow an &lt;code&gt;int&lt;/code&gt;)? (This would be easier to test on Roslyn with a fake stream source) Should it give an integer overflow compile-time error?&amp;#160; If [CallerLineNumber] also supports &lt;code&gt;byte&lt;/code&gt; and &lt;code&gt;short&lt;/code&gt; parameters, this scenario will be more likely to happen in practice.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/10/subtleties-of-c-5s-new-callermembername.html&quot;&gt;&lt;em&gt;Next time:&lt;/em&gt; [CallerMemberName]&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-6749814758208757359</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/10/subtleties-of-new-caller-info.html"/>
		<title type="text">Subtleties of the new Caller Info Attributes in C# 5</title>
		<updated>2011-10-06T13:02:00+00:00</updated>
		<published>2012-03-01T07:01:28+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="design" term="design" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="caller-info-attributes" term="caller-info-attributes" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C# 5" term="csharp-5" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: Now that the Visual Studio 11 beta has shipped with this feature implemented, I wrote a &lt;a href=&quot;/2012/03/exploring-caller-info-attributes.html&quot;&gt;separate blog post&lt;/a&gt; exploring how it actually behaves in these corner cases.&lt;/p&gt;  &lt;p&gt;C# 5 is all about asynchronous programming.&amp;#160; However, in additional to the new async features, the C# team managed to slip in a much simpler feature: Caller Info Attributes.&lt;/p&gt;  &lt;p&gt;Since C#’s inception, developers have asked for &lt;code&gt;__LINE__&lt;/code&gt; and &lt;code&gt;__FILE__&lt;/code&gt; macros like those in C and C++.&amp;#160; Since C# intentionally does not support macros, these requests have not been answered.&amp;#160; Until now.&lt;/p&gt;  &lt;p&gt;C# 5 adds these features using attributes and optional parameters.&amp;#160; As Anders Hejlsberg &lt;a href=&quot;https://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-816T&quot;&gt;presented&lt;/a&gt; at //Build/, you can write&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CallerFilePath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CallerLineNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CallerMemberName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{0}:{1} – {2}: {3}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                      &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If this method is called in C# 5 without specifying the optional parameters, the compiler will insert the file name, line number, and containing member name instead of the default values.&amp;#160; If it’s called in older languages that don’t support optional parameters, those languages will pass the default values, like any other optional method.&lt;/p&gt;

&lt;p&gt;These features look trivial at first glance.&amp;#160; However, like most features, they are actually more complicated to design in a solid and robust fashion.&amp;#160; Here are some of the less obvious issues that the C# team needed to deal with when creating this feature:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer&lt;/em&gt;: I am basing these posts entirely on&amp;#160; logical deduction.&amp;#160; I do not have access to a specification or implementation of this feature; all I know is what Anders announced in his //Build/ presentation.&amp;#160; However, the C# team would have needed to &lt;em&gt;somehow&lt;/em&gt; deal with each of thee issues.&amp;#160; Since these attributes are not yet supported by any public CTP, I can’t test my assumptions&lt;/p&gt;

&lt;p&gt;To start with, they needed to create new compiler errors if the attribute is applied to an incorrectly-typed parameter, or a non-optional parameter.&amp;#160; Creating compiler errors is expensive; they need to be documented, tested, and localized into every language supported by C#.&lt;/p&gt;

&lt;p&gt;The attributes should perhaps support nullable types, or parameter types with custom implicit conversions to &lt;code&gt;int&lt;/code&gt; or &lt;code&gt;string&lt;/code&gt;.&amp;#160; (especially &lt;code&gt;long&lt;/code&gt; or &lt;code&gt;short&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;If a method with these attributes is called in an expression tree literal (a lambda expression converted to an Expression&amp;lt;TDelegate&amp;gt;), the compiler would need to insert &lt;code&gt;ConstantExpression&lt;/code&gt;s with the actual line number or other info to pass as the parameter.&lt;/p&gt;

&lt;p&gt;In addition to being supported in methods, the attributes should also be supported on parameters&amp;#160; for delegate types, allowing you to write&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LinePrinter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CallerLineNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;LinePrinter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Each of the individual attributes also has subtle issues.&lt;/p&gt;

&lt;p&gt;[CallerFilePath] seems fairly simple.&amp;#160; Any function call must happen in source code, in a source file that has a path.&amp;#160; However, it needs to take into account &lt;code&gt;#line&lt;/code&gt; directives, so that, for example, it will work as expected in &lt;a href=&quot;/2011/02/dissecting-razor-part-5-use-source-luke.html&quot;&gt;Razor views&lt;/a&gt;.&amp;#160; I don’t know what it does inside &lt;code&gt;#line hidden&lt;/code&gt;, in which there isn’t a source file.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/10/subtleties-of-c-5s-new-callerlinenumber.html&quot;&gt;&lt;em&gt;Next time&lt;/em&gt;: [CallerLineNumber]&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-5195404507544876602</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/09/using-default-controller-in-aspnet-mvc.html"/>
		<title type="text">Using a default controller in ASP.Net MVC</title>
		<updated>2011-09-19T02:01:00+00:00</updated>
		<published>2011-09-19T02:01:23+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="routing" term="routing" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net MVC" term="asp-net-mvc" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;One common question about ASP.Net MVC is how to make “default” controller.&lt;/p&gt;  &lt;p&gt;Most websites will have a &lt;code&gt;Home&lt;/code&gt; controller with actions like &lt;code&gt;About&lt;/code&gt;, &lt;code&gt;FAQ&lt;/code&gt;, &lt;code&gt;Privacy&lt;/code&gt;, or similar pages.&amp;#160; Ordinarily, these actions can only be accessed through URLs like &lt;code&gt;~/Home/About&lt;/code&gt;.&amp;#160; Most people would prefer to put these URLs directly off the root: &lt;code&gt;~/About&lt;/code&gt;, etc.&lt;/p&gt;  &lt;p&gt;Unfortunately, there is no obvious way to do that in ASP.Net MVC without making a separate route or controller for each action.&lt;/p&gt;  &lt;p&gt;You cannot simply create a route matching &lt;code&gt;&amp;quot;/{action}&amp;quot;&lt;/code&gt; and map it to the &lt;code&gt;Home&lt;/code&gt; controller, since such a route would match any URL with exactly one term, including URLs meant for other controllers.&amp;#160; Since the routing engine is not aware of MVC actions, it doesn’t know that this route should only match actions that actually exist on the controller.&lt;/p&gt;  &lt;p&gt;To make it work, we can add a &lt;a href=&quot;https://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx&quot;&gt;custom route constraint&lt;/a&gt; that forces this route to only match URLs that correspond to actual methods on the controller.&lt;/p&gt;  &lt;p&gt;To this end, I wrote an extension method that scans a controller for all action methods and adds a route that matches actions in that controller. The code is available at &lt;a title=&quot;https://gist.github.com/1225676&quot; href=&quot;https://gist.github.com/1225676&quot;&gt;gist.github.com/1225676&lt;/a&gt;.&amp;#160; It can be used like this:&lt;/p&gt;
&lt;div class=&quot;small&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MapDefaultController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Controllers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HomeController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This maps the route &lt;code&gt;&amp;quot;/{action}/{id}&amp;quot;&lt;/code&gt; (with &lt;code&gt;id&lt;/code&gt; optional) to all actions defined in &lt;code&gt;HomeController&lt;/code&gt;.&amp;#160;&amp;#160; Note that this code ignores custom &lt;code&gt;ActionNameSelectorAttribute&lt;/code&gt;s. (The built-in &lt;code&gt;[ActionName(…)]&lt;/code&gt; &lt;strong&gt;is &lt;/strong&gt;supported)&lt;/p&gt;

&lt;p&gt;For additional flexibility, you can also create custom routes that will only match actions in a specific controller.&amp;#160; This is useful if you have a single controller with a number of actions that has special route requirements that differ from the rest of your site.&lt;/p&gt;

&lt;p&gt;For example: &lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MapControllerActions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UsersController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;User routes&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;s&quot;&gt;&quot;{userName}/{action}&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;defaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Index&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;(Note that this example will also match URLs intended for other controllers with the same actions; plan your routes carefully)&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2272047003100374809</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/09/xregexp-breaks-jquery-animations.html"/>
		<title type="text">XRegExp breaks jQuery Animations</title>
		<updated>2011-09-15T01:12:00+00:00</updated>
		<published>2011-09-15T01:12:34+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="jQuery" term="jquery" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="bugs" term="bugs" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Update&lt;/strong&gt;: This bug was fixed in &lt;a href=&quot;https://regexp.com/history/#v1-5-1&quot;&gt;XRegExp 1.5.1&lt;/a&gt;.&lt;/em&gt;&lt;br /&gt;
However, as far as I know, there are no released versions of SyntaxHighlighter that contain the fix.&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a href=&quot;http://regexp.com/&quot;&gt;XRegExp &lt;/a&gt;is an open source JavaScript library that provides an augmented, extensible, cross-browser implementation of regular expressions, including support for additional syntax, flags, and methods.&lt;/p&gt;  &lt;p&gt;It’s used by the popular &lt;a href=&quot;https://alexgorbatchev.com/SyntaxHighlighter/&quot;&gt;SyntaxHighlighter script&lt;/a&gt;, which is in turn used by many websites (including this blog) to display syntax-highlighted source code on the client.&amp;#160; Thus, XRegExp has a rather wide usage base.&lt;/p&gt;  &lt;p&gt;However, XRegExp conflicts with jQuery.&amp;#160; In IE, any page that includes XRegExp and runs a numeric jQuery animation will result in “TypeError: Object doesn't support this property or method”.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://jsfiddle.net/SLaks/LR7vA/&quot;&gt;Demo&lt;/a&gt; (only fails in IE)&lt;/p&gt;  &lt;p&gt;This bug is caused by an XRegExp fix for an IE bug in which the &lt;code&gt;exec&lt;/code&gt; method doesn't consistently return &lt;code&gt;undefined&lt;/code&gt; for nonparticipating capturing groups.&amp;#160; The fix, on line 271, assumes that the parameter passed to &lt;code&gt;exec&lt;/code&gt; is a string.&amp;#160; This behavior violates the ECMAScript standard (&lt;a href=&quot;https://ecma262-5.com/ELS5_HTML.htm#Section_15.10.6.2&quot;&gt;section 15.10.6.2&lt;/a&gt;), which states that the parameter to &lt;code&gt;exec&lt;/code&gt; should be converted to a string before proceeding.&lt;/p&gt;  &lt;p&gt;jQuery relies on this behavior in its &lt;code&gt;animate&lt;/code&gt; method, which parses a number using a regex to get the decimal portion.&amp;#160; (&lt;a href=&quot;https://github.com/jquery/jquery/blob/1.6.4/src/effects.js#L215&quot;&gt;source&lt;/a&gt;)&lt;/p&gt;  &lt;p&gt;Thus, calling &lt;code&gt;animate&lt;/code&gt; with a number after loading XRegExp will fail in IE when XRegExp tries to call &lt;code&gt;slice&lt;/code&gt; on a number.&lt;/p&gt;  &lt;p&gt;Fortunately, it is very simple to fix XRegExp to convert its argument to a string first:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;XRegExp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xExec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;RegExp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;RegExp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xExec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;a href=&quot;https://jsfiddle.net/SLaks/LR7vA/2/&quot;&gt;Here&lt;/a&gt; is an updated demo that uses this fix and works even in IE.
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-6240697587277288616</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/09/clarifying-boolean-parameters-part-2.html"/>
		<title type="text">Clarifying Boolean Parameters, part 2</title>
		<updated>2011-09-14T15:30:00+00:00</updated>
		<published>2011-09-14T15:30:38+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="parameters" term="parameters" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="VB.Net" term="vb-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="functions" term="functions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;em&gt;Part 1 is &lt;a href=&quot;/2011/09/clarifying-boolean-parameters-part-1.html&quot;&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Some languages have better ways to pass boolean parameters.&amp;#160; C# 4.0, and all versions of VB, allow parameters to be passed by name.&amp;#160; This allows us to write much clearer code: &lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//C# 4.0:&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;UpdateLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doFullLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vbnet&quot; data-lang=&quot;vbnet&quot;&gt;'VB.Net:
UpdateLayout(doFullLayout:=False) &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Without requiring any changes to the function definition, this makes the meaning of the &lt;code&gt;true&lt;/code&gt; / &lt;code&gt;false&lt;/code&gt; abundantly clear at the call-site.&lt;/p&gt;

&lt;p&gt;Javascript offers another interesting alternative.&amp;#160; In Javascript, booleans conditions actually check for “truthyness”.&amp;#160; The statement &lt;code&gt;if(x)&lt;/code&gt; will trigger&amp;#160; not just if &lt;code&gt;x&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;, but also if &lt;code&gt;x&lt;/code&gt; is any “truthy” value, including any object, non-empty string, or non-zero number. Similarly, the expression &lt;code&gt;!x&lt;/code&gt; will return &lt;code&gt;false&lt;/code&gt; if &lt;code&gt;x&lt;/code&gt; is “truthy” and &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;x&lt;/code&gt; “falsy”.&lt;/p&gt;

&lt;p&gt;This means that we can actually use any non-empty string instead of &lt;code&gt;true&lt;/code&gt; in Javascript.&amp;#160; Note that this will only work if the function checks the value for “truthyness”; it won’t work for code like &lt;code&gt;if (x === true)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thus, instead of passing &lt;code&gt;true&lt;/code&gt; as a boolean, you can pass a string that describes what you’re actually indicating.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;updatePosition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;animate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//Calculate position&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;animate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//...&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;updatePosition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;updatePosition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;With animation&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Although this results in much more readable code, it can be difficult to understand for people who aren’t familiar with this trick.&amp;#160; If the meaning of the parameter changes, you’ll need to hunt down every place that the function is called and change the string to reflect the new meaning.&lt;/p&gt;

&lt;p&gt;Finally, unlike an enum, this does not scale to multiple options.&amp;#160; If you need to have more than two options, you should use global variables or objects to simulate an enum, not strings.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2694072100214824965</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/09/clarifying-boolean-parameters-part-1.html"/>
		<title type="text">Clarifying Boolean Parameters, part 1</title>
		<updated>2011-09-12T21:31:00+00:00</updated>
		<published>2011-09-14T15:31:24+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="parameters" term="parameters" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Have you ever written code like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UpdateLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doFullLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//Code&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doFullLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//Expensive code&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//More code&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This pattern is commonly used when some operation has a “cheap” mode and an “expensive” mode.&amp;#160; Other code will have calls like &lt;code&gt;UpdateLayout(false)&lt;/code&gt; and &lt;code&gt;UpdateLayout(true)&lt;/code&gt; scattered throughout.&lt;/p&gt;

&lt;p&gt;The problem is that this isn’t very obvious for people who aren’t familiar with the codebase.&amp;#160; If you take a look at a file you’ve never seen before and see calls like &lt;code&gt;UpdateLayout(false)&lt;/code&gt; and &lt;code&gt;UpdateLayout(true)&lt;/code&gt; scattered, you’ll have no idea what the true / false means.&lt;/p&gt;

&lt;p&gt;The simplest solution is to break it out into two methods: &lt;code&gt;UpdateComplexLayout()&lt;/code&gt; and &lt;code&gt;UpdateBasicLayout()&lt;/code&gt;.&amp;#160; However,&amp;#160; if the two different layout modes have intertwined code paths (eg, the code before and after the &lt;code&gt;if&lt;/code&gt; above), this either won’t be possible or will lead to ugly duplication of code.&lt;/p&gt;

&lt;p&gt;One alternative is to use enums: &lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayoutUpdateType&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Basic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Full&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UpdateLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LayoutUpdateType&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//Code&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayoutUpdateType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Full&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//Expensive code&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//More code&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This way, the callsites are much more descriptive: &lt;code&gt;UpdateLayout(LayoutUpdateType.Full)&lt;/code&gt;.&amp;#160; This also makes it easy to add more update modes in the future should the need arise.&amp;#160; However, it makes the callsites much more verbose.&amp;#160; When used frequently, this pattern can lead a vast proliferation of enum types that are each only used by one method, polluting the namespace and making more important enums harder to notice.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/09/clarifying-boolean-parameters-part-2.html&quot;&gt;&lt;em&gt;Next time&lt;/em&gt;: Cleverer alternatives&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8075220932376926044</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/09/c-is-not-type-safe.html"/>
		<title type="text">C# is not type-safe</title>
		<updated>2011-09-08T02:12:00+00:00</updated>
		<published>2011-09-08T02:12:06+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="type-safety" term="type-safety" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;C# is usually touted as a type-safe language.&amp;#160; However, it is not actually fully type-safe!&lt;/p&gt;  &lt;p&gt;To examine this claim, we must first provide a strict definition of type-safety.&amp;#160;&lt;br /&gt;Wikipedia &lt;a href=&quot;https://en.wikipedia.org/wiki/Type_safety&quot;&gt;says&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;In &lt;a href=&quot;https://en.wikipedia.org/wiki/Computer_science&quot;&gt;computer science&lt;/a&gt;, &lt;b&gt;type safety&lt;/b&gt; is the extent to which a &lt;a href=&quot;https://en.wikipedia.org/wiki/Programming_language&quot;&gt;programming language&lt;/a&gt; discourages or prevents &lt;i&gt;type errors&lt;/i&gt;. A type error is erroneous or undesirable program behavior caused by a discrepancy between differing &lt;a href=&quot;https://en.wikipedia.org/wiki/Data_type&quot;&gt;data types&lt;/a&gt;. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;To translate this to C#, full type-safety means that any expression that compiles is guaranteed to work at runtime, without causing any invalid cast errors.&lt;/p&gt;  &lt;p&gt;Obviously, the cast (and &lt;code&gt;as&lt;/code&gt;) operator is an escape hatch from type safety.&amp;#160; It tells the compiler that “I expect this value to actually be of this type, even though you can’t prove it.&amp;#160; If I’m wrong, I’ll live with that”.&amp;#160; Therefore, to be fully type-safe, it must be impossible to get an InvalidCastException at runtime in C# code that does not contain an explicit cast.&lt;/p&gt;  &lt;p&gt;Note that parsing or conversion errors (such as any exception from the &lt;code&gt;Convert&lt;/code&gt; class) don’t count.&amp;#160; Parsing errors aren’t actually invalid cast errors (instead, they come from unexpected strings), and conversion errors from from cast operations inside the &lt;code&gt;Convert&lt;/code&gt; class.&amp;#160; Also, null reference exceptions aren’t cast errors.&amp;#160; &lt;/p&gt;  &lt;p&gt;So, why isn’t C# type-safe?&lt;/p&gt;  &lt;p&gt;MSDN says that &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.invalidcastexception.aspx&quot;&gt;InvalidCastException&lt;/a&gt; is thrown in two conditions:&lt;/p&gt;  &lt;blockquote&gt;   &lt;ul&gt;     &lt;li&gt;       &lt;p&gt;For a conversion from a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.single.aspx&quot;&gt;Single&lt;/a&gt; or a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.double.aspx&quot;&gt;Double&lt;/a&gt; to a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.decimal.aspx&quot;&gt;Decimal&lt;/a&gt;, the source value is infinity, Not-a-Number (NaN), or too large to be represented as the destination type.&lt;/p&gt;     &lt;/li&gt;      &lt;li&gt;       &lt;p&gt;A failure occurs during an explicit reference conversion.&lt;/p&gt;     &lt;/li&gt;   &lt;/ul&gt; &lt;/blockquote&gt;  &lt;p&gt;Both of these conditions can only occur from a cast operation, so it looks like C# is in fact type safe.&lt;/p&gt;  &lt;p&gt;Or is it?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code compiles (!). Running it results in &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;On the &lt;code&gt;foreach&lt;/code&gt; line.&lt;/p&gt;

&lt;p&gt;Since we don’t have any explicit cast operations (The implicit conversion from &lt;code&gt;int[]&lt;/code&gt; to &lt;code&gt;IEnumerable&lt;/code&gt; is an &lt;em&gt;implicit&lt;/em&gt; conversion, which is guaranteed to succeed) , this proves that C# is not type-safe.&lt;/p&gt;

&lt;p&gt;What happened?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;foreach&lt;/code&gt; construct comes from C# 1.0, before generics existed.&amp;#160; It worked with untyped collections such as &lt;code&gt;ArrayList&lt;/code&gt; or &lt;code&gt;IEnumerable&lt;/code&gt;.&amp;#160; Therefore, the &lt;code&gt;IEnumerator.Current&lt;/code&gt; property that gets assigned to the loop variable would usually be of type &lt;code&gt;object&lt;/code&gt;.&amp;#160;&amp;#160; (In fact, the &lt;code&gt;foreach&lt;/code&gt; statement is &lt;a href=&quot;https://blogs.msdn.com/b/kcwalina/archive/2007/07/18/ducknotation.aspx&quot;&gt;duck-typed&lt;/a&gt; to allow the enumerator to provide a typed &lt;code&gt;Current&lt;/code&gt; property, particularly to avoid boxing).&amp;#160; &lt;/p&gt;

&lt;p&gt;Therefore, you would expect that almost all (non-generic) &lt;code&gt;foreach&lt;/code&gt; loops would need to have the loop variable declared as &lt;code&gt;object&lt;/code&gt;, since that’s the compile-time type of the items in the collection.&amp;#160; Since that would be extremely annoying, the compiler allows you to use any type you want, and will implicitly cast the &lt;code&gt;Current&lt;/code&gt; values to the type you declared.&amp;#160; Thus, mis-declaring the type results in an InvalidCastException.&lt;/p&gt;

&lt;p&gt;Note that if the &lt;code&gt;foreach&lt;/code&gt; type isn’t compatible at all with the type of the &lt;code&gt;Current&lt;/code&gt; property, you will get a compile-time error (just like &lt;code&gt;(string)42&lt;/code&gt; doesn’t compile).&amp;#160; Therefore, if you stick with generic collections, you’re won’t get these runtime errors (unless you declare the &lt;code&gt;foreach&lt;/code&gt; as a subtype of the item type).&lt;/p&gt;

&lt;p&gt;C# also isn’t type-safe because of &lt;a href=&quot;https://blogs.msdn.com/b/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx&quot;&gt;array covariance&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/p&gt;

&lt;p&gt;This code compiles, but throws “ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.” at run-time.&lt;/p&gt;

&lt;p&gt;As Eric Lippert &lt;a href=&quot;https://blogs.msdn.com/b/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx&quot;&gt;explains&lt;/a&gt;, this feature was added in order to be more compatible with Java.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2185164814419059282</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/08/delegates-vs-function-pointers-addendum.html"/>
		<title type="text">Delegates vs. Function Pointers, Addendum: Multicast Delegates</title>
		<updated>2011-08-16T01:02:00+00:00</updated>
		<published>2011-08-16T01:02:38+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="delegates" term="delegates" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Until now, I've been focusing on only one of the differences between delegates and function pointers; namely, associated state.    &lt;br /&gt;Delegates have one other capability that function pointers do not.&amp;#160; A single function pointer can only point to one function.&amp;#160; .Net, on the other hand, supports multicast delegates – delegates that point to &lt;em&gt;multiple&lt;/em&gt; functions.&amp;#160; You can combine two existing delegates using the + operator (or by calling Delegate.Combine) to create a single new delegate instance that points two all of the methods in the original two delegates.&amp;#160; This new delegate stores all of the methods from the original two delegates in a private array of delegates called InvocationList (the delegates in this array are ordinary non-multicast delegates that each only point to a single method).&amp;#160; &lt;/p&gt;  &lt;p&gt;Note that delegates, like strings, are immutable.&amp;#160; Adding two delegates together creates a third delegate containing the methods from the first two; the original delegate instances are not affected.&amp;#160; For example, writing &lt;code&gt;delegateField += SomeMethod&lt;/code&gt; creates a new delegate instance containing the methods originally in &lt;code&gt;delegateField&lt;/code&gt; as well as &lt;code&gt;SomeMethod&lt;/code&gt;, then stores this new instance in &lt;code&gt;delegateField&lt;/code&gt;.&lt;/p&gt;  &lt;p&gt;Similarly, the - operator (or Delegate.Remove) will remove the second operand from the first one (again, returning a new delegate instance).&amp;#160; If the second operand has multiple methods, all of them will be removed from the final delegate.&amp;#160; If some of the methods in the second operand appear multiple times in the original delegate, only the last occurrence of each one will be removed (the one most recently added).&amp;#160; The RemoveAll method will remove all occurrences.&amp;#160; If all of the methods were removed, it will return null; there is no such thing as an empty delegate instance.&lt;/p&gt;  &lt;p&gt;Multicast delegates are not intended to be used with delegates that return values.&amp;#160; If you call a non-void delegate that contains multiple methods, it will return the return value of the last method in the delegate.&amp;#160; If you want to see the return values of all of the methods, you’ll need to loop over GetInvocationList() and call each delegate individually.&lt;/p&gt;  &lt;p&gt;Multicast delegates also don’t play well with the new covariant and contravariant generic delegates in .Net 4.0.&amp;#160; You cannot combine two delegates unless their types match exactly, including variant generic parameters.&lt;/p&gt;  &lt;p&gt;Function pointers cannot easily be combined the way multicast delegates can.&amp;#160; The only way to combine function pointers without cooperation from the code that calls the pointer is to make a function that uses a closure to call all of the function pointers you want to call.&lt;/p&gt;  &lt;p&gt;In Javascript, that would look like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;combine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;methods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;retVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
            &lt;span class=&quot;nx&quot;&gt;retVal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;retVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-3943524177840514075</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/07/tracking-event-handler-registrations.html"/>
		<title type="text">Tracking Event Handler Registrations</title>
		<updated>2011-07-29T19:58:00+00:00</updated>
		<published>2011-07-29T19:58:48+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="events" term="events" />
		
		
		<category scheme="https://blog.slaks.net/#" label="debugging" term="debugging" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;When working with large .Net applications, it can be useful to find out where event handlers are being registered, especially in an unfamiliar codebase.&lt;/p&gt;  &lt;p&gt;In simple cases, you can do this by right-clicking the event definition and clicking Find All References (Shift+F12).&amp;#160; This will show you every line of code that adds or removes a handler from the event by name.&amp;#160; For field-like (ordinary) events, this will also show you every line of code that raises the event.&lt;/p&gt;  &lt;p&gt;However, this isn’t always good enough.&amp;#160; Sometimes, event handlers are not added by name.&amp;#160; The .Net data-binding infrastructure, as well as the CompositeUI &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ff650653.aspx&quot;&gt;Event Broker service&lt;/a&gt;, will add and remove event handlers using reflection, so they won’t be found by Find All References.&amp;#160; Similarly, if an event handler is added by an external DLL, Find All References won’t find it.&lt;/p&gt;  &lt;p&gt;For these scenarios, you can use a less-obvious trick.&amp;#160; As I described &lt;a href=&quot;/2011/07/about-net-events.html&quot;&gt;last time&lt;/a&gt;, adding or removing an event handler actually executes code inside of an accessor method. Like any other code, we can set a breakpoint to see where the code is executed. &lt;/p&gt;  &lt;p&gt;For custom events, this is easy.&amp;#160; Just add a breakpoint in the &lt;code&gt;add&lt;/code&gt; and/or &lt;code&gt;remove&lt;/code&gt; accessors and run your program.&amp;#160; Whenever a handler is added or removed, the debugger will break into the accessor, and you can look at the callstack to determine where it’s coming from.&lt;/p&gt;  &lt;p&gt;However, most events are field-like, and don’t have actual source code in their accessor methods.&amp;#160; To set a breakpoint in a field-like event, you need to use a lesser-known feature: &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/15d1wtaf.aspx&quot;&gt;function breakpoints&lt;/a&gt; (Unfortunately, this feature is not available in Visual Studio Express).&amp;#160; You can click Debug, New Breakpoint, Break at Function (Ctrl+D, N) to tell the debugger to pause whenever a specific managed function is executed.&lt;/p&gt;  &lt;p&gt;To add a breakpoint at an event accessor, type &lt;code&gt;Namespace.ClassName.add_EventName&lt;/code&gt;.&amp;#160; To ensure that you entered it correctly, open the Debug, Breakpoints window (Ctrl+D, B) and check that the new breakpoint says &lt;strong&gt;break always (currently 0)&lt;/strong&gt; in the Hit Count column.&amp;#160; If it doesn’t say &lt;strong&gt;(currently 0)&lt;/strong&gt;, then either the assembly has not been loaded yet or you made a typo in the location (right-click the breakpoint and click Location).&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8241465683788933619</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/07/about-net-events.html"/>
		<title type="text">About .Net Events</title>
		<updated>2011-07-29T02:14:00+00:00</updated>
		<published>2011-07-29T22:38:04+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="events" term="events" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;A .Net event actually consists of a pair of &lt;em&gt;accessor methods&lt;/em&gt; named &lt;code&gt;add_&lt;em&gt;EventName&lt;/em&gt;&lt;/code&gt; and &lt;code&gt;remove_&lt;em&gt;EventName&lt;/em&gt;&lt;/code&gt;.&amp;#160; These functions each take a handler delegate, and are expected to add or remove that delegate from the list of event handlers.&amp;#160; &lt;/p&gt;  &lt;p&gt;In C#, writing &lt;code&gt;public event EventHandler EventName;&lt;/code&gt; creates a &lt;em&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/aa664455%28v=vs.71%29.aspx&quot;&gt;field-like event&lt;/a&gt;&lt;/em&gt;.&amp;#160; The compiler will automatically generate a private backing field (also a delegate), along with thread-safe accessor methods that add and remove handlers from the backing field (like an &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb384054.aspx&quot;&gt;auto-implemented property&lt;/a&gt;).&amp;#160; Within the class that declared the event, &lt;code&gt;EventName&lt;/code&gt; refers to this private backing field.&amp;#160; Thus, writing &lt;code&gt;EventName(...)&lt;/code&gt; in the class calls this field and raises the event (if no handlers have been added, the field will be &lt;code&gt;null&lt;/code&gt;).&lt;/p&gt;  &lt;p&gt;You can also write custom event accessors to gain full control over how handlers are added to your events.&amp;#160;&amp;#160; For example, this event will store and trigger handlers in reverse order:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ReversedEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ReversedEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ReversedEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;OnReversedEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;OnReversedEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reversedEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;reversedEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EventArgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reversedEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EventHandler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReversedEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;reversedEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reversedEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;remove&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;reversedEvent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This &lt;code&gt;add&lt;/code&gt; accessor uses the non-commutative delegate addition operator to prepend each new handler to the delegate field containing the existing handlers.&amp;#160; The raiser method simply calls the combined delegate in the private field. (which is &lt;code&gt;null&lt;/code&gt; if there aren’t any handlers)&lt;/p&gt;

&lt;p&gt;Note that this code is not thread-safe.&amp;#160; If two threads add a handler at the same time, both of them will read the original storage field, add their respective handlers to create a new delegate instance, then write this new delegate back to the field.&amp;#160; The thread that writes back to the field last will overwrite the changes made by the other thread, since it never saw the other thread’s handler (this is the same reason that &lt;code&gt;x += y&lt;/code&gt; is not thread-safe).&amp;#160; The accessors generated by the compiler are threadsafe, either by using &lt;code&gt;lock(this)&lt;/code&gt; (C# 3 or earlier) or a lock-free threadsafe implementation (C# 4).&amp;#160; For more details, see &lt;a href=&quot;https://www.google.com/search?q=site%3Amsdn.com+%22Events+get+a+little+overhaul+in+C%23+4%22&quot;&gt;this series of blog posts&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This example is rather useless.&amp;#160; However, there are better reasons to create custom event accessors. WinForms controls store their events in a special &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.componentmodel.eventhandlerlist.aspx&quot;&gt;&lt;code&gt;EventHandlerList&lt;/code&gt; class&lt;/a&gt; to save memory.&amp;#160; WPF controls create events using the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms742806.aspx&quot;&gt;Routed Event system&lt;/a&gt;, and store handlers in special storage in DependencyObject.&amp;#160; Custom event accessors can also be used to perform validation or &lt;a href=&quot;https://stackoverflow.com/q/6748320/34397&quot;&gt;logging&lt;/a&gt;.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1173428472759522726</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/07/creating-local-extension-methods.html"/>
		<title type="text">Creating Local Extension Methods</title>
		<updated>2011-07-27T02:08:00+00:00</updated>
		<published>2011-07-27T02:08:23+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="extension-methods" term="extension-methods" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Sometimes, it can be useful to make an extension method specifically for a single block of code.&amp;#160; Unfortunately, since extension methods cannot appear in nested classes, there is no obvious way to do that.&lt;/p&gt;  &lt;p&gt;Instead, you can create a child namespace containing the extension method.&amp;#160; In order to limit the extension method’s visibility to a single method, you can put that method in a separate namespace block.&amp;#160; This way, you can add a &lt;code&gt;using&lt;/code&gt; statement to that namespace alone.&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Company.Project&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;partial&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Company.Project&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;MyClassExtensions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;MyClassExtensions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Extensions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;(null &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;: &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
                     &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;{declared as &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;partial&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DoSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Since the &lt;code&gt;using MyClassExtensions&lt;/code&gt; statement appears inside the second namespace block, the extension methods are only visible within that block.&amp;#160; Code that uses these extension method can appear in this second block, while the rest of the class can go in the original namespace block without the extension methods.&lt;/p&gt;

&lt;p&gt;This technique should be avoided where possible, since it leads to confusing and non-obvious code.&amp;#160; However, there are situations in which this can make some code much more readable.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1429265338482423387</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/07/dont-modify-other-controls-during-wpf.html"/>
		<title type="text">Don’t modify other controls during a WPF layout pass</title>
		<updated>2011-07-26T02:18:00+00:00</updated>
		<published>2011-07-26T02:18:56+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="WPF" term="wpf" />
		
		
		<category scheme="https://blog.slaks.net/#" label="layout" term="layout" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Unlike WinForms or native Win32 development, WPF provides a rich &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms745058.aspx&quot;&gt;layout model&lt;/a&gt; which allows developers to easily create complicated UIs that resize to fit their contents or the parent window.&lt;/p&gt;  &lt;p&gt;However, when developing custom controls, it can be necessary to layout child controls manually by overriding the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride.aspx&quot;&gt;MeasureOverride&lt;/a&gt; and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.arrangeoverride.aspx&quot;&gt;ArrangeOverride&lt;/a&gt; methods.&amp;#160; To quote &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms750441.aspx#System_Windows_UIElement&quot;&gt;MSDN&lt;/a&gt;,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.windows.uielement.measure.aspx&quot;&gt;Measure&lt;/a&gt; allows a component to determine how much size it would like to take. This is a separate phase from &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.windows.uielement.arrange.aspx&quot;&gt;Arrange&lt;/a&gt; because there are many situations where a parent element will ask a child to measure several times to determine its optimal position and size. The fact that parent elements ask child elements to measure demonstrates another key philosophy of WPF – size to content. All controls in WPF support the ability to size to the natural size of their content. This makes localization much easier, and allows for dynamic layout of elements as things resize. The &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.windows.uielement.arrange.aspx&quot;&gt;Arrange&lt;/a&gt; phase allows a parent to position and determine the final size of each child.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Overriding these methods gives your custom control full power over the layout of its child element(s).&lt;/p&gt;  &lt;p&gt;Be careful what you do when overriding these methods.&amp;#160; Any code in MeasureOverride or ArrangeOverride runs during the WPF layout passes.&amp;#160; in these methods, you should not modify any part of the visual tree outside of the control you’re overriding in.&amp;#160; If you do, you’ll be changing the visuals between Measure() and Arrange(), which will have unexpected results.&lt;/p&gt;  &lt;p&gt;It is safe to modify your own child controls during the layout pass.&amp;#160; Before you call Measure() on a child control, its layout pass has not started.&amp;#160; Therefore, any changes will be seen by the child’s layout code.&amp;#160; Similarly, after you Arrange() a child control, its layout pass is finished, so it is safe to modify again (although you may end up triggering another layout pass to see the changes).&lt;/p&gt;  &lt;p&gt;If you do need to modify an outside control during the layout pass, you should call &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/cc190824.aspx&quot;&gt;Dispatcher.BeginInvoke()&lt;/a&gt; to run code asynchronously during the next message loop.&amp;#160; This way, your code will run after the layout pass finishes, and it will be able to safely modify whatever it wants.&lt;/p&gt;  &lt;p&gt;Note that Measure() can be called multiple times during a single layout pass (if a parent needs to iteratively determine the best fit for a child).&lt;/p&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-7978254654957666752</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/07/delegates-vs-function-pointers-part-5.html"/>
		<title type="text">Delegates vs. Function Pointers, part 5: Javascript</title>
		<updated>2011-07-25T00:33:00+00:00</updated>
		<published>2011-07-25T00:33:16+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="closures" term="closures" />
		
		
		<category scheme="https://blog.slaks.net/#" label="functions" term="functions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;i&gt;This is part 5 in a series about state and function pointers; part 1 is &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html&quot;&gt;here&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-4-c.html&quot;&gt;Last time&lt;/a&gt;, we saw how C# 2 supports closures by compiling anonymous functions into member functions of a special class that holds local state from the outer function.&amp;#160; &lt;/p&gt;  &lt;p&gt;Unlike the languages we’ve looked at before, Javascript has had closures baked in to the languages since its inception.&amp;#160; My &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html#example&quot;&gt;standard example&lt;/a&gt; can be achieved very simply in Javascript:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hugeNumbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code uses the &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter&quot;&gt;&lt;code&gt;Array.filter&lt;/code&gt; method&lt;/a&gt;, new to Javascript 1.6, to create a new array with those elements from the first array that pass a callback.&amp;#160; The function expression passed to &lt;code&gt;filter&lt;/code&gt; captures the &lt;code&gt;x&lt;/code&gt; variable for use inside the callback.&lt;/p&gt;

&lt;p&gt;This looks extremely similar to the C# 2.0 version from last time.&amp;#160; However. under the covers, it’s rather different.&lt;/p&gt;

&lt;p&gt;Like .Net managed instance methods, all Javascript functions take a hidden &lt;code&gt;this&lt;/code&gt; parameter.&amp;#160; However, unlike .Net, Javascript does not have delegates.&amp;#160; There is no (intrinsic) way to bind an object to the &lt;code&gt;this&lt;/code&gt; parameter the way a .Net closed delegate does.&amp;#160; Instead, the &lt;code&gt;this&lt;/code&gt; parameter comes from the callsite, depending on how the function was called.&amp;#160; Therefore, we cannot pass state in the &lt;code&gt;this&lt;/code&gt; parameter the way we did in C#.&lt;/p&gt;

&lt;p&gt;Instead, all Javascript function expressions capture the variable environment of the scope that they are declared in as a hidden property of the function.&amp;#160; Therefore, a function can reference local variables from its declaring scope.&amp;#160; Unlike C#, which binds functions to their parent scopes using a field in a separate delegate object that points to the function, Javascript functions have their parent scopes baked in to the functions themselves.&amp;#160; &lt;/p&gt;

&lt;p&gt;Javascript doesn’t have separate delegate objects that can hold a function and a &lt;code&gt;this&lt;/code&gt; parameter.&amp;#160; Instead, the value of the &lt;code&gt;this&lt;/code&gt; parameter is determined at the call-site, depending on how the function was called.&amp;#160; This is a common source of confusion to inexperienced Javascript developers.&lt;/p&gt;

&lt;p&gt;To simulate closed delegates, we can make a method that takes a function as well as a target object to call it on, and returns a new function which calls the original function with &lt;code&gt;this&lt;/code&gt; equal to the target parameter.&amp;#160; That sounds overwhelmingly complicated, but it’s actually not that hard:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;createDelegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Target!&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;createDelegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;delegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This &lt;code&gt;createDelegate&lt;/code&gt; method returns a function expression that captures the &lt;code&gt;func&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt; parameters, and calls &lt;code&gt;func&lt;/code&gt; in the context of &lt;code&gt;target&lt;/code&gt;.&amp;#160; Instead of storing the target in a property of a &lt;code&gt;Delegate&lt;/code&gt; object (like .Net does), this code stores it in the inner function expression’s closure.&lt;/p&gt;

&lt;p&gt;Javascript 1.8.5 provides the &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind&quot;&gt;&lt;code&gt;Function.bind&lt;/code&gt; method&lt;/a&gt;, which is equivalent to this &lt;code&gt;createDelegate&lt;/code&gt; method, with additional capabilities as well.&amp;#160; In Chrome, Firefox 4, and IE9, you can write&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Target!&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;delegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

For more information, see the &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind&quot;&gt;MDN documentation&lt;/a&gt;.  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-5387142343229685567</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/06/delegates-vs-function-pointers-part-4-c.html"/>
		<title type="text">Delegates vs. Function Pointers, part 4: C# 2.0+</title>
		<updated>2011-06-19T19:49:00+00:00</updated>
		<published>2011-08-16T01:09:05+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="delegates" term="delegates" />
		
		
		<category scheme="https://blog.slaks.net/#" label="closures" term="closures" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="lambda-expressions" term="lambda-expressions" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;i&gt;This is part 4 in a series about state and function pointers; part 1 is &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html&quot;&gt;here&lt;/a&gt;.&lt;/i&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-3-c.html&quot;&gt;Last time&lt;/a&gt;, we saw that it is possible to pass local state with a delegate in C#.&amp;#160; However, it involves lots of repetitive single-use classes, leading to ugly code.&lt;/p&gt;  &lt;p&gt;To alleviate this tedious task, C# 2 supports &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/0yw3tz5k%28v=VS.100%29.aspx&quot;&gt;anonymous methods&lt;/a&gt;, which allow you to embed a function inside another function.&amp;#160; This makes my &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html#example&quot;&gt;standard example&lt;/a&gt; much simpler:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//C# 2.0&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hugeNumbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FindAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;



&lt;span class=&quot;c1&quot;&gt;//C# 3.0&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hugeNumbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Clearly, this is much simpler than the C# 1.0 version from last time.&amp;#160; However, anonymous methods and lambda expressions are compile-time features; the CLR itself is not aware of them.&amp;#160; How does this code work? How can an anonymous method use a local variable from its parent scope?&lt;/p&gt;

&lt;p&gt;This is an example of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Closure_%28computer_science%29&quot;&gt;closure&lt;/a&gt; – a function bundled together with external variables that the function uses.&amp;#160; The C# compiler handles this the same way that I did manually last time in C# 1: it generates a class to hold the function and the variables that it uses, then creates a delegate from the member function in the class.&amp;#160; Thus, the local state is passed as the delegate’s &lt;code&gt;this&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;To see how the C# compiler implements closures, I’ll use &lt;a href=&quot;https://github.com/icsharpcode/ILSpy&quot;&gt;ILSpy&lt;/a&gt; to decompile the more-familiar C# 3 version: (I simplified the &lt;a href=&quot;https://stackoverflow.com/q/6402491/34397&quot;&gt;compiler-generated names&lt;/a&gt; for readability)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CompilerGenerated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ClosureClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ClosureClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closure&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ClosureClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;closure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hugeNumbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;closure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code&gt;ClosureClass&lt;/code&gt; (which was actually named &lt;code&gt;&amp;lt;&amp;gt;c__DisplayClass1&lt;/code&gt;) is equivalent to the &lt;code&gt;GreaterThan&lt;/code&gt; class from my previous example.&amp;#160; It holds the local variables used in the lambda expression.&amp;#160; Note that this class &lt;em&gt;replaces&lt;/em&gt; the variables – in the original method, instead a local variable named &lt;code&gt;x&lt;/code&gt;, the compiler uses the public &lt;code&gt;x&lt;/code&gt; field from the &lt;code&gt;ClosureClass&lt;/code&gt;.&amp;#160; This means that any changes to the variable affect the lambda expression as well.&lt;/p&gt;

&lt;p&gt;The lambda expression is compiled into the &lt;code&gt;Lambda&lt;/code&gt; method (which was originally named &lt;code&gt;&amp;lt;Main&amp;gt;b__0&lt;/code&gt;).&amp;#160; It uses the same field to access the local variable, sharing state between the original outer function and its lambda expression.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/07/delegates-vs-function-pointers-part-5.html&quot;&gt;&lt;em&gt;Next time: &lt;/em&gt;Javascript&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4338539915387757905</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/06/open-delegates-vs-closed-delegates.html"/>
		<title type="text">Open Delegates vs. Closed Delegates</title>
		<updated>2011-06-14T14:49:00+00:00</updated>
		<published>2011-08-31T01:36:14+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="delegates" term="delegates" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;.Net supports two kinds of delegates: Open delegates and closed delegates. &lt;/p&gt;  &lt;p&gt;When you create a delegate that points to an instance method, the instance that you created it from is stored in the delegate’s &lt;code&gt;Target&lt;/code&gt; property.&amp;#160; This property is passed as the first parameter to the method that the delegate points to.&amp;#160; For instance methods, this is the implicit this parameter; for static methods, it's the method's first parameter.&amp;#160; These are called &lt;i&gt;closed &lt;/i&gt;delegates, because they &lt;a href=&quot;https://en.wikipedia.org/wiki/Closure_%28computer_science%29&quot;&gt;close over&lt;/a&gt; the first parameter and bring it into the delegate instance. &lt;/p&gt;  &lt;p&gt;It is also possible to create &lt;i&gt;open&lt;/i&gt; delegates which do not pass a first parameter.&amp;#160; Open delegates do not use the &lt;code&gt;Target&lt;/code&gt; property; instead, all of the target method’s parameters are passed from the delegate’s formal parameter list, including the first parameter.&amp;#160; Therefore, an open delegate pointing to a given method must have one parameter more than a closed delegate pointing to the same method.&amp;#160; Open delegates are usually used to point to static methods.&amp;#160; When you make a delegate pointing to a static method, you (generally) don't want the delegate to hold a first parameter for the method.&amp;#160; &lt;/p&gt;  &lt;p&gt;In addition to these two normal cases, it is also possible (in .Net 2.0 and later) to create open delegates for instance methods and to create closed delegates for static methods.&amp;#160; With one exception, C# doesn’t have any syntactical support for these unusual delegates, so they can only be created by calling the &lt;code&gt;CreateDelegate&lt;/code&gt; method. &lt;/p&gt;  &lt;p&gt;Open delegates by calling the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/53cz7sc6.aspx&quot;&gt;&lt;code&gt;CreateDelegate&lt;/code&gt; overload&lt;/a&gt; that doesn’t take a &lt;code&gt;target&lt;/code&gt; parameter.&amp;#160; Before .Net 2.0, this function could only be called with a static method.&amp;#160;&amp;#160; In .Net 2.0, you can call this function with an instance method to create an open delegate.&amp;#160; Such a delegate will call use its first parameter as &lt;code&gt;this&lt;/code&gt; instead of its &lt;code&gt;Target&lt;/code&gt; field.&amp;#160;&lt;/p&gt;  &lt;p&gt;As a concrete example, consider the &lt;code&gt;String.ToUpperInvariant()&lt;/code&gt; method.&amp;#160; Ordinarily, this method takes no parameters, and operates on the string it’s called on.&amp;#160; An open delegate pointing to this instance method would take a single string parameter, and call the method on that parameter.&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closed&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToUpperInvariant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;open&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Delegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CreateDelegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;),&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ToUpperInvariant&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;closed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;//Returns &quot;A&quot;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;abc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//Returns &quot;ABC&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Closed delegates are created by calling the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/74x8f551.aspx&quot;&gt;&lt;code&gt;CreateDelegate&lt;/code&gt; overload&lt;/a&gt; that takes a &lt;code&gt;target&lt;/code&gt; parameter.&amp;#160; In .Net 2.0, this can be called with a static method and an instance of that method’s first argument type to create a closed delegate that calls the method with the given &lt;code&gt;target&lt;/code&gt; as its first parameter.&amp;#160; Closed delegates &lt;a href=&quot;https://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx&quot;&gt;curry&lt;/a&gt; the first parameter from the target method.&amp;#160; For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deepThought&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Delegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CreateDelegate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;),&lt;/span&gt;
        &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Equals&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Static&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Public&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code curries the static Object.Equals method to create a delegate that calls &lt;code&gt;Equals&lt;/code&gt; with 2 and the delegate’s single parameter).&amp;#160; It’s equivalent to &lt;code&gt;x =&amp;gt; Object.Equals(2, x)&lt;/code&gt;.&amp;#160; Note that since the method is (generally) not a member of the target object’s type, we need to pass an actual &lt;code&gt;MethodInfo&lt;/code&gt; instance; a name alone isn’t good enough.&lt;/p&gt;

&lt;p&gt;Note that you cannot create a closed delegate from a static method whose first parameter is a value type, because, unlike all instance methods, static delegates that take value types receive their parameters by value, not as a reference.&amp;#160;&amp;#160; For more details, see &lt;a href=&quot;https://stackoverflow.com/q/1016033/34397&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;https://blogs.msdn.com/sreekarc/archive/2009/06/25/why-can-t-extension-methods-on-value-type-be-curried.aspx&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;C# 3 added limited syntactical support for creating closed delegates.&amp;#160; You can create a delegate from an extension method as if it were an instance method on the type it extends.&amp;#160; For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allNumbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MaxValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countTo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allNumbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Take&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code creates an &lt;code&gt;IEnumerable&amp;lt;int&amp;gt;&lt;/code&gt; containing all positive integers, then creates a closed delegate that curries this sequence into the static &lt;code&gt;Enumerable.Take&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt;)&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;Except for extension methods, open instance delegates and closed static delegates are rarely used in actual code.&amp;#160; However, it is important to understand how ordinary open and closed delegates work, and where the target object ends up for instance delegates.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-6357646512767009617</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/06/delegates-vs-function-pointers-part-3-c.html"/>
		<title type="text">Delegates vs. Function Pointers, part 3: C# 1.0</title>
		<updated>2011-06-13T21:29:00+00:00</updated>
		<published>2011-06-19T19:49:57+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="delegates" term="delegates" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="functions" term="functions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;i&gt;This is part 3 in a series about state and function pointers; part 1 is &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html&quot;&gt;here&lt;/a&gt;.&lt;/i&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-2-c.html&quot;&gt;Last time&lt;/a&gt;, we saw that it is impossible to bundle context along with a function pointer in C.&lt;/p&gt;  &lt;p&gt;In C#, it is possible to fully achieve my &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html#example&quot;&gt;standard example&lt;/a&gt;.&amp;#160; In order to explain how this works behind the scenes, I will limit this post to C# 1.0 and not use a lambda expression.&amp;#160; This also means no LINQ, generics, or extension methods, so I will, once again, need to write the filter method myself. &lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;IntFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IntFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retVal&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;retVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retVal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GreaterThan&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GreaterThan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bound&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Passes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;Filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;IntFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GreaterThan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Passes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Please excuse the ugly code; in order to be true to C# 1.0, I can’t just write an iterator, and I can’t implicitly create the delegate.&lt;/p&gt;

&lt;p&gt;This code creates a class called &lt;code&gt;GreaterThan&lt;/code&gt; that holds the &lt;code&gt;Passes&lt;/code&gt; method and the state used by the method.&amp;#160; I create a delegate to pass to &lt;code&gt;Filter&lt;/code&gt; out of the &lt;code&gt;Passes&lt;/code&gt; method for an instance of the &lt;code&gt;GreaterThan&lt;/code&gt; class from the local variable.&lt;/p&gt;

&lt;p&gt;To understand how this works, we need to delve further into delegates.&amp;#160; .Net delegates are more than just type-safe function pointers. Unlike the function pointers we've &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-2-c.html&quot;&gt;looked at&lt;/a&gt;, delegates include state – the &lt;code&gt;Target&lt;/code&gt; property. This property stores the object to pass as the hidden &lt;code&gt;this&lt;/code&gt; parameter to the method (It's actually somewhat more complicated than that; I will describe open and closed delegates at a later date).&amp;#160; My example creates a delegate instance in which the &lt;code&gt;Target&lt;/code&gt; property points to the &lt;code&gt;GreaterThan&lt;/code&gt; instance.&amp;#160; When I call the delegate, the &lt;code&gt;Passes&lt;/code&gt; method is called on the correct instance from the &lt;code&gt;Target&lt;/code&gt; property, so it can read the &lt;code&gt;bound&lt;/code&gt; field.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-4-c.html&quot;&gt;Next time&lt;/a&gt;, we'll see how the C# 2.0+ compilers generate all this boilerplate for you using anonymous methods and lambda expressions. 

  &lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-6446695008622612171</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/06/delegates-vs-function-pointers-part-2-c.html"/>
		<title type="text">Delegates vs. Function Pointers, part 2: C</title>
		<updated>2011-06-01T21:08:00+00:00</updated>
		<published>2011-06-14T02:37:22+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C" term="c" />
		
		
		<category scheme="https://blog.slaks.net/#" label="closures" term="closures" />
		
		
		<category scheme="https://blog.slaks.net/#" label="functions" term="functions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;em&gt;This is part 2 in a series about state and function pointers; part 1 is &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html&quot;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Unlike most other languages, it is not possible to include any form of state in a function pointer in C.&amp;#160; Therefore, it is impossible to fully implement closures in C without the cooperation of the call-site and/or the compiler.&lt;/p&gt;  &lt;p&gt;To illustrate what this means in practice, I will refer to my &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-1.html#example&quot;&gt;standard example&lt;/a&gt;, using a filter function to find all elements in an array that are greater than &lt;code&gt;x&lt;/code&gt;.&amp;#160; Since C doesn’t have any such function, I’ll write one myself, inspired by &lt;code&gt;qsort&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
             &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
             &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elemSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
             &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//Magic...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With this function, one can easily find all elements that are greater than some constant:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;However, if we want to replace the hard-coded &lt;code&gt;2&lt;/code&gt; with a local variable, it becomes more complicated.&amp;#160; Since function pointers, unlike delegates, do not have state, there is no simple way to pass the local variable to the function.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://stackoverflow.com/questions/4210689/pass-extra-parameter-to-comparator-for-qsort/4210705#4210705&quot;&gt;most obvious answer&lt;/a&gt; is to use a global variable:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;minimum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;minimum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;minimum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;However, this code will fail horribly if it’s ever run on multiple threads.&amp;#160; In simple cases, you can work around that by storing the value in a global &lt;a href=&quot;https://en.wikipedia.org/wiki/Thread-local_storage&quot;&gt;thread-local variable&lt;/a&gt; (or in &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms682661.aspx&quot;&gt;fiber-local storage&lt;/a&gt; for code that uses fibers).&amp;#160;&amp;#160; Because each thread will have its own global, the threads won’t conflict with each-other.&amp;#160; Obviously, this won’t work if the callback might be run on a different thread.&lt;/p&gt;

&lt;p&gt;However, even if everything is running on one thread, this still isn’t enough if the code can be re-entrant, or if the callback might be called after the original function call finishes.&amp;#160; This technique assumes that exactly one callback (from a single call to the function that accepted the callback) will be used at any given time.&amp;#160; If the callback might be invoked later (eg, a timer, or a UI handler), this method will break down, since all of the callbacks will share the same global.&lt;/p&gt;

&lt;p&gt;In these more complicated cases, one can only pass any state with the cooperation of the original function.&amp;#160; In this (overly simple) example, the &lt;code&gt;filter&lt;/code&gt; method can be modified to take a &lt;code&gt;context&lt;/code&gt; parameter and pass it to the predicate:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
             &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elemSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
             &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; 
             &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//More magic...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To pass around more complicated state, one could pass a pointer to a &lt;code&gt;struct&lt;/code&gt; as the &lt;code&gt;context&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-3-c.html&quot;&gt;&lt;em&gt;Next Time: &lt;/em&gt;C# 1.0&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8184360551385403573</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/06/delegates-vs-function-pointers-part-1.html"/>
		<title type="text">Delegates vs. Function Pointers, part 1</title>
		<updated>2011-06-01T20:21:00+00:00</updated>
		<published>2011-08-16T01:10:34+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C++" term="cpp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C" term="c" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="closures" term="closures" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="functions" term="functions" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Most languages – with the unfortunate exception of Java – allow functions to be passed around as variables.&amp;#160; C has function pointers, .Net has delegates, and Javascript and most functional programming languages treat functions as first class objects.&lt;/p&gt;  &lt;p&gt;There is a fundamental difference between C-style function pointers vs. delegates or function objects.&amp;#160; Pure function pointers cannot hold any state other than the function itself.&amp;#160; In contrast, delegates and function objects do store additional state that the function can use.&lt;/p&gt;  &lt;p id=&quot;example&quot;&gt;To illustrate this difference,&amp;#160; I will use a simple example.&amp;#160; Most programming environments have a filter function that takes a collection and a predicate (a function that takes an item from the collection and returns a boolean), and returns a new collection with those items from the original collection that match the predicate.&amp;#160; In C#, this is the LINQ &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb534803.aspx&quot;&gt;Where method&lt;/a&gt;; in Javascript, it’s the &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Filter&quot;&gt;Array.filter method&lt;/a&gt; (introduced by Javascript 1.6).&lt;/p&gt;  &lt;p&gt;Given a list of numbers, you can use such a method to find all numbers in the list that are greater than a local variable &lt;code&gt;x&lt;/code&gt;.&amp;#160; However, in order to do that, the predicate have access to &lt;code&gt;x&lt;/code&gt;.&amp;#160; In &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-2-c.html&quot;&gt;subsequent&lt;/a&gt; &lt;a href=&quot;/2011/06/delegates-vs-function-pointers-part-3-c.html&quot;&gt;posts&lt;/a&gt;, I’ll explore how this can be done in &lt;a href=&quot;/2011/07/delegates-vs-function-pointers-part-5.html&quot;&gt;different&lt;/a&gt; languages.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1885925431799275952</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/05/htmlforeach-in-razor.html"/>
		<title type="text">Html.ForEach in Razor</title>
		<updated>2011-05-11T02:56:00+00:00</updated>
		<published>2011-05-11T02:56:12+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="inline-helpers" term="inline-helpers" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Many people write &lt;code&gt;ForEach&lt;/code&gt; extension methods for MVC WebForms views, which take a sequence and a delegate to turn each item in the sequence into HTML.&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ForEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HtmlHelper&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                              &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                              &lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htmlWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;htmlWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;(The unused &lt;code&gt;html&lt;/code&gt; parameter allows it to be called as an extension method like other HTML helpers)&lt;/p&gt;

&lt;p&gt;This code can be called like this:&lt;/p&gt;


&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-aspx-cs&quot; data-lang=&quot;aspx-cs&quot;&gt;&amp;lt;ul&amp;gt;
    &amp;lt;% Html.ForEach(
            Enumerable.Range(1, 10),
            item =&amp;gt; { %&amp;gt; &amp;lt;li&amp;gt;&amp;lt;%= item %&amp;gt;&amp;lt;/li&amp;gt; &amp;lt;% }
    ); %&amp;gt;
&amp;lt;/ul&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;This code creates a lambda expression that writes markup to the page’s response stream by ending the code block inside the lambda body. Neither the lambda expression nor the &lt;code&gt;ForEach&lt;/code&gt; helper itself return anything; they both write directly to the response.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;List&amp;lt;T&amp;gt;.ForEach&lt;/code&gt; method can be called exactly the same way.&lt;/p&gt;

&lt;p&gt;In Razor views, this method cannot easily be called directly, since Razor pages cannot put markup inside of expressions.&amp;#160; You can use a &lt;a href=&quot;/2011/05/creating-markup-actions-in-razor.html&quot;&gt;workaround&lt;/a&gt; by creating an inline helper and calling it immediately, but it would be better to rewrite the &lt;code&gt;ForEach&lt;/code&gt; method to take an &lt;a href=&quot;/2011/04/dissecting-razor-part-9-inline-helpers.html&quot;&gt;inline helper&lt;/a&gt; directly.&lt;/p&gt;

&lt;p&gt;The naïve way to do that is like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IHtmlString&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ForEachSimple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HtmlHelper&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htmlCreator&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HtmlString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;htmlCreator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code&gt;htmlCreator&lt;/code&gt; delegate, which can be passed as an inline helper, returns a &lt;code&gt;HelperResult&lt;/code&gt; object containing the markup generated for an item.&lt;/p&gt;

&lt;p&gt;This code uses LINQ to call &lt;code&gt;htmlCreator&lt;/code&gt; on each item in the set (the &lt;code&gt;Select&lt;/code&gt; call), then calls &lt;code&gt;String.Concat&lt;/code&gt; to combine them all into one giant string.&amp;#160; (&lt;code&gt;String.Concat&lt;/code&gt; will call &lt;code&gt;ToString&lt;/code&gt; on each &lt;code&gt;HelperResult&lt;/code&gt;, which will return the generated markup)&amp;#160; We could also call &lt;code&gt;String.Join&lt;/code&gt; to put a separator, such as a newline, between every two items.&lt;/p&gt;

&lt;p&gt;Finally, it returns an &lt;code&gt;HtmlString&lt;/code&gt; to prevent Razor from escaping the returned HTML.&lt;/p&gt;

&lt;p&gt;It’s equivalent to the following code using a &lt;code&gt;StringBuilder&lt;/code&gt; (this is what &lt;code&gt;String.Concat&lt;/code&gt; does internally)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;htmlCreator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HtmlString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This method can be called like this:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;@Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ForEachSimple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;The problem with this approach is that it combines all of the content into a giant string.&amp;#160; If there are a large number of items, or if each item will have a large amount of markup, this can become (a little bit) slow.&amp;#160; It would be better to write each item directly to the caller’s response stream, without assembling any giant strings.&amp;#160; This is where &lt;code&gt;HelperResult&lt;/code&gt; comes in.&lt;/p&gt;

&lt;p&gt;The HelperResult class allows its caller to pass a &lt;code&gt;TextWriter&lt;/code&gt; to the &lt;code&gt;WriteTo&lt;/code&gt; method, and the helper delegate will write directly to this &lt;code&gt;TextWriter&lt;/code&gt;.&amp;#160; I can take advantage of this to write a &lt;code&gt;ForEach&lt;/code&gt; extension that doesn’t build any strings, by returning a &lt;code&gt;HelperResult&lt;/code&gt; instead of a regular &lt;code&gt;IHtmlString&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ForEachFast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HtmlHelper&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htmlCreator&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tw&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;htmlCreator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

This version creates a &lt;code&gt;HelperResult&lt;/code&gt; with a delegate that writes each of its items in turn to the &lt;code&gt;TextWriter&lt;/code&gt;.


  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8518427648048559497</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/05/creating-markup-actions-in-razor.html"/>
		<title type="text">Creating Markup Actions in Razor</title>
		<updated>2011-05-02T18:06:00+00:00</updated>
		<published>2011-05-02T18:06:18+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="inline-helpers" term="inline-helpers" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Razor’s &lt;a href=&quot;/2011/04/dissecting-razor-part-9-inline-helpers.html&quot;&gt;inline helpers&lt;/a&gt; allow you to create lambda expression that return markup (as a &lt;code&gt;HelperResult&lt;/code&gt;).&amp;#160; However, there is no simple way to create a lambda expression that writes HTML directly to the page (instead of returning it).&lt;/p&gt;  &lt;p&gt;In ASPX pages, one can simply put the beginning and end of a lambda expression in expression blocks, then put markup in the middle.&lt;/p&gt;  &lt;p&gt;For example, this code creates a delegate that writes a &lt;code&gt;&amp;lt;b&amp;gt;&lt;/code&gt; tag to the page:&lt;/p&gt;  

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-aspx-cs&quot; data-lang=&quot;aspx-cs&quot;&gt;&amp;lt;%
    Action pageWriter = () =&amp;gt; {%&amp;gt;&amp;lt;b&amp;gt;I'm from a lambda!&amp;lt;/b&amp;gt;&amp;lt;%};
    pageWriter();
    pageWriter();
    pageWriter();
%&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Calling the &lt;code&gt;pageWriter&lt;/code&gt; delegate will write directly to the HTTP response stream.&lt;/p&gt;

&lt;p&gt;By contrast, Razor inline expressions return their markup.&amp;#160; To do this in a Razor page, one would write&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htmlMaker&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;!&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;@htmlMaker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//Note @ sign&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;@htmlMaker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//Note @ sign&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;@htmlMaker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//Note @ sign&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;Calling &lt;code&gt;htmlMaker&lt;/code&gt; without an @ sign will return HTML, but won’t write anything to the page.&lt;/p&gt;

&lt;p&gt;When working with libraries designed for ASPX pages, it can be necessary to create ASPX-style inline helpers that write to the page instead of returning a value.&amp;#160; You can do that by creating an inline helper lambda and passing it to the &lt;code&gt;Write method&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pageWriter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
         &lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;!&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;Like the ASPX version, &lt;code&gt;pageWriter&lt;/code&gt; now writes directly to the page and does not return anything.&lt;/p&gt;

&lt;p&gt;This code can be made simpler by wrapping it in a separate method:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MakeAction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inlineHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inlineHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This method takes an inline helper and returns an &lt;code&gt;Action&lt;/code&gt; that writes the helper’s output to the page.&amp;#160; Since it needs to call the page’s &lt;code&gt;Write&lt;/code&gt; method (to use the page’s output stream), this method must be defined in the &lt;code&gt;WebPageBase&lt;/code&gt; instance, either in an &lt;code&gt;@functions&lt;/code&gt; block or in a common base class.&lt;/p&gt;

&lt;p&gt;Any code in the inline helper will execute each time the resulting &lt;code&gt;Action&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;It can be called like this:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pageWriter2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MakeAction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;!&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;);&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;This code is equivalent to the previous sample, but it’s much simpler.&lt;/p&gt;

&lt;p&gt;One can also write write a version that takes a parameter:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MakeAction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inlineHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inlineHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

Note that the type parameter must be specified explicitly; C# does not infer type parameters from the method’s return type.  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-741715917846448517</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/04/dissecting-razor-part-9-inline-helpers.html"/>
		<title type="text">Dissecting Razor, part 9: Inline Helpers</title>
		<updated>2011-04-29T14:08:00+00:00</updated>
		<published>2011-04-29T19:08:58+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="inline-helpers" term="inline-helpers" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;In addition to &lt;a href=&quot;/2011/02/dissecting-razor-part-7-helpers.html&quot;&gt;normal&lt;/a&gt; and &lt;a href=&quot;/2011/03/dissecting-razor-part-8-static-helpers.html&quot;&gt;static&lt;/a&gt; helpers, Razor supports inline helpers, (also known as templates), which allow you to create lambda expressions that return markup.&lt;/p&gt;  &lt;p&gt;An inline helper is created by writing &lt;code&gt;@&amp;lt;tag&amp;gt;Content&amp;lt;/tag&amp;gt;&lt;/code&gt; as an expression.&amp;#160; This creates a lambda expression that takes a parameter called &lt;code&gt;item&lt;/code&gt; and returns a &lt;a href=&quot;/2011/02/dissecting-razor-part-7-helpers.html#HelperResult&quot;&gt;&lt;code&gt;HelperResult&lt;/code&gt; object&lt;/a&gt; containing the markup.&lt;/p&gt;  &lt;p&gt;Inline helpers are used to create functions that take markup as parameters.&amp;#160; For example, you might make an &lt;code&gt;IfLoggedOn&lt;/code&gt; helper that displays content if there is a logged-in user, but shows a login link to anonymous users.&amp;#160; To pass the content to the helper, you can use an inline helper:&lt;/p&gt;  
&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;@helper&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;IfLoggedOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MembershipUser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentUser&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Membership&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetUser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentUser&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;you&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;must&lt;/span&gt; 
            &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@Href(&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;~/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Login&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;@content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentUser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;You can call this method with an inline helper like this:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;nf&quot;&gt;@IfLoggedOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@Href(&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Comment&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;)&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;post&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;@Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;TextArea&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Comment&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Type a comment&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;submit&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Submit&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Add Comment&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;The &lt;code&gt;@content(currentUser)&lt;/code&gt; call in helper method is translated by the Razor compiler into a call to the overload of the &lt;code&gt;Write&lt;/code&gt; method that takes a &lt;code&gt;HelperResult&lt;/code&gt; (returned from the delegate).&amp;#160; This overload writes the content of the &lt;code&gt;HelperResult&lt;/code&gt; to the page without escaping it (Just like a &lt;a href=&quot;/2011/02/dissecting-razor-part-7-helpers.html#HelperResult&quot;&gt;call to a normal helper method&lt;/a&gt;).&amp;#160; Functions that take inline helpers can also get the text rendered by the helper by calling &lt;code&gt;ToString()&lt;/code&gt; on the &lt;code&gt;HelperResult&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The call to the helper method is compiled to the following C# code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IfLoggedOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__razor_template_writer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_template_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;    &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_template_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;form action=\&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_template_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Href&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Add-Comment&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_template_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;\&quot; method=\&quot;post\&quot;&amp;gt;\r\n        &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_template_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;TextArea&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Comment&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Type a comment&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_template_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;\r\n   ...  &amp;lt;/form&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;})));&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code&gt;IsLoggedOn&lt;/code&gt; method is passed a lambda expression that takes an &lt;code&gt;item&lt;/code&gt; parameter and returns a new &lt;code&gt;HelperResult&lt;/code&gt;.&amp;#160; The &lt;code&gt;HelperResult&lt;/code&gt; is constructed exactly like a &lt;a href=&quot;/2011/02/dissecting-razor-part-7-helpers.html&quot;&gt;normal helper&lt;/a&gt;, except that it’s in a lambda expression instead of a normal method.&lt;/p&gt;

&lt;p&gt;Like normal helpers, then markup inside inline helpers can contain arbitrary code blocks and code nuggets.&amp;#160; However, inline helpers cannot be nested (since that would create conflicting parameter names)&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;item&lt;/code&gt; parameter is implicitly typed based on the delegate type that the helper is being passed as (just like normal lambda expressions).&amp;#160; This allows inline helpers to accept a parameter:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;nf&quot;&gt;@IfLoggedOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Welcome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;@item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;!&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;(The special &lt;code&gt;&amp;lt;text&amp;gt;&lt;/code&gt; tag is used to pass markup without creating an actual HTML tag)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next Time&lt;/em&gt;: Sections&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2480252504549257469</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/03/dissecting-razor-part-8-static-helpers.html"/>
		<title type="text">Dissecting Razor, part 8: Static Helpers</title>
		<updated>2011-03-08T16:41:00+00:00</updated>
		<published>2011-04-29T14:09:11+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Razor-helpers" term="razor-helpers" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-7-helpers.html&quot;&gt;Razor helpers&lt;/a&gt; can be extremely useful, but they are designed to be used only by the page that created them.&lt;/p&gt;  &lt;p&gt;To create reusable helpers, you can create CSHTML pages in the App_Code directory.&amp;#160; The WebRazorHostFactory will check for pages in App_Code and create a WebCodeRazorHost instead of the normal WebPageRazorHost.&lt;/p&gt;  &lt;p&gt;This happens before the virtual CreateHost method; in order to change this behavior, one must create an inherited RazorBuildProvider and override the CreateHost method; for more details, see the source for more details.&lt;/p&gt;  &lt;p&gt;The WebCodeRazorHost compiles helper methods as public static methods by setting the RazorEngineHost.StaticHelpers property to true.&amp;#160; It also overrides PostProcessGeneratedCode to remove the Execute() method and make the &lt;code&gt;Application&lt;/code&gt; property static.&amp;#160; &lt;/p&gt;  &lt;p&gt;Static helper pages inherit the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.webpages.helperpage.aspx&quot;&gt;HelperPage class&lt;/a&gt;, a very stripped-down base class which contains the static WriteTo methods used by the helpers and some static members that expose the Model, Html, and other members from the currently executing page (using the &lt;code&gt;WebPageContext.Current.Page&lt;/code&gt; property). &lt;/p&gt;  &lt;p&gt;Because the generated Execute() method is removed, all content outside helpers and &lt;code&gt;@functions&lt;/code&gt;&amp;#160; blocks is not seen by the compiler.&amp;#160; It is seen by the Razor parser, so it must contain valid Razor syntax (eg, no stray @ characters).&lt;/p&gt;  &lt;p&gt;Here is an example:&lt;/p&gt;  

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;@helper&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Menu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;params&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[][]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;@foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@Href(pair[1])&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;This&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;@code&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;This helper can then be called from any other code by writing &lt;code&gt;&lt;em&gt;PageName&lt;/em&gt;.Menu(...)&lt;/code&gt;, where &lt;code&gt;&lt;em&gt;PageName&lt;/em&gt;&lt;/code&gt; is the filename of the CHSTML page in App_Code.&amp;#160; In Razor pages, it can be called like any other helper.&amp;#160; In normal C# code, it returns a &lt;code&gt;HelperResult&lt;/code&gt; instance; call &lt;code&gt;ToString()&lt;/code&gt; or &lt;code&gt;WriteTo(TextWriter)&lt;/code&gt; to get the HTML source.&lt;/p&gt;

&lt;p&gt;For example:&amp;#160; (assuming that the helper is defined in ~/App_Code/Helpers.cshtml)&lt;/p&gt;
&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;@Helpers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Menu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Home&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;s&quot;&gt;&quot;~/&quot;&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;About&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;s&quot;&gt;&quot;~/About&quot;&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Contact&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;~/Contact&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In order to see the source, I need to insert a &lt;code&gt;#error&lt;/code&gt; directive inside the helper block; putting it in the page itself will have no effect, since the page is not compiled.&amp;#160; Since the contents of the helper block are code, not markup, I don’t need to wrap it in &lt;code&gt;@{ ... }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The above helper is transformed into the following C#: (As usual, &lt;code&gt;@line&lt;/code&gt; directives have been removed)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Helpers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HelperPage&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Menu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;params&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[][]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__razor_helper_writer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

            &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;    &amp;lt;ul&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;            &amp;lt;li&amp;gt;&amp;lt;a href=\&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Href&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]));&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;\&quot;&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;    &amp;lt;/ul&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#error
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Helpers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpApplication&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that the page-level content does not show up anywhere.&amp;#160; The helper itself is compiled exactly like a normal helper, except that it’s &lt;code&gt;static&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although there aren’t any in this example, &lt;code&gt;@functions&lt;/code&gt; blocks will also be emitted normally into the generated source.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/04/dissecting-razor-part-9-inline-helpers.html&quot;&gt;&lt;em&gt;Next Time&lt;/em&gt;: Inline Helpers&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-7914512834123709816</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/03/boot-camp-just-got-better.html"/>
		<title type="text">Boot Camp just got better</title>
		<updated>2011-03-06T16:33:00+00:00</updated>
		<published>2011-03-06T16:33:36+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="MacBook-Pro" term="macbook-pro" />
		
		
		<category scheme="https://blog.slaks.net/#" label="boot-camp" term="boot-camp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Apple" term="apple" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Windows-7" term="windows-7" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;The Boot Camp drivers in the latest generation of MacBook Pros now expose more of the laptop’s hardware to Windows.&lt;/p&gt;  &lt;p&gt;This means that Windows can now adjust the screen brightness in the Windows Mobility Center, and when you plug in or unplug the laptop.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://lh3.ggpht.com/_iWJc6lVY4ho/TXO3XlfN3EI/AAAAAAAAHLs/VhGuDNvVGYg/s1600-h/image%5B24%5D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://lh5.ggpht.com/_iWJc6lVY4ho/TXO3X7SOA3I/AAAAAAAAHLw/TEeHJSrZNLs/image_thumb%5B28%5D.png?imgmax=800&quot; width=&quot;564&quot; height=&quot;603&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The new drivers also expose the laptop’s ambient light sensor via Windows 7’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/windows/hardware/gg463473&quot;&gt;sensor platform&lt;/a&gt;, allowing the screen brightness to be adjusted automatically, and allowing 3rd-party programs to read the brightness.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-3265989975997743760</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/02/dissecting-razor-part-7-helpers.html"/>
		<title type="text">Dissecting Razor, part 7: Helpers</title>
		<updated>2011-02-28T16:08:00+00:00</updated>
		<published>2011-04-29T13:51:31+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Razor-helpers" term="razor-helpers" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;We’ll continue our &lt;a href=&quot;/2011/02/dissecting-razor-part-6-function-blocks.html&quot;&gt;trek&lt;/a&gt; into Razor’s class-level features with helpers.&lt;/p&gt;  &lt;p&gt;Helpers are one of Razor’s unique features.&amp;#160; They encapsulate blocks of HTML and server-side logic into reusable page-level methods.&amp;#160; &lt;/p&gt;  &lt;p&gt;You can define a helper by writing &lt;code&gt;@helper MethodName(parameters) { ... }&lt;/code&gt;.&amp;#160; Inside the code block, you can put any markup or server-side code.&amp;#160; The contents of a helper are parsed as a code block (like the contents of a loop or &lt;code&gt;if&lt;/code&gt; block), so any non-HTML-like markup must be surrounded by &lt;code&gt;&amp;lt;text&amp;gt;&lt;/code&gt; tags or prefixed by the &lt;code&gt;@:&lt;/code&gt; escape.&lt;/p&gt;  &lt;p&gt;Here is a simple example:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        @helper NumberRow(int num) {
            var square = num * num;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;td&amp;gt;&lt;/span&gt;@num&lt;span class=&quot;nt&quot;&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;td&amp;gt;&lt;/span&gt;@square&lt;span class=&quot;nt&quot;&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;td&amp;gt;&lt;/span&gt;@(num % 2 == 0 ? &quot;Even&quot; : &quot;Odd&quot;)&lt;span class=&quot;nt&quot;&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
        }
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;table&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;thead&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Number&lt;span class=&quot;nt&quot;&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Square&lt;span class=&quot;nt&quot;&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Eveness&lt;span class=&quot;nt&quot;&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/thead&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
                @for (int i = 0; i &lt;span class=&quot;nt&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;10;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;NumberRow&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;err&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tbody&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that code statements (such as the &lt;code&gt;square&lt;/code&gt; declaration) can go directly inside the helper body without being wrapped in code blocks – the direct contents of the helper is a code block, not markup.&amp;#160; Like any other code block, HTML-like markup is automatically treated as markup instead of code.&lt;/p&gt;

&lt;p&gt;Like &lt;code&gt;@functions&lt;/code&gt; blocks, helper methods can go anywhere in the source file; the physical location of the block is ignored.&lt;/p&gt;

&lt;p&gt;Here is the generated source for the above example: (with blank lines and &lt;code&gt;#line&lt;/code&gt; directives stripped for clarity)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_Page_Razor_Helpers_cshtml&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPage&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HelperResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NumberRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;HelperResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__razor_helper_writer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;            &amp;lt;tr&amp;gt;\r\n                &amp;lt;td&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;/td&amp;gt;\r\n                &amp;lt;td&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;/td&amp;gt;\r\n                &amp;lt;td&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;WriteTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Even&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Odd&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;nf&quot;&gt;WriteLiteralTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;/td&amp;gt;\r\n            &amp;lt;/tr&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_Page_Razor_Helpers_cshtml&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_asax&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ASP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_asax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;!DOCTYPE html&amp;gt;\r\n&amp;lt;html&amp;gt;\r\n    &amp;lt;body&amp;gt;\r\n        &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;\r\n        &amp;lt;table&amp;gt;\r\n            &amp;lt;thead&amp;gt;\r\n                &amp;lt;tr&amp;gt;\r\n                   &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot; &amp;lt;th&amp;gt;Number&amp;lt;/th&amp;gt;\r\n                    &amp;lt;th&amp;gt;Square&amp;lt;/th&amp;gt;\r\n                    &amp;lt;th&amp;gt;E&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;veness&amp;lt;/th&amp;gt;\r\n                &amp;lt;/tr&amp;gt;\r\n            &amp;lt;/thead&amp;gt;\r\n            &amp;lt;tbody&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;NumberRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;            &amp;lt;/tbody&amp;gt;\r\n        &amp;lt;/table&amp;gt;\r\n    &amp;lt;/body&amp;gt;\r\n&amp;lt;/html&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#error
&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Helpers are compiled as class-level methods that take a parameter set and return a System.Web.WebPages.HelperResult.&amp;#160; (This class name is configured by the RazorHostFactory) &lt;/p&gt;

&lt;p&gt;Notice the the contents of the helper method are inside a lambda expression that takes a parameter named &lt;code&gt;__razor_helper_writer&lt;/code&gt;.&amp;#160; This construction allows the helper to write directly to the HTTP response stream instead of assembling a giant string and then writing the string all at once.&lt;/p&gt;

&lt;p id=&quot;HelperResult&quot;&gt;The HelperResult constructor takes an &lt;code&gt;Action&amp;lt;TextWriter&amp;gt;&lt;/code&gt; which contains the contents of the helper block.&amp;#160; The class implements &lt;code&gt;IHtmlString&lt;/code&gt; and calls the action from the constructor to generate HTML.&amp;#160; However, under normal circumstances, this &lt;code&gt;IHtmlString&lt;/code&gt; implementation is never called.&lt;/p&gt;

&lt;p&gt;Calls to helper methods (&lt;code&gt;@NumberRow(i)&lt;/code&gt;) are passed to the &lt;code&gt;Write(HelperResult)&lt;/code&gt; overload.&amp;#160; This overload calls &lt;code&gt;HelperResult.WriteTo(writer)&lt;/code&gt;, which passes the page’s &lt;code&gt;TextWriter&lt;/code&gt; directly to the helper’s lambda expression.&amp;#160; Thus, the lambda expression can write directly to the page’s output stream, without passing the output as a parameter to the helper method.&lt;/p&gt;

&lt;p&gt;Looking inside the helper, we see that all content is passed to &lt;code&gt;WriteTo&lt;/code&gt; and &lt;code&gt;WriteLiteralTo&lt;/code&gt; methods, as opposed to the &lt;code&gt;Write&lt;/code&gt; and &lt;code&gt;WriteLiteral&lt;/code&gt; methods used by the rest of the page.&lt;/p&gt;

&lt;p&gt;Helper methods cannot call the normal &lt;code&gt;Write*&lt;/code&gt; methods since they aren’t necessarily writing to the current output (even though they usually do).&amp;#160; Therefore, they call these &lt;code&gt;Write*To&lt;/code&gt; methods, which accept the &lt;code&gt;TextWriter&lt;/code&gt; as a parameter.&amp;#160; These static methods are inherited from the WebPageExecutingBase class; their names are also configured by the RazorHostFactory.&amp;#160; The @ in the parameter is a little-used C# &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/aa664670%28v=vs.71%29.aspx&quot;&gt;syntactictal feature&lt;/a&gt; that allows keywords to be used as identifiers; it has nothing to do with Razor’s use of the @ character.&lt;/p&gt;

&lt;p&gt;Since helpers are compiled as normal methods, they can do almost anything that a normal method can.&amp;#160; However, because their contents are compiled inside a lambda expression, they have some limitations.&amp;#160; For example, helpers cannot use &lt;code&gt;ref&lt;/code&gt; or &lt;code&gt;out&lt;/code&gt; parameters, since &lt;a href=&quot;https://stackoverflow.com/questions/1365689/cannot-use-ref-or-out-parameter-in-lambda-expressions/1365865#1365865&quot;&gt;they cannot be used inside lambda expressions&lt;/a&gt;.&amp;#160; Helpers can take &lt;code&gt;params&lt;/code&gt; arrays or optional parameters.&lt;/p&gt;

&lt;p&gt;Also, Razor’s C# code parser doesn’t support generic helper methods, although there is no reason that they couldn’t be supported in a later release.&lt;/p&gt;

&lt;p&gt;The VB.Net code parser also doesn’t explicitly support generic helpers.&amp;#160; However, because VB.Net generics use parentheses, a VB.Net helper can declare a generic type parameter &lt;em&gt;instead of&lt;/em&gt; a normal parameter list.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        @Helper PrintType(Of T)
            @GetType(T)
        End Helper
        @PrintType(Of Dictionary(Of Integer, List(Of String)))()
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This trick is not very useful.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/03/dissecting-razor-part-8-static-helpers.html&quot;&gt;&lt;em&gt;Next Time&lt;/em&gt;: Static Helpers&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-5112256259124914398</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/02/dissecting-razor-part-6-function-blocks.html"/>
		<title type="text">Dissecting Razor, part 6: Function Blocks</title>
		<updated>2011-02-18T02:46:00+00:00</updated>
		<published>2011-02-28T16:09:33+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;After looking at how Razor’s &lt;a href=&quot;/2011/02/dissecting-razor-part-4-anatomy-of.html&quot;&gt;&lt;code&gt;Execute()&lt;/code&gt; method&lt;/a&gt; is generated, we will turn to class-level features.&lt;/p&gt;  &lt;p&gt;C# Razor pages can define class members inside of &lt;code&gt;@functions { ... }&lt;/code&gt; blocks.&amp;#160; These are Razor’s equivalent of &lt;code&gt;&amp;lt;script runat=&amp;quot;server&amp;quot;&amp;gt;&lt;/code&gt; blocks in ASPX pages.&amp;#160; VBHTML pages use &lt;code&gt;@Functions ... End Functions&lt;/code&gt; instead. &lt;/p&gt;  &lt;p&gt;Functions blocks are emitted directly into top of the generated class, regardless of their location in the original source.&amp;#160; Unlike code blocks, function blocks cannot contain markup.&lt;/p&gt;  &lt;p&gt;Here is a simple example:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        @functions{
            public int GetPageLength() {
                //Don't try this in production.
                return ((StringWriter)this.Output).ToString().Length;
            }
        }
        @GetPageLength() characters have been written so far.
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
@{ #error } &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that functions blocks can be defined anywhere, even in the middle of the markup.&amp;#160; The location of the block is totally irrelevant.&lt;/p&gt;

&lt;p&gt;Here is the generated C# source, with a comment indicating where the block used to be.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_Page_Razor_Functions_cshtml&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPage&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line hidden
#line 4 &quot;...\Functions.cshtml&quot;
&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetPageLength&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//Don't try this in production.&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StringWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line default
#line hidden
&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_Page_Razor_Functions_cshtml&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_asax&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ASP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_asax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;!DOCTYPE html&amp;gt;\r\n&amp;lt;html&amp;gt;\r\n    &amp;lt;body&amp;gt;\r\n        &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//Here was the functions block&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;\r\n        &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 10 &quot;...\Functions.cshtml&quot;
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetPageLength&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default
#line hidden
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; characters have been written so far.\r\n    &amp;lt;/body&amp;gt;\r\n&amp;lt;/html&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 13 &quot;...\Functions.cshtml&quot;
#error
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default
#line hidden
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;     &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The contents of the &lt;code&gt;functions&lt;/code&gt; block is inserted at the very top of the class, with the familiar &lt;code&gt;#line&lt;/code&gt; directives to pretend that it comes from the CSHTML.&lt;/p&gt;

&lt;p&gt;Notice that none of the whitespace around the &lt;code&gt;functions&lt;/code&gt; block is stripped; you can see the newline and indentation in the &lt;code&gt;@functions&lt;/code&gt; line in the first &lt;code&gt;WriteLiteral&lt;/code&gt; string, and the newline and indentation after the closing &lt;code&gt;}&lt;/code&gt; in the second &lt;code&gt;WriteLiteral&lt;/code&gt; string.&lt;/p&gt;

&lt;p&gt;Sure enough, the rendered HTML contains an extra blank like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        
        55 characters have been written so far.
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This means that putting a &lt;code&gt;functions&lt;/code&gt; block before the &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; will cause the HTML to start with an ugly blank line.&amp;#160; Therefore, it’s best to put &lt;code&gt;functions&lt;/code&gt; blocks at the end of the source, where the blank lines won’t matter.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-7-helpers.html&quot;&gt;&lt;em&gt;Next Time&lt;/em&gt;: Helpers&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1710821792428510177</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/02/dissecting-razor-part-5-use-source-luke.html"/>
		<title type="text">Dissecting Razor, part 5: Use the Source, Luke</title>
		<updated>2011-02-16T16:54:00+00:00</updated>
		<published>2011-02-18T02:50:23+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-4-anatomy-of.html&quot;&gt;Last time&lt;/a&gt;, we saw how basic Razor constructs are translated into C#.&lt;/p&gt;  &lt;p&gt;We can see the generated class by adding &lt;code&gt;@{ #error }&lt;/code&gt; to the page source. This creates a compiler error in the &lt;code&gt;Execute&lt;/code&gt; method, and the resulting Yellow Screen of Death contains a &lt;u&gt;Show Complete Compilation Source:&lt;/u&gt; link which will show the generated C# class.&amp;#160; &lt;/p&gt;  &lt;p&gt;Let’s start with a very simple page:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        1 + 2 = @(1 + 2)&lt;span class=&quot;nt&quot;&gt;&amp;lt;br&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
        @{ var source = &quot;&lt;span class=&quot;nt&quot;&gt;&amp;lt;b&amp;gt;&lt;/span&gt;bold &lt;span class=&quot;ni&quot;&gt;&amp;amp;amp;&lt;/span&gt; fancy&lt;span class=&quot;nt&quot;&gt;&amp;lt;/b&amp;gt;&lt;/span&gt;&quot;; }
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;code&amp;gt;&lt;/span&gt;@source&lt;span class=&quot;nt&quot;&gt;&amp;lt;/code&amp;gt;&lt;/span&gt; is rendered as
        @(new HtmlString(source))
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
@{ #error }&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This page is rendered like this: (after removing &lt;code&gt;@{ #error }&lt;/code&gt;)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        1 + 2 = 3&lt;span class=&quot;nt&quot;&gt;&amp;lt;br&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;code&amp;gt;&lt;/span&gt;&lt;span class=&quot;ni&quot;&gt;&amp;amp;lt;&lt;/span&gt;b&lt;span class=&quot;ni&quot;&gt;&amp;amp;gt;&lt;/span&gt;bold &lt;span class=&quot;ni&quot;&gt;&amp;amp;amp;&lt;/span&gt;amp; fancy&lt;span class=&quot;ni&quot;&gt;&amp;amp;lt;&lt;/span&gt;/b&lt;span class=&quot;ni&quot;&gt;&amp;amp;gt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/code&amp;gt;&lt;/span&gt; is rendered as
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;b&amp;gt;&lt;/span&gt;bold &lt;span class=&quot;ni&quot;&gt;&amp;amp;amp;&lt;/span&gt; fancy&lt;span class=&quot;nt&quot;&gt;&amp;lt;/b&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As expected, the expression &lt;code&gt;@source&lt;/code&gt; is automatically escaped.&amp;#160; Also notice that the newline and indentation around the code block (&lt;code&gt;@{ var ... }&lt;/code&gt;)&amp;#160; was not rendered – the Razor parser strips all whitespace surrounding code blocks.&amp;#160; This is a welcome improvement over the ASPX view engine.&lt;/p&gt;

&lt;p&gt;Now let’s look at how this HTML is generated.&amp;#160; This page is transformed into the following C# source:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ASP&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Collections.Generic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.IO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Linq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Web.Helpers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Web.Security&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Web.UI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Web.WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Web.WebPages.Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;WebMatrix.Data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;WebMatrix.WebData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_Page_Razor_SimplePage_cshtml&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebPage&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line hidden
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_Page_Razor_WriteTo_cshtml&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_asax&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ASP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_asax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ApplicationInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;!DOCTYPE html&amp;gt;\r\n&amp;lt;html&amp;gt;\r\n    &amp;lt;body&amp;gt;\r\n        1 + 2 = &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 4 &quot;...\SimplePage.cshtml&quot;
&lt;/span&gt;            &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default
#line hidden
&lt;/span&gt;            &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;br /&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 5 &quot;...\SimplePage.cshtml&quot;
&lt;/span&gt;            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;b&amp;gt;bold &amp;amp;amp; fancy&amp;lt;/b&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default
#line hidden
&lt;/span&gt;            &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;        &amp;lt;code&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 6 &quot;...\SimplePage.cshtml&quot;
&lt;/span&gt;            &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default
#line hidden
&lt;/span&gt;            &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;/code&amp;gt; is rendered as\r\n        &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 7 &quot;...\SimplePage.cshtml&quot;
&lt;/span&gt;            &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HtmlString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default
#line hidden
&lt;/span&gt;            &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;\r\n    &amp;lt;/body&amp;gt;\r\n&amp;lt;/html&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 10 &quot;...\SimplePage.cshtml&quot;
#error
#line default
#line hidden
&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The WebPageRazorEngineHost injects the &lt;code&gt;ApplicationInstance&lt;/code&gt; property into the CodeDOM tree; this property allows code in the page to access any custom properties in Global.asax.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, the page source is compiled into the &lt;code&gt;Execute()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;It uses &lt;code&gt;#line&lt;/code&gt; directives to pretend that its code is actually in the CSHTML page.&amp;#160; This means that code or line numbers appearing in error pages come from the original CSHTML source, making the code easier to find when debugging.&amp;#160; The &lt;code&gt;#line hidden&lt;/code&gt; directives indicate generated source that did not come from actual code in the CSHTML.&lt;/p&gt;

&lt;p&gt;As mentioned last time, literal HTML source is passed to the &lt;code&gt;WriteLiteral&lt;/code&gt; method, which is inherited from the &lt;code&gt;WebPageBase&lt;/code&gt; class.&amp;#160; This method writes its argument to the current output stream (which can vary when making sections).&amp;#160; These calls are wrapped in &lt;code&gt;#line hidden&lt;/code&gt; because they come from literal text, not code.&lt;/p&gt;

&lt;p&gt;The two code blocks (the variable declaration and the &lt;code&gt;#error&lt;/code&gt; directive) are copied straight into&amp;#160; &lt;code&gt;Execute()&lt;/code&gt;, wrapped in &lt;code&gt;#line&lt;/code&gt; directives that map them to the actual code lines in the CSHTML.&lt;/p&gt;

&lt;p&gt;The code nuggets are passed to the &lt;code&gt;Write&lt;/code&gt; method, and are similarly wrapped in &lt;code&gt;#line&lt;/code&gt; directives. &lt;/p&gt;

&lt;p&gt;Here is a more sophisticated Razor page:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        @{ const int count = 10; }
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;table&amp;gt;&lt;/span&gt;
            @for (int i = 0; i &lt;span class=&quot;nt&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;td&amp;gt;&lt;/span&gt;@i&lt;span class=&quot;nt&quot;&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;td&amp;gt;&lt;/span&gt;@(i * i)&lt;span class=&quot;nt&quot;&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
            }
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
@{ #error }&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code&gt;@for&lt;/code&gt; loop is a code block in the form of a control flow statement.&amp;#160; Razor’s C# parser is aware of C# control flow structures and parses them as code blocks.&amp;#160; (The VB parser does the same thing)&lt;/p&gt;

&lt;p&gt;Here is the generated &lt;code&gt;Execute()&lt;/code&gt; method, with &lt;code&gt;#line&lt;/code&gt; directives removed for clarity:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;!DOCTYPE html&amp;gt;\r\n&amp;lt;html&amp;gt;\r\n    &amp;lt;body&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;        &amp;lt;table&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;                &amp;lt;tr&amp;gt;\r\n                    &amp;lt;td&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;/td&amp;gt;\r\n                    &amp;lt;td&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;/td&amp;gt;\r\n                &amp;lt;/tr&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;        &amp;lt;/table&amp;gt;\r\n    &amp;lt;/body&amp;gt;\r\n&amp;lt;/html&amp;gt;\r\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#error
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here too, we see that all contiguous chunks of literal HTML are passed to &lt;code&gt;WriteLiteral&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This example has two code blocks – the &lt;code&gt;const&lt;/code&gt; declaration and the loop.&amp;#160; The &lt;code&gt;for&lt;/code&gt; loop code block has HTML inside of it – any HTML markup inside a code block is parsed as normal HTML and passed to &lt;code&gt;WriteLiteral&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-6-function-blocks.html&quot;&gt;&lt;em&gt;Next Time&lt;/em&gt;: Function blocks&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4805498511693380800</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/02/dissecting-razor-part-4-anatomy-of.html"/>
		<title type="text">Dissecting Razor, part 4: Anatomy of a Razor Page</title>
		<updated>2011-02-09T14:08:00+00:00</updated>
		<published>2011-02-18T03:43:51+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;After looking at the various assemblies in the WebPages framework, we will drill into the inner workings of Razor pages.&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;h2&gt;Razor Side&lt;/h2&gt;  &lt;p&gt;An ordinary CSHTML page is transformed into a class which inherits the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.webpages.webpage%28v=vs.99%29.aspx&quot;&gt;&lt;code&gt;WebPage&lt;/code&gt; class&lt;/a&gt;.&amp;#160; The generator overrides the abstract &lt;code&gt;Execute()&lt;/code&gt; method from the to render the page to the HTTP response stream.&lt;/p&gt;  &lt;p&gt;Except for class-level directives and constructs (which will be discussed later), all ordinary content in a Razor page end up in the Execute method.&amp;#160; &lt;/p&gt;  &lt;p&gt;There are three types of normal content: Literals, Code Blocks, and Code Nuggets.&lt;/p&gt;  &lt;p&gt;Literals include any normal text. Razor compiles literal text into calls to the &lt;code&gt;WriteLiteral&lt;/code&gt; method, with the text as a (correctly-escaped) string parameter.&amp;#160; Razor expects this method to write its parameter to the page.&lt;/p&gt;  &lt;p&gt;Code Blocks include &lt;code&gt;@{ ... }&lt;/code&gt; blocks, as well as control structures.&amp;#160; They’re Razor’s equivalent of &lt;code&gt;&amp;lt;% ... %&amp;gt;&lt;/code&gt; blocks.&amp;#160; The contents of a code block are emitted as-is in the Execute method.&amp;#160; Code blocks must contain complete statements, or they’ll result in C# syntax errors.&lt;/p&gt;  &lt;p&gt;VBHTML pages use &lt;code&gt;@Code ... End &lt;/code&gt;Code blocks instead. &lt;/p&gt;  &lt;p&gt;Code Nuggets are @-blocks.&amp;#160; They’re Razor’s equivalent of &lt;code&gt;&amp;lt;%: ... %&amp;gt;&lt;/code&gt; blocks in an ASPX page.&amp;#160; Scott Guthrie describes &lt;a href=&quot;https://weblogs.asp.net/scottgu/archive/2010/12/16/asp-net-mvc-3-implicit-and-explicit-code-nuggets-with-razor.aspx&quot;&gt;how these blocks are tokenized&lt;/a&gt;.&amp;#160; The contents of a code nugget are passed to the &lt;code&gt;Write&lt;/code&gt; method, which is expected to HTML-escape its parameter and print it.&lt;/p&gt;  &lt;h2&gt;WebPages Side&lt;/h2&gt;  &lt;p&gt;The WebPages framework’s&amp;#160; &lt;code&gt;Write&lt;/code&gt; method (which comes from the &lt;code&gt;WebPageBase&lt;/code&gt; class) takes a parameter of type &lt;code&gt;Object&lt;/code&gt;, allowing one to put any expression in a code nugget.&amp;#160; It passes its parameter to &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ee360286.aspx&quot;&gt;HttpUtility.HtmlEncode&lt;/a&gt;&lt;/code&gt;, which will call ToString() and HTML-escape the output.&amp;#160; If the parameter is an &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.ihtmlstring.aspx&quot;&gt;IHtmlString&lt;/a&gt;&lt;/code&gt;, &lt;code&gt;HtmlEncode&lt;/code&gt; will return its &lt;code&gt;ToHtmlString()&lt;/code&gt; method without escaping.&lt;/p&gt;  &lt;p&gt;The base class, method names, and default namespaces can be configured in the RazorEngineHost.&amp;#160; In addition, custom RazorEngineHosts can override the PostProcessGeneratedCode method to make arbitrary modifications to the generated code.&lt;/p&gt;  &lt;p&gt;The WebRazorHostFactory in System.Web.WebPages.Razor.dll can also read default namespaces, a default base type, and a custom host from the &lt;code&gt;&amp;lt;system.web.webPages.razor&amp;gt;&lt;/code&gt; section in Web.config:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;system.web.webPages.razor&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;host&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;factoryType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;MyProject.MyWebPageRazorHost&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;pages&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;pageBaseType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;MyProject.CustomWebPage&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;namespaces&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;namespace=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;MyProject.SomeNamespace&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/namespaces&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/pages&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/system.web.webPages.razor&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-5-use-source-luke.html&quot;&gt;&lt;em&gt;Next Time&lt;/em&gt;: Looking at the generated page&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-910318883188205728</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/02/dissecting-razor-part-3-razor-and-mvc.html"/>
		<title type="text">Dissecting Razor, part 3: Razor and MVC</title>
		<updated>2011-02-03T18:49:00+00:00</updated>
		<published>2011-02-09T14:09:35+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net MVC" term="asp-net-mvc" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-2-gluing-pieces.html&quot;&gt;Last time&lt;/a&gt;, we saw how standalone Razor pages are served.&lt;/p&gt;  &lt;p&gt;MVC3 maintains the strict separation between the WebPages framework and the Razor engine.1&lt;/p&gt;  &lt;h2&gt;Razor Side&lt;/h2&gt;  &lt;p&gt;Like the WebPages framework, MVC3 interacts with Razor indirectly, by relying on RazorBuildProvider from System.Web.WebPages.Razor.dll.&amp;#160;&amp;#160; However, MVC3 requires that Razor views inherit its own base class, &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.mvc.webviewpage.aspx&quot;&gt;System.Web.Mvc.WebViewPage&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;MVC3 adds a new @model directive, which can be used instead of @inherits to specify a strongly-typed model.&amp;#160; This syntax is implemented by customized RazorCodeParsers and RazorCodeLanguages in the System.Web.MVC.Razor namespaces.&amp;#160; These classes are invoked by MvcRazorEngineHosts from a custom RazorHostFactory registered in Views/Web.Config:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;system.web.webPages.razor&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;host&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;factoryType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;pages&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;pageBaseType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Web.Mvc.WebViewPage&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;namespaces&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;namespace=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Web.Mvc&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;namespace=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Web.Mvc.Ajax&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;namespace=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Web.Mvc.Html&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;add&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;namespace=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Web.Routing&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/namespaces&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/pages&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/system.web.webPages.razor&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2&gt;MVC Side&lt;/h2&gt;

&lt;p&gt;On the MVC side, MVC3 includes a new &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.mvc.razorviewengine.aspx&quot;&gt;RazorViewEngine&lt;/a&gt; which creates &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.mvc.razorview.aspx&quot;&gt;RazorView&lt;/a&gt; instances.&amp;#160; RazorView inherits the existing BuildManagerCompiledView class, which passes the view’s virtual path to the build manager.&amp;#160; RazorView will take the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.mvc.webviewpage.aspx&quot;&gt;WebViewPage&lt;/a&gt; from the build manager, find any matching start pages, and execute the view.&lt;/p&gt;

&lt;p&gt;As with the WebPages framework, one can substitute other templating engines.&amp;#160; One can register a build provider which compiles classes that inherit &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.mvc.webviewpage.aspx&quot;&gt;WebViewPage&lt;/a&gt;, then add a RazorViewEngine to &lt;code&gt;ViewEngines.Engines&lt;/code&gt; with additional extensions in its FileExtensions property.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-4-anatomy-of.html&quot;&gt;&lt;em&gt;Next time: &lt;/em&gt;Inside Razor Pages&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2819573016088544796</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/02/dissecting-razor-part-2-gluing-pieces.html"/>
		<title type="text">Dissecting Razor, part 2: Gluing the pieces together</title>
		<updated>2011-02-02T16:46:00+00:00</updated>
		<published>2011-02-03T18:51:34+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;/2011/01/dissecting-razor-part-1-pieces-of.html&quot;&gt;Last time&lt;/a&gt;, we saw that ASP.Net Web Pages are implemented in two independent assemblies. These assemblies are not directly connected to each-other.&lt;/p&gt;   &lt;h2&gt;Razor Side&lt;/h2&gt;  &lt;p&gt;System.Web.WebPages.Razor.dll contains the RazorBuildProvider class, which allows ASP.Net’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.aspx&quot;&gt;build system&lt;/a&gt; to compile Razor pages.&amp;#160; This class uses a WebRazorHostFactory to create WebPageRazorHosts used to process CSHTML (or VBHTML) files into CodeDOM trees.&amp;#160; It compiles the CodeDOM tree and returns the generated type(which will typically inherit System.Web.WebPages.WebPage) to the build system.&lt;/p&gt;  &lt;p&gt;WebPageRazorHost is coupled to the WebPages framework; it handles the non-standard base types for special pages (StartPage and ApplicationStartPage).&lt;/p&gt;  &lt;p&gt;RazorBuildProvider can be configured to use a different WebRazorHostFactory that creates custom WebPageRazorHosts.&amp;#160; (more on this later)&lt;/p&gt;  &lt;h2&gt;WebPages Side&lt;/h2&gt;  &lt;p&gt;The WebPages framework contains an internal WebPageHttpModule class which runs when an ASP.Net AppDomain is started.&amp;#160; It runs any _AppStart files, and hooks the request lifecycle to handle requests for CSHTML (or VBHTML) pages.&lt;/p&gt;  &lt;p&gt;Requests for Razor pages are handled by &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.webpages.webpagehttphandler.aspx&quot;&gt;System.Web.WebPages.WebPageHttpHandler&lt;/a&gt;.&amp;#160; This class passes the page’s virtual path to the build manager and gets a WebPage instance (It assumes that RazorBuildProvider will build the page and give a WebPage instance).&amp;#160; &lt;/p&gt;  &lt;p&gt;The handler calls the page’s ExecutePageHierarchy method to serve the page to the client.&amp;#160; This method runs any _PageStart pages in the page’s parent directories, then executes the page.&lt;/p&gt;  &lt;p&gt;The WebPageHttpHandler will also add a custom HTTP header, &lt;strong&gt;X-AspNetWebPages-Version&lt;/strong&gt;: &lt;code&gt;1.0&lt;/code&gt;.&amp;#160; This can be disabled by setting &lt;code&gt;WebPageHttpHandler.DisableWebPagesResponseHeader&lt;/code&gt; to false.&lt;/p&gt;  &lt;p&gt;As mentioned, System.Web.WebPages.dll is not directly tied to the Razor language and engine.&amp;#160; One can create a custom build provider which compiles classes that inherit WebPage, then call WebPageHttpHandler.RegisterExtension to tell the WebPages framework to handle requests to the extension, without using the Razor parser.&lt;/p&gt;  &lt;p&gt;The source code for this project is &lt;a href=&quot;https://aspnet.codeplex.com/releases/view/58781&quot;&gt;part of the same bundle&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-3-razor-and-mvc.html&quot;&gt;&lt;em&gt;Next time&lt;/em&gt;: MVC Razor views&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;

</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2157605876304002296</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/dissecting-razor-part-1-pieces-of.html"/>
		<title type="text">Dissecting Razor, part 1: Parts of the framework</title>
		<updated>2011-02-01T03:51:00+00:00</updated>
		<published>2011-02-02T16:47:51+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="dissecting-razor" term="dissecting-razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Razor involves two distinct components: The Razor engine and the WebPages framework.&lt;/p&gt;  &lt;p&gt;The Razor engine, in System.Web.Razor.dll, parses CSHTML (and VBHTML) files into CodeDOM trees.&amp;#160; Except for the word Web in project name, the engine has nothing to do with ASP.Net; it doesn’t even reference System.Web.dll.&amp;#160; In fact, it targets the .Net Client Profile, and only references mscorlib and System.dll.&lt;/p&gt;  &lt;p&gt;The Razor engine is aware of all of Razor’s syntax-level features (code nuggets, sections, helpers), but is not aware of what they mean; it blindly transforms them into function calls.&lt;/p&gt;  &lt;p&gt;The Razor engine can be used without ASP.Net for any kind of templating system.&amp;#160; This is done by inheriting the RazorEngineHost class to provide default code-generation settings (such as base class and method name), then passing the host to a RazorTemplateParser.&amp;#160; &lt;/p&gt;  &lt;p&gt;The standard syntax will be annoying when generating non-XML-like content.&amp;#160; To avoid this, one can write a custom MarkupParser to define a Razor syntax for a different markup language (such as CSS). &lt;/p&gt;  &lt;hr /&gt;The WebPages framework, in System.Web.Webpages.dll, is a set of classes to use with the Razor parser.&amp;#160; It contains the WebPage class which standard Razor pages inherit. This class defines the methods which are blindly called by the Razor parser, such as DefineSection and WriteLiteral.&amp;#160;&amp;#160;&amp;#160; This framework also handles _PageStart and _AppStart pages and contains the HtmlHelper infrastructure.    &lt;p&gt;The WebPages framework is not directly connected to the Razor parser.&amp;#160; It could theoretically be used with a different template engine, as long as the template engine emits the correct method calls and class definitions.&amp;#160; (more on this later)&lt;/p&gt;  &lt;p&gt;The WebPages framework also contains two sets of utility methods.&amp;#160; &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.helpers.aspx&quot;&gt;System.Web.Helpers&lt;/a&gt;.dll contains miscellaneous utility classes, such as Crypto and WebMail wrappers, plus grid and chart implementations.&amp;#160; &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/microsoft.web.helpers.aspx&quot;&gt;Microsoft.Web.Helpers&lt;/a&gt;.dll contains HTML helper classes which integrate with various third-party services, including Twitter, ReCaptcha, Google Analytics, and more.&amp;#160; Most of these helpers can also be used in ordinary ASPX pages.&lt;/p&gt;  &lt;p&gt;The source code for all of these projects is available &lt;a href=&quot;https://aspnet.codeplex.com/releases/view/58781&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;/2011/02/dissecting-razor-part-2-gluing-pieces.html&quot;&gt;&lt;em&gt;Next time&lt;/em&gt;: Gluing it all together&lt;/a&gt;.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1913121853705105374</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/modifying-html-strings-using-jquery.html"/>
		<title type="text">Modifying HTML strings using jQuery</title>
		<updated>2011-01-31T13:55:00+00:00</updated>
		<published>2011-01-31T14:02:52+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="jQuery" term="jquery" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="HTML" term="html" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://jquery.com&quot;&gt;jQuery&lt;/a&gt; makes it very easy to modify a DOM tree.&amp;#160; For example, to strip all hyperlinks (&lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags) from an element, we can write (&lt;strong&gt;&lt;a href=&quot;https://jsfiddle.net/SLaks/Q6VNH/&quot;&gt;demo&lt;/a&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;a[href]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;replaceWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;childNodes&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After getting used to this, &lt;a href=&quot;https://stackoverflow.com/questions/4750703/cant-modify-returned-ajax-variable&quot;&gt;one&lt;/a&gt; &lt;a href=&quot;https://stackoverflow.com/questions/4536329/whats-the-best-way-to-strip-out-only-the-anchor-html-tags-in-javascript-given-a&quot;&gt;might&lt;/a&gt; want to use jQuery to modify HTML contained in a string.&amp;#160; Here, however, the naïve approach does not work:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;htmlSource&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;htmlSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;a[href]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;replaceWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;childNodes&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code tries to remove all &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags from the HTML contained in the &lt;code&gt;htmlSource&lt;/code&gt; string.&amp;#160; However, what it actually does is create a detached DOM tree containing the new elements, strip all &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags in those elements, and throw the whole thing away.&amp;#160; It doesn’t modify the original string.&amp;#160; In fact, since the&amp;#160; &lt;code&gt;$&lt;/code&gt; function only takes a reference to an immutable string, this approach &lt;em&gt;cannot&lt;/em&gt; modify the original string.&lt;/p&gt;

&lt;p&gt;Instead, you need to retrieve the source from the DOM tree after modifying it, then assign that source back to the variable.&amp;#160; &lt;/p&gt;

&lt;p&gt;There is an additional subtlety with this approach.&amp;#160; jQuery cannot return the complete HTML source for a collection of elements.&amp;#160; Therefore, it is also necessary to wrap the HTML in a dummy element (typically a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;).&amp;#160;&amp;#160; One can then call &lt;code&gt;.html()&lt;/code&gt; to get the &lt;code&gt;innerHTML&lt;/code&gt; of the dummy element, which will contain exactly the desired content&lt;/p&gt;

&lt;p&gt;This also eliminates the distinction between root-level elements and nested elements.&amp;#160; If the original HTML string contains root-level &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; elements (which aren’t nested in other tags), writing &lt;code&gt;$(htmlSource).find('a')&lt;/code&gt; won’t find them, since &lt;code&gt;.find()&lt;/code&gt; only searches the &lt;em&gt;descendants&lt;/em&gt; of the elements in the jQuery object.&amp;#160; By wrapping the HTML in a dummy element, all of the elements in the original content become descendants, and can be returned by &lt;code&gt;.find()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here, therefore, is the correct way to modify an HTML string using jQuery:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;htmlSource&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tree&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;lt;div&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;htmlSource&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;a[href]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;replaceWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;childNodes&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;htmlSource&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4895941282151911436</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/generic-base-classes-in-aspnet-mvc.html"/>
		<title type="text">Generic Base Classes in ASP.Net MVC</title>
		<updated>2011-01-30T23:46:00+00:00</updated>
		<published>2011-01-30T23:46:25+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="generics" term="generics" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net MVC" term="asp-net-mvc" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;/2011/01/generic-base-classes-in-aspnet.html&quot;&gt;Last time&lt;/a&gt;, we saw that there are severe limitations in creating ASPX pages which inherit generic base classes.&amp;#160; Many readers were probably wondering how ASP.Net MVC works around this limitation.&amp;#160; In ASP.Net MVC views, people write pages like this all the time:&lt;/p&gt;  &lt;p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-aspx-cs&quot; data-lang=&quot;aspx-cs&quot;&gt;&amp;lt;%@ Page Language=&quot;C#&quot; 
    Inherits=&quot;ViewPage&amp;lt;IEnumerable&amp;lt;DataLayer.Product&amp;gt;&amp;gt;&quot; %&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/p&gt;

&lt;p&gt;ASP.Net MVC includes its own workaround for these limitations.&amp;#160; The Web.config file in the Views folder of an ASP.Net MVC project registers a &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.ui.pageparserfilter.aspx&quot;&gt;PageParserFilter&lt;/a&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;pages&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;validateRequest=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;pageParserFilterType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;&lt;/span&gt;
    &lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/pages&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.ui.pageparserfilter.aspx&quot;&gt;PageParserFilter&lt;/a&gt; is one of ASP.Net’s lesser-known extensibility points.&amp;#160; It can intercept different parts in the parsing process for an ASPX page and modify the page.&amp;#160; The MVC framework’s ViewTypeParserFilter will check whether the page’s &lt;code&gt;inherits=&amp;quot;&amp;quot;&lt;/code&gt; attribute contains &lt;code&gt;(&lt;/code&gt; or a &lt;code&gt;&amp;lt;&lt;/code&gt; characters; these characters can only appear in C# or VB.Net generic types, but not in the CLR’s native syntax. &lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;inherits=&amp;quot;&amp;quot;&lt;/code&gt; attribute contains these characters, it will save the attribute’s original value, then replace it with ​&lt;code&gt;ViewPage&lt;/code&gt; (Or &lt;code&gt;ViewMasterPage&lt;/code&gt; or &lt;code&gt;ViewUserControl&lt;/code&gt;, as appropriate).&amp;#160; This way, the rest of the built-in ASP.Net parser will see a normal type name that it knows how to parse. After the page finishes parsing, an internal &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.ui.controlbuilder.aspx&quot;&gt;ControlBuilder&lt;/a&gt; registered on MVC’s base types (&lt;code&gt;ViewPage&lt;/code&gt;, &lt;code&gt;ViewMasterPage&lt;/code&gt; or &lt;code&gt;ViewUserControl&lt;/code&gt;) will replace the base type in the generated CodeDOM tree with the original value of the &lt;code&gt;inherits=&amp;quot;&amp;quot;&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;The one problem with the hack is that it leaves the ASPX parsing engine unaware of the page’s actual base type.&amp;#160; Therefore, if you make a page that inherits a generic base class with additional properties, you won’t be able to set those properties in the &lt;code&gt;&amp;lt;%@ Page&lt;/code&gt; declaration (since the ASPX parser won’t know about them).&amp;#160; If you inherit a non-generic type, this mechanism will not kick in and page properties will work fine.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8664816070151914102</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/generic-base-classes-in-aspnet.html"/>
		<title type="text">Generic Base Classes in ASP.Net</title>
		<updated>2011-01-27T16:55:00+00:00</updated>
		<published>2011-02-02T16:48:18+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="generics" term="generics" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;ASP.Net pages can inherit from custom classes (as long as they inherit System.Web.UI.Page).&amp;#160; This can be useful to add utility functions or shared (code-behind) behaviors to your pages.&amp;#160; (Note that you could also use Extension methods or HTTP modules)&lt;/p&gt;  &lt;p&gt;However, if you try to inherit a generic base class, it won’t work:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DataPage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Page&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-aspx-cs&quot; data-lang=&quot;aspx-cs&quot;&gt;&amp;lt;%@ Page Language=&quot;C#&quot; Inherits=&quot;DataPage&amp;lt;string&amp;gt;&quot; %&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;This code results in a yellow screen of death, with the&amp;#160; parser error, &lt;code&gt;Could not load type 'DataPage&amp;lt;string&amp;gt;'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This happens because the ASP.Net page parser is unaware of C# generics syntax.&amp;#160; The familiar generics syntax (eg, &lt;code&gt;List&amp;lt;int&amp;gt;&lt;/code&gt;) is actually a C# innovation and is not used at all in the actual framework.&amp;#160; The “native” generics syntax, which is used by Reflection, is markedly different: &lt;code&gt;List`1[Int32]&lt;/code&gt; (namespaces omitted for brevity).&amp;#160; This name is returned by the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx&quot;&gt;Type.AssemblyQualifiedName property&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Since ASP.Net uses reflection APIs to load types, we need to specify generic types using CLR syntax (and with full namespaces).&amp;#160;&amp;#160; Therefore, the following page will work:&lt;/p&gt;


&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-aspx-cs&quot; data-lang=&quot;aspx-cs&quot;&gt;&amp;lt;%@ Page Language=&quot;C#&quot; 
    Inherits=&quot;TestSite.DataPage`1[[System.String, mscorlib]]&quot; %&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;However, it’s not so simple.&amp;#160; ASP.Net does not call &lt;code&gt;Type.GetType&lt;/code&gt; to parse these strings; instead, it loops over every referenced assembly and calls &lt;code&gt;Assembly.GetType&lt;/code&gt; on each one.&amp;#160; This is why you don’t need to include the assembly name whenever using the &lt;code&gt;Inherits&lt;/code&gt; attribute (which would have been necessary for &lt;code&gt;Type.GetType&lt;/code&gt;)&amp;#160; Ordinarily, this is very useful, but here, it comes back to bite you.&amp;#160; &lt;br /&gt;&lt;em&gt;It is not possible&lt;/em&gt; to parse a type from one assembly with a generic parameter from a different assembly using &lt;code&gt;Assembly.GetType&lt;/code&gt;, unless the generic parameter is in mscorlib.&lt;/p&gt;

&lt;p&gt;Therefore, for example, it is not possible to create an ASPX page that inherits &lt;code&gt;DataPage&amp;lt;DataLayer.Product&amp;gt;&lt;/code&gt; if &lt;code&gt;DataLayer.Product&lt;/code&gt; is in a different assembly than &lt;code&gt;DataPage&lt;/code&gt;.&amp;#160; As a workaround, one can create a non-generic class which inherits &lt;code&gt;DataPage&amp;lt;DataLayer.Product&amp;gt;&lt;/code&gt;, then make the ASPX page inherit this temporary class.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2011/01/generic-base-classes-in-aspnet-mvc.html&quot;&gt;&lt;em&gt;Next time&lt;/em&gt;: MVC magic&lt;/a&gt;&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4241907612246859502</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/building-connection-strings-in-net.html"/>
		<title type="text">Building Connection Strings in .Net</title>
		<updated>2011-01-26T14:20:00+00:00</updated>
		<published>2011-01-26T14:20:29+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="data" term="data" />
		
		
		<category scheme="https://blog.slaks.net/#" label="string-concatenation" term="string-concatenation" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;.Net developers frequently need to build connection strings, especially when connecting to Access or Excel files using OleDB.    &lt;br /&gt;Code like the following has been written countless times:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//Bad code! Do not use!&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Data Source=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openFileDialog1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;; &quot;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Extended Properties=\&quot;Excel 8.0\&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code looks innocuous at first glance, but will not work for all filenames.&amp;#160; If the filename contains characters like &lt;code&gt;'&lt;/code&gt;, &lt;code&gt;&amp;quot;&lt;/code&gt;, &lt;code&gt;;&lt;/code&gt;, or &lt;code&gt;=&lt;/code&gt;, this code will create an invalid connection string and throw an exception.&lt;/p&gt;

&lt;p&gt;The correct way to build connection strings is to use one of the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.data.common.dbconnectionstringbuilder.aspx&quot;&gt;DbConnectionStringBuilder&lt;/a&gt; classes.&amp;#160; This class implements a dictionary of key-value pairs in the connection string.&amp;#160; It has a &lt;code&gt;ConnectionString&lt;/code&gt; property which assembles the instance’s contents into a usable connection string.&amp;#160; Unlike the string concatenation shown above, this property will correctly escape all values.&lt;/p&gt;

&lt;p&gt;In addition, each of the database clients included with the .Net framework (SQL, OleDB, ODBC, Oracle, and Entity Framework) have their own inherited ConnectionStringBuilder classes in their respective namespaces.&amp;#160; These classes add type-safe properties for the the keys supported by their databases, and handle any special cases when generating the connection string.&lt;/p&gt;

&lt;p&gt;Thus, the correct way to write the above code is:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connBuilder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OleDbConnectionStringBuilder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;DataSource&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openFileDialog1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Provider&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Microsoft.Jet.OLEDB.4.0&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;connBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Extended Properties&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Excel 12.0 Macro&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

As an added bonus, these classes implement ICustomTypeDescriptor, so they can be bound to a PropertyGrid to allow the end-user to edit the connection string.&amp;#160; This can be seen in certain places in Visual Studio.  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-3862886444619630150</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/dont-call-htmlencode-in-razor.html"/>
		<title type="text">Don’t call Html.Encode in Razor Pages</title>
		<updated>2011-01-21T01:35:00+00:00</updated>
		<published>2011-03-06T16:35:13+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;One of the unique features of ASP.Net WebPages (formerly Razor) is automatic HTML encoding.&amp;#160; All strings printed by embedded code nuggets (@ blocks) are automatically HTML-encoded.&lt;/p&gt;  &lt;p&gt;In addition to this feature, Razor also includes the &lt;code&gt;Html.Encode&lt;/code&gt; method, probably copied from ASP.Net MVC.&amp;#160; Calling this method naively leads to a nasty surprise – the string will be double-encoded!     &lt;br /&gt;To see why, look more closely at a typical call: &lt;code&gt;@Html.Encode(&amp;quot;&amp;lt;text&amp;gt;&amp;quot;)&lt;/code&gt;.&amp;#160; This Razor markup will call &lt;code&gt;Html.Encode&lt;/code&gt;, which returns the &lt;em&gt;string&lt;/em&gt; &lt;code&gt;&amp;quot;&amp;amp;lt;text&amp;amp;gt;&amp;quot;&lt;/code&gt;.&amp;#160;&amp;#160; Since it returns a string and not an &lt;code&gt;IHtmlString&lt;/code&gt;, the Razor engine will encode it again, and render &lt;code&gt;&amp;amp;amp;lt;text&amp;amp;amp;gt;&lt;/code&gt;.&lt;/p&gt;  &lt;p&gt;Careful thought indicates that this behavior is probably correct.&amp;#160; The programmer (hopefully) knows that Razor will escape its output, so the call to &lt;code&gt;Html.Encode&lt;/code&gt; should be an attempt to display &lt;em&gt;encoded&lt;/em&gt; text.&amp;#160; In fact, this is the simplest way to display HTML-encoded text in a Razor view.&amp;#160; &lt;/p&gt;  &lt;p&gt;However, even if it is correct, the behavior is unexpected and should not be relied upon.&amp;#160; The unambiguous way to display encoded text is to call &lt;code&gt;Html.Raw&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;@Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Double-encoded &amp;lt;html&amp;gt; text!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Although it is long and clunky, this clearly shows that the text will be double-encoded.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Exercise for the reader&lt;/em&gt;: Why is it also necessary to call &lt;code&gt;Html.Raw&lt;/code&gt;?&lt;/p&gt;
&lt;/div&gt;</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-5331866883235257023</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/optional-parameters-in-c-4.html"/>
		<title type="text">Optional Parameters in C# &lt; 4</title>
		<updated>2011-01-19T22:04:00+00:00</updated>
		<published>2011-02-03T18:55:24+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="optional-parameters" term="optional-parameters" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;C# 4.0 adds support for &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dd264739.aspx&quot;&gt;optional parameters&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The following code prints 4:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;TestMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TestMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Optional parameters are a compiler feature.&amp;#160; The compiler will emit a normal method with the IL &lt;code&gt;[opt]&lt;/code&gt; attribute and a &lt;code&gt;.param&lt;/code&gt; declaration that includes a default value:&lt;/p&gt;


&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;.method hidebysig static void 
        TestMethod([opt] int32 i) cil managed
{
    .param [1] = int32(4)
    .maxstack 8
    L_0001: ldarg.0 
    L_0002: call void [mscorlib]System.Console::WriteLine(int32)
    L_0007: ret 
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;Earlier versions of the C# compiler will ignore this metadata.&amp;#160; Therefore, you can call such methods in earlier versions of C#, but you will always need to pass the optional parameters.&lt;/p&gt;

&lt;p&gt;You can also create methods with optional parameters in earlier versions of C#.&amp;#160; You can force the compiler to emit this metadata using the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.optionalattribute.aspx&quot;&gt;&lt;code&gt;[Optional]&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.defaultparametervalueattribute.aspx&quot;&gt;&lt;code&gt;[DefaultParameterValue]&lt;/code&gt;&lt;/a&gt; attributes. 

  &lt;br /&gt;(in the System.Runtime.InteropServices namespace)&lt;/p&gt;

&lt;p&gt;The following C# 1 code will compile identically to the above:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TestMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DefaultParameterValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Since the older compilers cannot consume optional parameters, you will always need to pass &lt;code&gt;i&lt;/code&gt; when calling this method from C# &amp;lt; 4.&amp;#160; However, in C# 4, you can call this method without passing the parameter.&lt;/p&gt;

&lt;p&gt;Unfortunately, the syntax parser used by VS2010 doesn’t recognize these attributes.&amp;#160; Therefore, if you attempt to call a method using these attributes, inside the project that defines the method, without specifying the parameter, the IDE will show a syntax error.&amp;#160; The compiler itself, however, will work correctly.&amp;#160; I &lt;a href=&quot;https://connect.microsoft.com/VisualStudio/feedback/details/636954/vs2010-background-compiler-cannot-handle-attribute-based-optional-parameters&quot;&gt;reported this bug on Connect&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;[DefaultParameterValue]&lt;/code&gt; attribute is optional; omitting it will use the type’s default value (&lt;code&gt;0&lt;/code&gt;, an empty struct, or &lt;code&gt;null&lt;/code&gt;, as appropriate) as the default.&lt;/p&gt;

&lt;p&gt;These attributes can also be used to create methods with optional parameters using &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/y2k85ax6.aspx&quot;&gt;CodeDOM&lt;/a&gt;, which cannot emit the new syntax.&lt;/p&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8310792801475341255</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/writing-output-in-razor-helpers-using.html"/>
		<title type="text">Writing output in Razor helpers using code</title>
		<updated>2011-01-12T20:22:00+00:00</updated>
		<published>2011-03-08T16:44:06+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="Razor" term="razor" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net WebPages" term="asp-net-webpages" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Razor-helpers" term="razor-helpers" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="ASP.Net" term="asp-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;The new ASP.Net WebPages view engine (formerly &lt;a href=&quot;https://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx&quot;&gt;Razor&lt;/a&gt;) allows you to create reusable parameterized blocks of HTML called &lt;a href=&quot;https://weblogs.asp.net/mikaelsoderstrom/archive/2010/10/06/declarative-helpers-in-razor.aspx&quot;&gt;helpers&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;  
&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;@helper&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;@current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;This helper will write out the first &lt;em&gt;count&lt;/em&gt; &lt;a href=&quot;https://en.wikipedia.org/wiki/Fibonacci_number&quot;&gt;Fibonacci numbers&lt;/a&gt;.&amp;#160; It can be called by writing &lt;code&gt;@Fibonacci(30)&lt;/code&gt; in the page that defines the helper.&lt;/p&gt;

&lt;p&gt;Using Razor syntax in this code looks strange.&amp;#160; Razor syntax is designed to write HTML tags.&amp;#160; Since I’m printing plain text, I need to use the&amp;#160; &lt;a href=&quot;https://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx&quot;&gt;&lt;code&gt;@:&lt;/code&gt; escape&lt;/a&gt; (or the &lt;code&gt;&amp;lt;text&amp;gt;&lt;/code&gt; tag) in order to output my text.&amp;#160; This small bit of code looks confusing and can get lost inside larger blocks of server-side code.&lt;/p&gt;

&lt;p&gt;Instead, I can use an undocumented hack.&amp;#160; Razor helpers are implemented by compiling a lambda expression as an &lt;code&gt;Action&amp;lt;TextWriter&amp;gt;&lt;/code&gt;.&amp;#160; The lambda expression receives a &lt;code&gt;TextWriter&lt;/code&gt; parameter named &lt;code&gt;__razor_helper_writer&lt;/code&gt;.&amp;#160; (You can see this by writing a Razor page with a compilation error and clicking Show Complete Compilation Source)&amp;#160; There is nothing preventing me from using this parameter yourself.&amp;#160; (it even shows up in IntelliSense!)&lt;/p&gt;

&lt;p&gt;Therefore, I can rewrite the helper as follows:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;@helper&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;__razor_helper_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;);&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


&lt;p&gt;Remember to correctly escape your text by calling &lt;code&gt;Html.Encode&lt;/code&gt;.&amp;#160; Since this writes directly to the output stream, it doesn’t get the benefit of Razor’s automatic escaping.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: &lt;/em&gt;This relies on undocumented implementation details of the Razor compiler, and may change in future releases. I would not recommend doing this. 

  &lt;br /&gt;Instead, you can write a function:&lt;/p&gt;

&lt;div class=&quot;razor&quot;&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;@functions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;);&lt;/span&gt;

            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;prev&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


This can be called the same way as the helper.&amp;#160; Instead of using the special helper support, the call will just print a string, the same you print any other string.&amp;#160; Therefore, the function’s output will be HTML-escaped.&amp;#160; To prevent that, you can change the function to return an &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.htmlstring.aspx&quot;&gt;HtmlString&lt;/a&gt;&lt;/code&gt;.


  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2890885898739485997</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2011/01/binding-to-lists-of-datarows.html"/>
		<title type="text">Binding to lists of DataRows</title>
		<updated>2011-01-10T20:30:00+00:00</updated>
		<published>2011-01-10T22:02:38+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="data-binding" term="data-binding" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label="LINQ" term="linq" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;.Net DataTables can be very useful when writing data-driven applications.&amp;#160; However, they have one limitation: There is no obvious way to databind a grid (or other control) to an arbitrary list of datarows from a table.    &lt;br /&gt;You can bind to an entire table directly by setting a DataSource to the DataTable itself, and you can bind to a subset of a table by creating a DataView with a filter.&amp;#160; &lt;/p&gt;  &lt;p&gt;In general, you cannot bind to an &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; (eg, a LINQ query); the databinding infrastructure can only handle an &lt;code&gt;IList&lt;/code&gt; (non-generic) or an &lt;code&gt;IListSource&lt;/code&gt;.&amp;#160; This is true for any kind of datasource.&amp;#160; Therefore, to bind to any LINQ query, you need to call &lt;code&gt;.ToList()&lt;/code&gt;.&amp;#160; (Or &lt;code&gt;.ToArray()&lt;/code&gt;)&lt;/p&gt;  &lt;p&gt;However, when binding to a DataTable, you&amp;#160; can’t even use a &lt;code&gt;List&amp;lt;DataRow&amp;gt;&lt;/code&gt;.&amp;#160; If you try, you’ll get four columns (RowError, RowState, Table, and HasErrors) and no useful information.&amp;#160; This happens because the &lt;code&gt;List&amp;lt;DataRow&amp;gt;&lt;/code&gt; doesn’t tell the databinding infrastructure about the special properties of the DataRows.&amp;#160; To understand the problem, some background is necessary&lt;/p&gt;  &lt;p&gt;Databinding is controlled by the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.windows.forms.listbindinghelper.aspx&quot;&gt;ListBindingHelper&lt;/a&gt; and &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.componentmodel.typedescriptor.aspx&quot;&gt;TypeDescriptor&lt;/a&gt; classes.&amp;#160; When you bind to a list, the &lt;a href=&quot;https://https://msdn.microsoft.com/en-us/library/ms159799.aspx&quot;&gt;ListBindingHelper.GetListItemProperties method&lt;/a&gt; is called to get the columns in the list.&amp;#160; If the list implements the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.componentmodel.itypedlist.aspx&quot;&gt;&lt;code&gt;ITypedList&lt;/code&gt; interface&lt;/a&gt;, its &lt;code&gt;GetItemProperties&lt;/code&gt;&amp;#160; method is called.&amp;#160; Otherwise, it will use TypeDescriptor to get the properties of the first item in the list.&amp;#160; (this uses reflection)&lt;/p&gt;  &lt;p&gt;The DataView class (which DataTable also binds through, using &lt;code&gt;IListSource&lt;/code&gt;) implements &lt;code&gt;ITypedList&lt;/code&gt; and returns DataColumnPropertyDescriptors that expose the columns in the table.&amp;#160; This is why you can bind to a DataView or DataTable and see columns.&amp;#160; However, when you bind to a &lt;code&gt;List&amp;lt;DataRow&amp;gt;&lt;/code&gt;, there is no &lt;code&gt;ITypedList&lt;/code&gt; that can return the columns as properties.&amp;#160; It therefore falls back on reflection and shows the physical properties of the &lt;code&gt;DataRow&lt;/code&gt; class.&lt;/p&gt;  &lt;p&gt;To solve this issue, you need to wrap the list in a DataView so that you can take advantage of its &lt;code&gt;ITypedList&lt;/code&gt; implementation.&amp;#160; You can do that using the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb918807.aspx&quot;&gt;&lt;code&gt;AsDataView()&lt;/code&gt; method&lt;/a&gt;.&amp;#160; This method is only available on the &lt;code&gt;DataTable&lt;/code&gt;&amp;#160; and &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb907979.aspx&quot;&gt;EnumerableRowCollection&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/code&gt; classes; it cannot be called on an arbitrary LINQ query.&amp;#160; You can only get an &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb907979.aspx&quot;&gt;EnumerableRowCollection&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/code&gt; by calling &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.data.enumerablerowcollectionextensions.aspx&quot;&gt;special versions of&lt;/a&gt; the Cast, OrderBy, Where, and Select methods from a DataTable.&amp;#160; &lt;/p&gt;  &lt;p&gt;Therefore, you can databind to a simple LINQ query by calling &lt;code&gt;AsDataView()&lt;/code&gt; on the query.&amp;#160; To bind to a &lt;code&gt;List&amp;lt;DataRow&amp;gt;&lt;/code&gt;, or to a more complicated query, you can use an ugly hack:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataSource&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AsEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                       &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                       &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AsDataView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The&amp;#160; &lt;code&gt;AsEnumerable()&lt;/code&gt; call is not needed for typed datasets.&lt;/p&gt;

&lt;p&gt;You can also call &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/bb396189.aspx&quot;&gt;CopyToDataTable()&lt;/a&gt;&lt;/code&gt;, which will works on an arbitrary &lt;code&gt;IEnumerable&amp;lt;DataRow&amp;gt;&lt;/code&gt;.&amp;#160;&amp;#160; However, it makes deep copies of the rows, so it isn’t helpful if you want the user to update the data, or if you want the user to see changes made (in code) to the original datarows.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8378284758038699494</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/partial-type-inference-in-net.html"/>
		<title type="text">Partial Type Inference in .Net</title>
		<updated>2010-12-30T01:58:00+00:00</updated>
		<published>2010-12-30T02:06:02+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="type-inference" term="type-inference" />
		
		
		<category scheme="https://blog.slaks.net/#" label="generics" term="generics" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;When designing fluent APIs, one issue that comes up is partial type inference.&amp;#160; If a method has two type parameters, there is no way to call it and only specify one of the type parameters (and leave the other inferred by the compiler)&lt;/p&gt;  &lt;p&gt;For example, suppose we are creating a type-safe wrapper around a parameterized SqlCommand.    &lt;br /&gt;Ideally, it would be called like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DbConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ExecuteScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;SELECT COUNT(*) FROM TableName WHERE Modified &amp;gt; someDate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someDate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Where the generic parameter specifies the return type.&lt;/p&gt;

&lt;p&gt;In order to implement this efficiently, one would create methods at runtime which add DbParameters for each property in the anonymous type, and &lt;a href=&quot;http://stackoverflow.com/questions/686630/static-generic-class-as-dictionary&quot;&gt;store them in a static generic class&lt;/a&gt;.&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;It would look something like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Extensions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AddParams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDbCommand&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ParamAdders&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Adder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ParamAdders&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ParamAdder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDbCommand&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ParamAdder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Adder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CreateParamAdder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ParamAdder&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CreateParamAdder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;However, that requires that the ExecuteScalar extension method take TParam as a generic parameter.&amp;#160; Since anonymous types can only be passed as generic parameters via type inference, this makes it impossible to pass the return type as a generic parameter.&lt;/p&gt;

&lt;p&gt;To fix this issue, we can split the generic parameters across two methods.&amp;#160; We can change the extension method to take a single generic parameter, and return a generic class with a method that takes the other generic parameter.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BetterExtensions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlStatement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDbConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqlText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlStatement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqlText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SqlStatement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TReturn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IHideObjectMembers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SqlStatement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDbConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Connection&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDbConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Connection&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TReturn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TReturn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TParam&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I use an &lt;a href=&quot;http://www.clariusconsulting.net/blogs/kzu/archive/2008/03/10/58301.aspx&quot;&gt;IHideObjectMembers interface&lt;/a&gt; to hide the methods inherited from Object from IntelliSense.&amp;#160; Note that the interface must be defined in an assembly outside of your solution. (or, to be more precise, that isn’t a Project reference)&lt;/p&gt;

&lt;p&gt;This version would be called like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDbConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;SELECT COUNT(*) FROM TableName WHERE Modified &amp;gt; someDate&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someDate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The return type is specified explicitly in the call to &lt;code&gt;Sql&amp;lt;T&amp;gt;()&lt;/code&gt;, and the parameter type is passed implicitly to &lt;code&gt;Execute&amp;lt;TParam&amp;gt;()&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-5251896001691898669</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/simplifying-value-comparison-semantics.html"/>
		<title type="text">Simplifying Value Comparison Semantics</title>
		<updated>2010-12-29T03:10:00+00:00</updated>
		<published>2012-02-05T03:00:37+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="comparison" term="comparison" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;A common chore in developing real-world C# applications is implementing value semantics for equality.&amp;#160; This involves implementing &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/ms131187.aspx&quot;&gt;IEquatable&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/code&gt;, overriding &lt;code&gt;Equals()&lt;/code&gt; and &lt;code&gt;GetHashCode()&lt;/code&gt;, and overloading the &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;!=&lt;/code&gt; operators.&lt;/p&gt;  &lt;p&gt;Implementing these methods is a time-consuming and repetitive task, and is easy to get wrong, especially &lt;code&gt;GetHashCode()&lt;/code&gt;.&amp;#160; In particular, &lt;a title=&quot;What is the best algorithm for an overridden System.Object.GetHashCode?&quot; href=&quot;https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode/263416#263416&quot;&gt;the best way implement GetHashCode()&lt;/a&gt;&lt;code&gt;&lt;/code&gt; is much more complicated than &lt;code&gt;return x.GetHashCode() ^ y.GetHashCode()&lt;/code&gt;.&lt;/p&gt;  &lt;p&gt;To simplify this task, I created a &lt;code&gt;ValueComparer&lt;/code&gt; class:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;///&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// Contains all of the properties of a class that &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// are used to provide value semantics.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;///&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;///&amp;lt;remarks&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// You can create a static readonly ValueComparer for your class,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// then call into it from Equals, GetHashCode, and CompareTo.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;///&amp;lt;/remarks&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueComparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IComparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEqualityComparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ValueComparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;params&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Properties&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReadOnlyCollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReadOnlyCollection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Properties&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ReferenceEquals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//Object.Equals handles strings and nulls correctly&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Properties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;All&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;    
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//https://stackoverflow.com/questions/263400/263416#263416&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetHashCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;unchecked&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Properties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;23&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;23&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetHashCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Properties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//The properties can be any type including null.&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;comp&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Comparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DefaultInvariant&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;    
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comp&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;comp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This class implements an external comparer that compares two instances by an ordered list of properties.&lt;/p&gt;

&lt;p&gt;ValueComparer can be used as a standalone &lt;code&gt;IComparer&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;IEqualityComparer&amp;lt;T&amp;gt;&lt;/code&gt; implementation.&lt;/p&gt;

&lt;p&gt;It can also be used to implement value semantics within a type. 
  &lt;br /&gt;For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IComparable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEquatable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IComparable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FirstName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LastName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Phone&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Email&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetHashCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Comparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetHashCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CompareTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Comparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IComparable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CompareTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CompareTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Comparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ValueComparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Comparer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ValueComparer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LastName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FirstName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Phone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Email&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To simplify this task, I created a &lt;a href=&quot;https://www.slaks.net/Files/ValueComparer.snippet&quot;&gt;code snippet&lt;/a&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;CodeSnippets&lt;/span&gt;  &lt;span class=&quot;na&quot;&gt;xmlns=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;CodeSnippet&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Format=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1.0.0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;Header&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;Title&amp;gt;&lt;/span&gt;ValueComparer&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Title&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;Shortcut&amp;gt;&lt;/span&gt;vc&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Shortcut&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;Description&amp;gt;&lt;/span&gt;Code snippet for equality methods using ValueComparer&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Description&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;Author&amp;gt;&lt;/span&gt;SLaks&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Author&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;SnippetTypes&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;SnippetType&amp;gt;&lt;/span&gt;Expansion&lt;span class=&quot;nt&quot;&gt;&amp;lt;/SnippetType&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/SnippetTypes&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Header&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;Snippet&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;Declarations&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;Literal&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Editable=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;ID&amp;gt;&lt;/span&gt;classname&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ID&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;ToolTip&amp;gt;&lt;/span&gt;Class name&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ToolTip&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Default&amp;gt;&lt;/span&gt;ClassNamePlaceholder&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Default&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Function&amp;gt;&lt;/span&gt;ClassName()&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Function&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Literal&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Declarations&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;Code&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;csharp&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;cp&quot;&gt;&amp;lt;![CDATA[public override int GetHashCode() { return Comparer.GetHashCode(this); }
        public int CompareTo($classname$ obj) { return Comparer.Compare(this, obj); }
        int IComparable.CompareTo(object obj) { return CompareTo(obj as $classname$); }
        public bool Equals($classname$ obj) { return Comparer.Equals(this, obj); }
        public override bool Equals(object obj) { return Equals(obj as $classname$); }
        static readonly ValueComparer&amp;lt;$classname$&amp;gt;&lt;/span&gt; Comparer = new ValueComparer&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;$classname$&amp;gt;(
            o =&amp;gt; o.$end$
        );]]&amp;gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Code&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Snippet&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/CodeSnippet&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/CodeSnippets&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

It can also be downloaded &lt;a href=&quot;http://www.slaks.net/Files/ValueComparer.snippet&quot;&gt;here&lt;/a&gt;; save it to My Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets\


  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1877917720070472499</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/when-shouldnt-you-write-ref-this.html"/>
		<title type="text">When shouldn’t you write ref this?</title>
		<updated>2010-12-22T22:54:00+00:00</updated>
		<published>2011-12-21T00:51:40+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="value-types" term="value-types" />
		
		
		<category scheme="https://blog.slaks.net/#" label="iterators" term="iterators" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;&lt;a href=&quot;/2010/12/when-can-you-write-ref-this.html&quot;&gt;Last time&lt;/a&gt;, we saw that the &lt;code&gt;this&lt;/code&gt; parameter to an instance method in a struct is passed by reference, allowing the method to re-assign &lt;code&gt;this&lt;/code&gt; or pass it as a &lt;code&gt;ref&lt;/code&gt; parameter.&lt;/p&gt;  &lt;p&gt;Due to limitations in the CLR, the &lt;code&gt;this&lt;/code&gt; parameter to an iterator method is &lt;em&gt;not&lt;/em&gt; a reference to the caller’s struct, and is instead a copy of the value.&amp;#160; Quoting the spec (§7.6.7)&lt;/p&gt;  &lt;blockquote&gt;   &lt;ul&gt;     &lt;li&gt;When this is used in a primary-expression within an instance method or instance accessor of a struct, it is classified as a variable. The type of the variable is the instance type (§10.3.1) of the struct within which the usage occurs.        &lt;ul&gt;         &lt;li&gt;If the method or accessor is not an iterator (§10.14), the this variable represents the struct for which the method or accessor was invoked, and behaves exactly the same as a ref parameter of the struct type. &lt;/li&gt;          &lt;li&gt;If the method or accessor is an iterator, the this variable represents a &lt;em&gt;copy&lt;/em&gt; of the struct for which the method or accessor was invoked, and behaves exactly the same as a &lt;em&gt;value&lt;/em&gt; parameter of the struct type. &lt;/li&gt;       &lt;/ul&gt;     &lt;/li&gt;   &lt;/ul&gt; &lt;/blockquote&gt;  &lt;p&gt;To explain why, you’ll need to understand how iterators are compiled.&amp;#160; &lt;a href=&quot;https://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx&quot;&gt;As Jon Skeet explains&lt;/a&gt;, an iterator method is compiled into a nested class that implements IEnumerable, with the original code transformed into a state machine.&amp;#160; This nested class has fields to store the method’s parameters (which includes &lt;code&gt;this&lt;/code&gt; for instance methods) so that the iterator code can use them.&amp;#160; &lt;/p&gt;  &lt;p&gt;This is why &lt;a href=&quot;https://blogs.msdn.com/b/ericlippert/archive/2009/07/13/iterator-blocks-part-two-why-no-ref-or-out-parameters.aspx&quot;&gt;iterators cannot take &lt;code&gt;ref&lt;/code&gt; parameters&lt;/a&gt;; the CLR cannot store a reference (as opposed to a reference &lt;em&gt;type&lt;/em&gt;) in a field.&amp;#160; Therefore, the &lt;code&gt;this&lt;/code&gt; parameter is passed to iterator methods by value, not by reference.     &lt;br /&gt;This is also why &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/6ea4324b.aspx&quot;&gt;anonymous methods in structs cannot use &lt;code&gt;this&lt;/code&gt;&lt;/a&gt;; anonymous methods are also compiled to methods in separate classes and so cannot inherit the &lt;code&gt;ref&lt;/code&gt; parameter.&lt;/p&gt;  &lt;p&gt;This means that if you change the &lt;code&gt;Mutate&lt;/code&gt; method &lt;a href=&quot;/2010/12/when-can-you-write-ref-this.html&quot;&gt;from before&lt;/a&gt; into an iterator, the code will still compile (!), but the calling method will not see the changes.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Mutable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Mutable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MutateWrong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//Force the iterator to execute&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;In Main(): &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Mutable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MutateWrong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Mutable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;MutateStruct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Inside MutateWrong(): &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MutateStruct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mutable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++;&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;
This code prints 
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;Inside MutateWrong(): 1
In Main(): 0&lt;/pre&gt;&lt;/blockquote&gt;
In summary, don’t mutate structs in iterators (or &lt;a href=&quot;https://stackoverflow.com/questions/441309/why-are-mutable-structs-evil&quot;&gt;at all&lt;/a&gt;, if you can help it). 

&lt;br /&gt;&lt;a href=&quot;https://stackoverflow.com/q/4514538/34397&quot;&gt;I don’t know why this isn’t a compiler error.&lt;/a&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-3950602889384290075</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/when-can-you-write-ref-this.html"/>
		<title type="text">When can you write ref this?</title>
		<updated>2010-12-22T20:50:00+00:00</updated>
		<published>2010-12-22T22:55:03+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="value-types" term="value-types" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Usually, you cannot pass &lt;code&gt;ref this&lt;/code&gt; as a parameter, since &lt;code&gt;this&lt;/code&gt; is not a writable field.&amp;#160; However, that’s not true for value types.&amp;#160; The &lt;code&gt;this&lt;/code&gt; field of a value type is a &lt;em&gt;writable&lt;/em&gt; value.&lt;/p&gt;  &lt;p&gt;To quote the spec (§5.1.5)&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Within an instance method or instance accessor of a struct type, the &lt;code&gt;this&lt;/code&gt; keyword behaves exactly as a reference parameter of the struct type (§7.6.7).&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Therefore, the following code prints 1:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Mutable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Mutable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Mutate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Mutable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Mutate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Mutable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; 
        &lt;span class=&quot;nf&quot;&gt;MutateStruct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MutateStruct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mutable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++;&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In practice, this should never come up, since &lt;a href=&quot;https://stackoverflow.com/questions/441309/why-are-mutable-structs-evil&quot;&gt;mutable structs are &lt;em&gt;evil&lt;/em&gt;&lt;/a&gt; and should be avoided at all costs. &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/2010/12/when-shouldnt-you-write-ref-this.html&quot;&gt;Next time&lt;/a&gt;: This doesn’t always work.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1294289107482175748</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/nothing-vs-null.html"/>
		<title type="text">Nothing vs Null</title>
		<updated>2010-12-22T17:28:00+00:00</updated>
		<published>2010-12-22T17:28:22+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="VB.Net" term="vb-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="conditional" term="conditional" />
		
		
		<category scheme="https://blog.slaks.net/#" label="" term="conditional" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		
		
		<category scheme="https://blog.slaks.net/#" label="" term="" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;VB.Net’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/0x9tb07z.aspx&quot;&gt;&lt;code&gt;Nothing&lt;/code&gt; keyword&lt;/a&gt; is is not the same as C#’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/edakx9da.aspx&quot;&gt;&lt;code&gt;null&lt;/code&gt;&lt;/a&gt;.&amp;#160; MSDN states, “Assigning Nothing to a variable sets it to the default value for its declared type. If that type contains variable members, they are all set to their default value”.&lt;/p&gt;  &lt;p&gt;In other words, the &lt;code&gt;Nothing&lt;/code&gt; keyword is actually equivalent to C#’s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/xwth0h0d%28v=VS.100%29.aspx&quot;&gt;&lt;code&gt;default(T)&lt;/code&gt; keyword&lt;/a&gt;, where &lt;code&gt;T&lt;/code&gt; is the type that the expression is used as.&lt;/p&gt;  &lt;p&gt;This can lead to nasty surprises with nullable types in conditional operators.&amp;#160; &lt;br /&gt;In C#, the expression &lt;code&gt;(...) ? null : 1&lt;/code&gt; will not compile, since “there is no implicit conversion between '&amp;lt;null&amp;gt;' and 'int'”.&amp;#160;&amp;#160;&amp;#160; Since&amp;#160; &lt;code&gt;null&lt;/code&gt; is an untyped expression, the type of the conditional is inferred to be &lt;code&gt;int&lt;/code&gt;, resulting in an error because &lt;code&gt;null&lt;/code&gt; cannot be converted to &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;  &lt;p&gt;In VB.Net, by contrast, the equivalent expression, &lt;code&gt;If((...), Nothing, 1)&lt;/code&gt;, &lt;em&gt;will &lt;/em&gt;compile, but will have unexpected results.&amp;#160; Here too, &lt;code&gt;Nothing&lt;/code&gt; is an untyped expression, so the type of the conditional is inferred to be &lt;code&gt;Integer&lt;/code&gt;.&amp;#160; However, unlike &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;Nothing&lt;/code&gt; &lt;em&gt;can &lt;/em&gt;be converted to &lt;code&gt;Integer&lt;/code&gt;, so this is compiled as &lt;code&gt;If((...), &lt;strong&gt;0&lt;/strong&gt;, 1)&lt;/code&gt;,&amp;#160; which is probably &lt;a href=&quot;https://stackoverflow.com/questions/4511608/identical-if-and-if-yield-different-results&quot;&gt;not what the programmer intended&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;In both languages, the solution is to use an expression which is actually typed as &lt;code&gt;int?&lt;/code&gt;, by writing new &lt;code&gt;int?()&lt;/code&gt;, (in C#) or &lt;code&gt;New Integer?()&lt;/code&gt; (in VB.Net).&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-315267121528090830</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/animating-table-rows-with-jquery.html"/>
		<title type="text">Animating Table Rows with jQuery</title>
		<updated>2010-12-21T17:19:00+00:00</updated>
		<published>2010-12-21T17:24:01+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="jQuery" term="jquery" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		
		
		<category scheme="https://blog.slaks.net/#" label="animation" term="animation" />
		
		
		<category scheme="https://blog.slaks.net/#" label="HTML" term="html" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;jQuery contains a powerful and flexible animation engine.&amp;#160; However, it has some limitations, primarily due to underlying limitations of CSS-based layout&lt;/p&gt;  &lt;p&gt;For example, there is no simple way to &lt;a href=&quot;https://api.jquery.com/slideUp/&quot;&gt;&lt;code&gt;slideUp()&lt;/code&gt;&lt;/a&gt; a table row (&lt;code&gt;&amp;lt;tr&amp;gt;&lt;/code&gt; element).&amp;#160; The slideUp animation will animate the element’s height to zero.&amp;#160; However, a table row is always tall enough to show its elements, so the animation cannot actually shrink the element.&lt;/p&gt;  &lt;p&gt;To work around this, we can wrap the contents of each cell in a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element, then &lt;code&gt;slideUp()&lt;/code&gt; the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; elements.&amp;#160; Doing this in the HTML would create ugly and non-semantic markup, so we can do it in jQuery instead.&lt;/p&gt;  &lt;p&gt;For example: &lt;strong&gt;&lt;em&gt;&lt;a href=&quot;https://jsfiddle.net/SLaks/LjHBR/&quot;&gt;&lt;font size=&quot;3&quot;&gt;Demo&lt;/font&gt;&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;children&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;td, th&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;animate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wrapInner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;lt;div /&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;children&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slideUp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;closest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Get all of the cells in the row&lt;/li&gt;

  &lt;li&gt;Animate away any padding in the cells&lt;/li&gt;

  &lt;li&gt;Wrap all of the contents of each cell in one &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element for each cell (calling &lt;a href=&quot;https://api.jquery.com/wrapInner/&quot;&gt;wrapInner()&lt;/a&gt;)&lt;/li&gt;

  &lt;li&gt;Select the new &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; elements&lt;/li&gt;

  &lt;li&gt;Slide up the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;s, and remove the rows when finished.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you don’t remove the rows, their borders will still be visible.&amp;#160; Therefore, if you want the rows to stay after the animation, call &lt;code&gt;hide()&lt;/code&gt; instead of &lt;code&gt;remove()&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4479505221964117033</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/requiring-inherited-types-in-generic.html"/>
		<title type="text">Requiring Inherited Types in Generic Constraints</title>
		<updated>2010-12-17T17:08:00+00:00</updated>
		<published>2010-12-17T17:08:16+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="generics" term="generics" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;A generic class can specify that its generic parameter must inherit a type.&amp;#160; However, there is no obvious way in general to prevent clients from passing the base type itself.&lt;/p&gt;  &lt;p&gt;For example, take the following set of types:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Entity&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Boat&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Repository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TEntity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TEntity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This allows the type &lt;code&gt;Repository&amp;lt;Entity&amp;gt;&lt;/code&gt;, which doesn’t make logical sense.&lt;/p&gt;

&lt;p&gt;In this particular case, we could prevent that by changing the generic constraint to &lt;code&gt;where TEntity : Entity, new()&lt;/code&gt;.&amp;#160; Since the base Entity class is abstract, that would disallow a &lt;code&gt;Repository&amp;lt;Entity&amp;gt;&lt;/code&gt;.&amp;#160; However,if the concrete entities also&amp;#160; don’t have default constructors, this wouldn’t work.&amp;#160; Similarly, had the base type been an interface, we could add a &lt;code&gt;: class&lt;/code&gt; constraint.&lt;/p&gt;

&lt;p&gt;There is an (somewhat) ugly hack that can be used to prevent parameterizations of the base class in arbitrary cases (as long as you control every type involved):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Entity&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IConcreteEntity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;   &lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//Marker interface&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IConcreteEntity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Boat&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IConcreteEntity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IConcreteEntity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Repository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TEntity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; 
      &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TEntity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IConcreteEntity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Specifically, we can add a marker interface which is implemented by all concrete implementations of the base type.&amp;#160; We can then constrain a generic parameter to inherit both the base type and the marker interface.&amp;#160; Since the base class itself does implement the marker interface, it will not be valid as a parameter.&amp;#160; &lt;br /&gt;Note that the marker interface must be implemented (perhaps indirectly) by every single concrete implementation.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-2468876042420645645</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/nested-iterators-part-2.html"/>
		<title type="text">Nested Iterators, part 2</title>
		<updated>2010-12-16T21:50:00+00:00</updated>
		<published>2010-12-17T17:11:12+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="laziness" term="laziness" />
		
		
		<category scheme="https://blog.slaks.net/#" label="iterators" term="iterators" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;In &lt;a href=&quot;/2010/12/nested-iterators-part-1.html&quot;&gt;part 1&lt;/a&gt;, we discussed the simple approach to making a nested iterator.&amp;#160; However, we fell short of a completely lazy nested iterator.&lt;/p&gt;  &lt;p&gt;In simple cases, we can make an separate iterator method for the subsequence: &lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;FullyLazy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Inner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Inner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

Note that this is actually smaller than the single-method implementation! 

&lt;p&gt;This seems to work very well; the inner iterator code for a particular subsequence will not execute at all unless that subsequence is actually enumerated.&lt;/p&gt;

&lt;p&gt;However, this approach falls short in practice.&amp;#160; To see why, consider a real-world example. 
  &lt;br /&gt;Here is a fully lazy implementation of a Partition method, which converts a sequence into a 2D “jagged IEnumerable” where each subsequence (partition) contains &lt;em&gt;n&lt;/em&gt; elements.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isFinished&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PartitionInner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isFinished&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PartitionInner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;isFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MoveNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This method is complicated by the need to communicate back from the inner iterator to the outer one.&amp;#160; Since &lt;a href=&quot;https://blogs.msdn.com/b/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx&quot;&gt;iterators cannot have &lt;code&gt;ref&lt;/code&gt; parameters&lt;/a&gt;, I need to make a “box” class that holds a reference to an int.&amp;#160; This allows the outer iterator to find out when the sequence finishes.&lt;/p&gt;

&lt;p&gt;In addition, this implementation has a subtle bug: If the last partition is full, it will return an extra, empty, partition after it. 
  &lt;br /&gt;Fixing this issue would require that the outer method call MoveNext() before each call to the inner method.&amp;#160; This makes the code even more complicated; I won’t list it here (unless people really want me to)&lt;/p&gt;

&lt;p&gt;This design has a more fundamental problem: The behavior of the outer method is determined by the inner ones.&amp;#160; It will only work if each inner IEnumerable&amp;lt;T&amp;gt; is enumerated exactly once, before the next MoveNext() call to the outer iterator.&amp;#160; If, for example, you iterate each inner iterator twice, it will behave unexpectedly:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;Enumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;, &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

This code is intended to create strings that contain each partition twice.&amp;#160; It is supposed to return 

&lt;pre&gt;1, 2, 3, 1, 2, 3
4, 5, 6, 4, 5, 6
7, 8, 7, 8&lt;/pre&gt;

&lt;p&gt;However, it actually creates the strings&lt;/p&gt;

&lt;pre&gt;1, 2, 3, 4, 5, 6
7, 8&lt;/pre&gt;

&lt;p&gt;Enumerating the inner iterator a second time will end up consuming extra items from the original sequence.&lt;/p&gt;

&lt;p&gt;Thus, in order to produce enumerables that behave correctly, the inner enumerator &lt;em&gt;needs&lt;/em&gt; to cache its items; it is impossible to make a fully lazy Partition method.&lt;/p&gt;

&lt;p&gt;It gets worse.&amp;#160; In order to allow callers to write &lt;code&gt;thingy.Partition(5).Skip(2)&lt;/code&gt;, (which should skip the first two partitions) the &lt;em&gt;outer&lt;/em&gt; enumerator needs to cache all items, because it cannot assume that the inner iterators will be called at all.&amp;#160; &lt;/p&gt;

&lt;p&gt;Thus, the laziest possible Partition method must use the &lt;a title=&quot;Part 1&quot; href=&quot;/2010/12/nested-iterators-part-1.html&quot;&gt;original approach&lt;/a&gt;: &lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PartitionCorrect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It would be possible to write a slightly lazier version that combines these two approaches and passes a List&amp;lt;T&amp;gt; to the inner iterator, which would return whatever is in the list, then enumerate if necessary to fill the list. However, I’m too &lt;em&gt;lazy&lt;/em&gt; to do it.&amp;#160; (unless people really want me to)&lt;/p&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-6877596973447383503</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/nested-iterators-part-1.html"/>
		<title type="text">Nested Iterators, part 1</title>
		<updated>2010-12-16T16:26:00+00:00</updated>
		<published>2010-12-16T16:27:22+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="iterators" term="iterators" />
		
		
		<category scheme="https://blog.slaks.net/#" label="C#" term="csharp" />
		
		
		<category scheme="https://blog.slaks.net/#" label=".Net" term="dot-net" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;C# 2.0 introduced a powerful feature called an &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/dscyy5s0.aspx&quot;&gt;iterator&lt;/a&gt;, a method which returns an IEnumerable&amp;lt;T&amp;gt; or IEnumerator&amp;lt;T&amp;gt; using the new &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx&quot;&gt;yield keyword&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Using an iterator, you can quickly and easily create a method which returns lazily a sequence of values.&amp;#160; However, lazily returning a sequence of &lt;em&gt;sequences&lt;/em&gt; (IEnumerable&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;) is not so simple.&lt;/p&gt;  &lt;p&gt;The obvious approach is to yield return a List&amp;lt;T&amp;gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SemiLazy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p align=&quot;left&quot;&gt;(This can be shortened to a single LINQ statement, but that’s beyond the point of this post: &lt;code&gt;Enumerable.Range(0, 10).Select(i =&amp;gt; Enumerable.Range(10 * i, 10))&lt;/code&gt; ) &lt;/p&gt;

&lt;p&gt;This approach is very simple, but isn’t very lazy; each subsequence will be computed in its entirety, whether it’s consumed or not.&lt;/p&gt;

&lt;p&gt;This approach also has a subtle catch: the iterator must return a different List&amp;lt;T&amp;gt; instance every time.&lt;/p&gt;

&lt;p&gt;If you “optimize” it to re-use the instance, you’ll break callers which don’t use the subsequences immediately:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Wrong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Calling &lt;code&gt;SemiLazy().ToArray()[0].First()&lt;/code&gt; will return 0 (the first element in the first subsequence); calling &lt;code&gt;Wrong().ToArray()[0].First()&lt;/code&gt; will return 90 (since all subsequences refer to the same instance). &lt;/p&gt;

&lt;p&gt;Next: How can we achieve full laziness?&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-5729657627769682586</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/on-copy-prevention-in-html-part-3.html"/>
		<title type="text">On copy prevention in HTML, part 3</title>
		<updated>2007-04-27T20:56:00+00:00</updated>
		<published>2007-04-27T20:56:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="HTML" term="html" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;My &lt;a href=&quot;/2010/12/on-copy-prevention-in-html-part-2.html&quot;&gt;previous&lt;/a&gt; post stretched the limit of simple copy prevention.  Beyond this point, it gets very complicated.  Before continuing, some thought is in order.  Who are you trying to prevent from copying your text?  Why shouldn't the text be copied?  Unless you are trying to stop a hardcore developer, the previous methods should suffice.  Also, what kind of copying are you trying to prevent?  If you are trying to prevent the copier from copying into a web page, it is significantly harder, because he can copy your source and it will display normally.&lt;/p&gt;
&lt;p&gt;I can think of two ways to prevent the copier from using a screenreader to copy your text.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Put all of the text into a single, &lt;a href=&quot;https://en.wikipedia.org/wiki/CAPTCHA&quot;&gt;CAPTCHA&lt;/a&gt;-like image.  This way, the screenreader will not be able to read the text.  However, this will also make it more difficult for legitimate people to read your text.  Also, the copier could simply insert the large image as-is into his document.  This risk could be mitigated by watermarking it with your name.&lt;/li&gt;
&lt;li&gt;Break apart the text into many different images, each one somewhat smaller than a letter.  This could be done in server-side code.  Then, use a JavaScript timer to alternate the images so that the screenreader will never see all of the text at once.  To prevent the copier from modifying the JavaScript to show all of the images, alternate them with other images.  For example, write a server-side script that takes X and Y coordinates, and a timer index.  This script would either a white image, or the chunk of text at the given coordinates, depending on the timer index.  To prevent the copier from using GDI to OR-blit screenshots from different times, (or from using Photoshop to make the white transparent, then pasting them together) the white could have random black patterns.  However, this will make the text hard to read.  If a JavaScript timer isn't fast enough to make the text legible, it could be done in Flash.&lt;/li&gt;
&lt;/ol&gt;
Preventing the copier from copying your content into HTML is more difficult.  No matter how obscure your source is, the copier could use a tool like &lt;a href=&quot;https://getfirebug.com/&quot;&gt;Firebug&lt;/a&gt; to copy your DOM source using the innerHTML property.  Here too, there are several options.
&lt;ol&gt;
&lt;li&gt;Use my Scrambler (see part 2), and put your name anywhere within the scrambled text.  (for example, you could put in, in the middle, Written by &lt;i&gt;Your Name Here&lt;/i&gt;; do not copy)  It is virtually impossible for the copier to extricate the spans that form your name and then fill the resulting gap.  This could also be done in an image.  However,  if you put your name at the end, the copier could position a white DIV to hide it. Or, if his name is similar in length, he could position a white DIV with his name to hide it.&lt;/li&gt;
&lt;li&gt; If you are only worried about part of your text, scramble the entire page.  It would be extremely difficult for the copier to extract the sensitive part.  For example, you could add a lengthy copyright header before the content, and scramble it with the content.  However, the copier could position the containing DIV so that the copyright header is above the top of the DIV. &lt;/li&gt;
&lt;li&gt;Break the text into a large set of images, and use  client-side JavaScript to execute a server-generated script that adds these images from another server-side script.   For every request, require a single-use authorization token returned by the previous request.  The initial page request would include the auth-token for the first script, and each image would be preceded by an AJAX request for auth-tokens for the image and for the next AJAX request.  The first script would have an auth-token for the first AJAX request embedded within it.
To make it more difficult for an attacker to get an auth-token from the initial script, you could encode the script before sending it, and decode on the client, then pass it to the eval() function.  All responses would have the no-cache header to prevent the copier from taking the images out of the cache, and the images would be used by the background-image attribute on a DIV to prevent Save Image As (only necessary if Save Image As doesn't redownload the image from the server).  The server would track auth-tokens in a database, and delete them when used.
If you do all this, the only way the copier could get the images from the page would be Print Screen.  To prevent that, make the images flicker as described earlier.  The copier could, however, load your page with JavaScript disabled, download and decode the script, convert it to a full programming language (eg, C#), and use the script's embedded auth-token to send the &quot;AJAX&quot; requests over HTTP (for example, using .NET's HttpWebRequest class) and download the images.  To prevent this, make the auth-tokens expire after about 30 seconds.  If any auth-token is expired, all subsequent images should form something different. (maybe Service Unavailable, or random black pixels, or a different text) By the time the copier finishes writing his program, his &quot;stolen&quot; auth-token will have expired, and he will not know what he did wrong.  To prevent him from trying again, you could blacklist his IP address after receiving an expired request, and embed IP address in auth-tokens.   You could also set and require some innocuous-seeing cookies on the server for every request.  The copier wouldn't notice these cookies, and when he requests the images without these cookies in his request, you could send him whatever you want.  Please note that if your page requires a login, the copier probably will check cookies.  And, if the copier is being paid by the hour (this is quite likely; otherwise, he'd give up), he might even thank you for doing all this.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-6341061042378383197</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/on-copy-prevention-in-html-part-2.html"/>
		<title type="text">On copy prevention in HTML, part 2</title>
		<updated>2007-04-16T21:49:00+00:00</updated>
		<published>2007-04-16T21:49:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="HTML" term="html" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;The methods discussed in my &lt;a href=&quot;/2010/12/on-copy-prevention-in-html-part-1.html&quot;&gt;previous&lt;/a&gt; post are crude and ugly.  Most of the time, they do work, but they do nothing to prevent the user from viewing the source and copying the text from there.  Also, the user has a right to select text that should not be denied.  For example, if one wants to show someone part of a large document, the easiest way to do that is to select the part.&lt;/p&gt;
&lt;textarea style=&quot;float: left; height: 2em; width: 200px;&quot;&gt;Paste here.&lt;/textarea&gt;
&lt;div&gt;
&lt;span style=&quot;left: -9477px; position: absolute;&quot;&gt;Z&lt;/span&gt;&lt;span style=&quot;left: -9765px; position: absolute;&quot;&gt;S&lt;/span&gt;&lt;span style=&quot;left: -9586px; position: absolute;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: -9373px; position: static;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;left: -9734px; position: absolute;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: -9872px; position: absolute;&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;left: -9773px; position: absolute;&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;left: -9326px; position: absolute;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;left: -9195px; position: static;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: -9413px; position: absolute;&quot;&gt;L&lt;/span&gt;&lt;span style=&quot;left: -9196px; position: absolute;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9737px; position: absolute;&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;left: -9897px; position: static;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: -9014px; position: absolute;&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;left: -9893px; position: absolute;&quot;&gt;W&lt;/span&gt;&lt;span style=&quot;left: -9103px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9223px; position: absolute;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;left: -9184px; position: absolute;&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;left: -9122px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9626px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9741px; position: absolute;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;left: -9464px; position: absolute;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;left: -9705px; position: absolute;&quot;&gt;O&lt;/span&gt;&lt;span style=&quot;left: -9863px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9440px; position: absolute;&quot;&gt;M&lt;/span&gt;&lt;span style=&quot;left: -9269px; position: absolute;&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;left: -9032px; position: absolute;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9369px; position: static;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9783px; position: absolute;&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;left: -9392px; position: absolute;&quot;&gt;R&lt;/span&gt;&lt;span style=&quot;left: -9862px; position: absolute;&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;left: -9230px; position: absolute;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9818px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9548px; position: static;&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;left: -9325px; position: static;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: -9586px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9129px; position: absolute;&quot;&gt;z&lt;/span&gt;&lt;span style=&quot;left: -9492px; position: absolute;&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;left: -9673px; position: absolute;&quot;&gt;Y&lt;/span&gt;&lt;span style=&quot;left: -9451px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9242px; position: absolute;&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;left: -9143px; position: absolute;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9821px; position: static;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: -9051px; position: static;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9043px; position: absolute;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;left: -9384px; position: absolute;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;left: -9140px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9449px; position: absolute;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: -9806px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9758px; position: absolute;&quot;&gt;L&lt;/span&gt;&lt;span style=&quot;left: -9077px; position: absolute;&quot;&gt;L&lt;/span&gt;&lt;span style=&quot;left: -9850px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9365px; position: absolute;&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;left: -9737px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9424px; position: static;&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;left: -9597px; position: absolute;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9372px; position: absolute;&quot;&gt;w&lt;/span&gt;&lt;span style=&quot;left: -9094px; position: absolute;&quot;&gt;S&lt;/span&gt;&lt;span style=&quot;left: -9644px; position: absolute;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;left: -9991px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9139px; position: absolute;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;left: -9982px; position: absolute;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9261px; position: absolute;&quot;&gt;I&lt;/span&gt;&lt;span style=&quot;left: -9584px; position: absolute;&quot;&gt;g&lt;/span&gt;&lt;span style=&quot;left: -9943px; position: static;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;left: -9299px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9533px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9184px; position: static;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;left: -9195px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9678px; position: absolute;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: -9861px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9953px; position: static;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9351px; position: static;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9594px; position: absolute;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;left: -9234px; position: absolute;&quot;&gt;M&lt;/span&gt;&lt;span style=&quot;left: -9227px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9710px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9026px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9862px; position: static;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: -9248px; position: absolute;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9591px; position: absolute;&quot;&gt;M&lt;/span&gt;&lt;span style=&quot;left: -9823px; position: static;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9215px; position: absolute;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;left: -9132px; position: absolute;&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;left: -9047px; position: absolute;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9660px; position: absolute;&quot;&gt;Z&lt;/span&gt;&lt;span style=&quot;left: -9045px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9510px; position: static;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;left: -9809px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9126px; position: static;&quot;&gt;w&lt;/span&gt;&lt;span style=&quot;left: -9062px; position: static;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: -9625px; position: absolute;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;left: -9729px; position: absolute;&quot;&gt;Z&lt;/span&gt;&lt;span style=&quot;left: -9561px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9183px; position: static;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9584px; position: absolute;&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;left: -9970px; position: absolute;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;left: -9557px; position: absolute;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;left: -9932px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9366px; position: absolute;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: -9791px; position: absolute;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;left: -9699px; position: absolute;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9935px; position: static;&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;left: -9255px; position: absolute;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;left: -9520px; position: absolute;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9784px; position: absolute;&quot;&gt;I&lt;/span&gt;&lt;span style=&quot;left: -9834px; position: absolute;&quot;&gt;P&lt;/span&gt;&lt;span style=&quot;left: -9550px; position: static;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9919px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9169px; position: absolute;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;left: -9226px; position: absolute;&quot;&gt;Z&lt;/span&gt;&lt;span style=&quot;left: -9550px; position: absolute;&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;left: -9510px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9419px; position: absolute;&quot;&gt;g&lt;/span&gt;&lt;span style=&quot;left: -9478px; position: absolute;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;left: -9420px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9012px; position: absolute;&quot;&gt;Y&lt;/span&gt;&lt;span style=&quot;left: -9343px; position: absolute;&quot;&gt;W&lt;/span&gt;&lt;span style=&quot;left: -9850px; position: absolute;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;left: -9429px; position: static;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;left: -9179px; position: static;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;left: -9253px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9187px; position: static;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9912px; position: absolute;&quot;&gt;S&lt;/span&gt;&lt;span style=&quot;left: -9872px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9475px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9774px; position: absolute;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;left: -9242px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9859px; position: absolute;&quot;&gt;M&lt;/span&gt;&lt;span style=&quot;left: -9614px; position: absolute;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;left: -9073px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9850px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9494px; position: static;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: -9018px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9967px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9938px; position: absolute;&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;left: -9667px; position: absolute;&quot;&gt;P&lt;/span&gt;&lt;span style=&quot;left: -9007px; position: absolute;&quot;&gt;z&lt;/span&gt;&lt;span style=&quot;left: -9092px; position: absolute;&quot;&gt;R&lt;/span&gt;&lt;span style=&quot;left: -9952px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9163px; position: absolute;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: -9586px; position: absolute;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9294px; position: absolute;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9143px; position: absolute;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;left: -9262px; position: static;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9401px; position: absolute;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;left: -9652px; position: static;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: -9255px; position: absolute;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9781px; position: absolute;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: -9616px; position: absolute;&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;left: -9403px; position: absolute;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;left: -9691px; position: absolute;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: -9308px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9432px; position: absolute;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9121px; position: absolute;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;left: -9041px; position: absolute;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;left: -9349px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9352px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9571px; position: absolute;&quot;&gt;I&lt;/span&gt;&lt;span style=&quot;left: -9952px; position: absolute;&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;left: -9049px; position: absolute;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9397px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9903px; position: absolute;&quot;&gt;M&lt;/span&gt;&lt;span style=&quot;left: -9572px; position: absolute;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;left: -9053px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9880px; position: absolute;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;left: -9728px; position: static;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9264px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9246px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9748px; position: static;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9557px; position: absolute;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: -9920px; position: absolute;&quot;&gt;S&lt;/span&gt;&lt;span style=&quot;left: -9258px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9142px; position: absolute;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: -9942px; position: absolute;&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;left: -9111px; position: static;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9521px; position: absolute;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;left: -9989px; position: absolute;&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;left: -9033px; position: absolute;&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;left: -9021px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9557px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9044px; position: static;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9628px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9922px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9236px; position: absolute;&quot;&gt;g&lt;/span&gt;&lt;span style=&quot;left: -9046px; position: absolute;&quot;&gt;X&lt;/span&gt;&lt;span style=&quot;left: -9182px; position: static;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;left: -9590px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9885px; position: absolute;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9510px; position: absolute;&quot;&gt;U&lt;/span&gt;&lt;span style=&quot;left: -9325px; position: absolute;&quot;&gt;H&lt;/span&gt;&lt;span style=&quot;left: -9969px; position: static;&quot;&gt;H&lt;/span&gt;&lt;span style=&quot;left: -9318px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9073px; position: absolute;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;left: -9199px; position: static;&quot;&gt;w&lt;/span&gt;&lt;span style=&quot;left: -9001px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9603px; position: absolute;&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;left: -9615px; position: absolute;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;left: -9948px; position: absolute;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9107px; position: static;&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;left: -9654px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9641px; position: absolute;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9216px; position: absolute;&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;left: -9267px; position: absolute;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;left: -9791px; position: static;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: -9396px; position: absolute;&quot;&gt;U&lt;/span&gt;&lt;span style=&quot;left: -9887px; position: static;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;left: -9263px; position: absolute;&quot;&gt;P&lt;/span&gt;&lt;span style=&quot;left: -9677px; position: absolute;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;left: -9174px; position: absolute;&quot;&gt;Z&lt;/span&gt;&lt;span style=&quot;left: -9777px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9734px; position: absolute;&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;left: -9716px; position: absolute;&quot;&gt;M&lt;/span&gt;&lt;span style=&quot;left: -9492px; position: static;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9240px; position: absolute;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;left: -9007px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9832px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9378px; position: absolute;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: -9349px; position: absolute;&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;left: -9683px; position: static;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9228px; position: static;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9707px; position: static;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9254px; position: absolute;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: -9637px; position: absolute;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;left: -9648px; position: absolute;&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;left: -9472px; position: absolute;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: -9625px; position: absolute;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9798px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9291px; position: absolute;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9212px; position: absolute;&quot;&gt;H&lt;/span&gt;&lt;span style=&quot;left: -9520px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9329px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9521px; position: static;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9775px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9964px; position: absolute;&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;left: -9068px; position: absolute;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;left: -9757px; position: absolute;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: -9075px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9477px; position: absolute;&quot;&gt;O&lt;/span&gt;&lt;span style=&quot;left: -9673px; position: absolute;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;left: -9864px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9887px; position: static;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;left: -9446px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9892px; position: absolute;&quot;&gt;R&lt;/span&gt;&lt;span style=&quot;left: -9653px; position: absolute;&quot;&gt;P&lt;/span&gt;&lt;span style=&quot;left: -9836px; position: absolute;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: -9961px; position: absolute;&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;left: -9332px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9097px; position: absolute;&quot;&gt;S&lt;/span&gt;&lt;span style=&quot;left: -9409px; position: absolute;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;left: -9854px; position: absolute;&quot;&gt;w&lt;/span&gt;&lt;span style=&quot;left: -9078px; position: absolute;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9378px; position: absolute;&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;left: -9766px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9857px; position: absolute;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: -9438px; position: absolute;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;left: -9797px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9057px; position: absolute;&quot;&gt;P&lt;/span&gt;&lt;span style=&quot;left: -9201px; position: absolute;&quot;&gt;X&lt;/span&gt;&lt;span style=&quot;left: -9245px; position: absolute;&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;left: -9566px; position: absolute;&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;left: -9973px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9677px; position: absolute;&quot;&gt;R&lt;/span&gt;&lt;span style=&quot;left: -9675px; position: absolute;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9867px; position: absolute;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;left: -9501px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9799px; position: static;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9069px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9743px; position: absolute;&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;left: -9210px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9990px; position: absolute;&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;left: -9892px; position: absolute;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;left: -9495px; position: absolute;&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;left: -9301px; position: static;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;left: -9564px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9035px; position: absolute;&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;left: -9443px; position: absolute;&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;left: -9709px; position: absolute;&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;left: -9575px; position: static;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: -9412px; position: absolute;&quot;&gt;X&lt;/span&gt;&lt;span style=&quot;left: -9907px; position: absolute;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9865px; position: absolute;&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;left: -9752px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9212px; position: absolute;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;left: -9880px; position: absolute;&quot;&gt;Y&lt;/span&gt;&lt;span style=&quot;left: -9580px; position: static;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: -9283px; position: absolute;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;left: -9522px; position: absolute;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9687px; position: absolute;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: -9179px; position: static;&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;left: -9549px; position: static;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9630px; position: absolute;&quot;&gt;K&lt;/span&gt;&lt;span style=&quot;left: -9391px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9626px; position: static;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: -9113px; position: static;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: -9495px; position: static;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;left: -9226px; position: absolute;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;left: -9968px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9064px; position: absolute;&quot;&gt;I&lt;/span&gt;&lt;span style=&quot;left: -9865px; position: absolute;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9428px; position: static;&quot;&gt;L&lt;/span&gt;&lt;span style=&quot;left: -9329px; position: absolute;&quot;&gt;w&lt;/span&gt;&lt;span style=&quot;left: -9879px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9217px; position: absolute;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: -9155px; position: absolute;&quot;&gt;z&lt;/span&gt;&lt;span style=&quot;left: -9173px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9019px; position: absolute;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;left: -9257px; position: static;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: -9703px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9282px; position: static;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: -9376px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9043px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9554px; position: static;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: -9896px; position: static;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: -9444px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9253px; position: absolute;&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;left: -9929px; position: absolute;&quot;&gt;w&lt;/span&gt;&lt;span style=&quot;left: -9688px; position: static;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;left: -9050px; position: absolute;&quot;&gt;J&lt;/span&gt;&lt;span style=&quot;left: -9342px; position: absolute;&quot;&gt;X&lt;/span&gt;&lt;span style=&quot;left: -9302px; position: static;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: -9976px; position: absolute;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;left: -9635px; position: static;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: -9937px; position: absolute;&quot;&gt;I&lt;/span&gt;&lt;span style=&quot;left: -9664px; position: absolute;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: -9083px; position: static;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: -9403px; position: absolute;&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;left: -9299px; position: absolute;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: -9707px; position: absolute;&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;left: -9649px; position: static;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: -9524px; position: static;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: -9962px; position: static;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: -9430px; position: static;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush: html&quot; style=&quot;clear: both;&quot;&gt;&amp;lt;SPAN style=&quot;position: static;left:-9477px;position: absolute;&quot;&amp;gt;Z&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9765px;position: absolute;&quot;&amp;gt;S&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9586px;position: absolute;&quot;&amp;gt;k&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9373px;position: absolutee;&quot;&amp;gt;T&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9734px;position: absolute;&quot;&amp;gt;u&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9872px;position: absolute;&quot;&amp;gt;K&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9773px;position: absolute;&quot;&amp;gt;p&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9326px;position: absolute;&quot;&amp;gt;B&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9195px;position: absolutee;&quot;&amp;gt;r&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9413px;position: absolute;&quot;&amp;gt;L&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9196px;position: absolute;&quot;&amp;gt;l&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9737px;position: absolute;&quot;&amp;gt;j&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9897px;position: absolutee;&quot;&amp;gt;y&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9014px;position: absolute;&quot;&amp;gt;V&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9893px;position: absolute;&quot;&amp;gt;W&amp;lt;/SPAN&amp;gt;
&amp;lt;SPAN style=&quot;position: static;left:-9103px;position: absolutee;&quot;&amp;gt; &amp;lt;/SPAN&amp;gt;
...
&lt;/pre&gt;
&lt;div style=&quot;position: relative; height:2em&quot;&gt;


&lt;span style=&quot;left: 241px; position: absolute; top: 0px;&quot;&gt;g&lt;/span&gt;&lt;span style=&quot;left: 263px; position: absolute; top: 0px;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: 117px; position: absolute; top: 0px;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: 17px; position: absolute; top: 20px;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: 276px; position: absolute; top: 0px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 287px; position: absolute; top: 0px;&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;left: 129px; position: absolute; top: 0px;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: 9px; position: absolute; top: 20px;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: 13px; position: absolute; top: 0px;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: 280px; position: absolute; top: 0px;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: 53px; position: absolute; top: 0px;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: 201px; position: absolute; top: 0px;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: 109px; position: absolute; top: 20px;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;left: 224px; position: absolute; top: 0px;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: 158px; position: absolute; top: 0px;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: 43px; position: absolute; top: 0px;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: 208px; position: absolute; top: 0px;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: 111px; position: absolute; top: 0px;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: 185px; position: absolute; top: 0px;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: 25px; position: absolute; top: 20px;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: 173px; position: absolute; top: 0px;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;left: 136px; position: absolute; top: 0px;&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;left: 83px; position: absolute; top: 20px;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: 72px; position: absolute; top: 0px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 37px; position: absolute; top: 20px;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: 124px; position: absolute; top: 0px;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: 190px; position: absolute; top: 0px;&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;left: 59px; position: absolute; top: 20px;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: 155px; position: absolute; top: 0px;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;left: 294px; position: absolute; top: 0px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 69px; position: absolute; top: 20px;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: 298px; position: absolute; top: 0px;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;left: 55px; position: absolute; top: 20px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 266px; position: absolute; top: 0px;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: 98px; position: absolute; top: 0px;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: 39px; position: absolute; top: 0px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 83px; position: absolute; top: 0px;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;left: 252px; position: absolute; top: 0px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 60px; position: absolute; top: 0px;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;left: 50px; position: absolute; top: 0px;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: 52px; position: absolute; top: 20px;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: 101px; position: absolute; top: 0px;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;left: 28px; position: absolute; top: 0px;&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;left: 75px; position: absolute; top: 20px;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: 216px; position: absolute; top: 0px;&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;left: 181px; position: absolute; top: 0px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 95px; position: absolute; top: 20px;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;left: 90px; position: absolute; top: 20px;&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;left: 102px; position: absolute; top: 20px;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;left: 147px; position: absolute; top: 0px;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;left: 76px; position: absolute; top: 0px;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: 90px; position: absolute; top: 0px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 44px; position: absolute; top: 20px;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;left: 234px; position: absolute; top: 0px;&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;left: 231px; position: absolute; top: 0px;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;left: 256px; position: absolute; top: 0px;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;left: 165px; position: absolute; top: 0px;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;left: 20px; position: absolute; top: 0px;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;left: 0px; position: absolute; top: 20px;&quot;&gt;L&lt;/span&gt;&lt;span style=&quot;left: 5px; position: absolute; top: 0px;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;left: 0px; position: absolute; top: 0px;&quot;&gt;I&lt;/span&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 241px; top: 0px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;g&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 263px; top: 0px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;i&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 117px; top: 0px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;c&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 17px; top: 20px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;o&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 276px; top: 0px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;t&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 287px; top: 0px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;x&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 129px; top: 0px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;a&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 9px; top: 20px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;o&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position: absolute; left: 13px; top: 0px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;y&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
...&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

These texts were generated by a script (currently unavailable).  It can do two things: Inflate (the first paragraph above) and Scramble (the second paragraph).  &lt;!-- Each one should work on &lt;i&gt;any&lt;/i&gt; snippet of HTML, including forms and JavaScript. --&gt; &lt;/p&gt;&lt;p&gt;
Using the Inflate option will add random characters in SPANs positioned absolutely between nine and ten thousand pixels to the left.  When the user selects text, the random characters will also be selected, and when the text is copied, they will also be copied.  It will randomly add up to five letters between every two characters, and it will ignore text in TEXTAREAs, SCRIPTs, and SELECTs.
Such text could be &quot;deflated&quot; by copying the source, then removing all text that matches the &lt;a href=&quot;https://www.regular-expressions.info/&quot;&gt;regular expression&lt;/a&gt; 
&lt;code&gt;/&amp;lt;span[a-z0-9 ;:=&quot;']*?&amp;gt;[A-Za-z]&amp;lt;/span&amp;gt;/&lt;/code&gt;
, removing all SPAN tags that contain a letter and an attribute.  Therefore, I put each original letter in a very similar SPAN tag, complete with a random location, and gave both types of spans position:absolute and position:static, in different orders.  This could also be matched by a regular expression, but it would be much more complicated, and I will not list it here.  It would also be possible to write a GreaseMonkey script that would loop through the SPANs and delete all of them which has a position attribute equal to absolute.  However, it would probably be easier to retype it manually.&lt;/p&gt;&lt;p&gt;
Using the Scramble option will put each letter in to a SPAN, position them absolutely at their correct location, and  randomize the order.  Therefore, when the user selects the text, the selection will be scrambled, and when it is pasted, it will show up as nonsense.  Spaces are rendered pointless by the procedure, and are therefore removed.  Scrambled text will not select cleanly, but is nearly impossible to descramble.   It would be possible to write a GreaseMonkey script that would sort the SPANs by top, then by left, but it would be &lt;i&gt;much&lt;/i&gt; easier to retype the text manually.&lt;/p&gt;&lt;p&gt;
When using this approach, remember to position the text's container, or it will show up in unexpected places.  In addition, this approach will completely break word wrap,and must therefore be placed in a container with a fixed width.  This width should be entered in the Width textbox in the scrambler so that the text will flow correctly.&lt;/p&gt;&lt;p&gt;
These methods will definitely prevent all but the most determined and technically skilled copiers.  However, they do not prevent OCR screenreaders.  This will be discussed in part 3.&lt;/p&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-1778488699333085844</id>
		<link rel="alternate" type="text/html" href="https://blog.slaks.net/2010/12/on-copy-prevention-in-html-part-1.html"/>
		<title type="text">On copy prevention in HTML, part 1</title>
		<updated>2007-04-08T18:27:00+00:00</updated>
		<published>2007-04-08T18:27:00+00:00</published>

		
		
		<category scheme="https://blog.slaks.net/#" label="HTML" term="html" />
		
		
		<category scheme="https://blog.slaks.net/#" label="Javascript" term="javascript" />
		

		<author>
			<name>Schabse Laks</name>
			<uri>http://slaks.net</uri>
			<email>Dev@SLaks.Net</email>
		</author>
		<content type="html">
&lt;div class=&quot;css-full-post-content js-full-post-content&quot;&gt;
&lt;p&gt;Many web developers like to prevent their viewers from copying their text. While I do not approve of this, there are cases where it is appropriate.&lt;/p&gt;  &lt;p&gt;The simplest way to achieve this is to use the IE only attribute UNSELECTABLE and the FireFox only css style -moz-user-select. Such HTML looks like this:&lt;/p&gt;  &lt;table&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;DIV&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;unselectable=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;on&quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-moz-user-select:none;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
You can't select me.
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/DIV&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/td&gt;

      &lt;td&gt;
        &lt;div style=&quot;-moz-user-select: none&quot; unselectable=&quot;on&quot;&gt;You can't select me.&lt;/div&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;To make the HTML and CSS validate, one could do this in Javascript: &lt;code class=&quot;prettyprint&quot;&gt;&lt;i&gt;Elem&lt;/i&gt;.unselectable = &amp;quot;on&amp;quot;; &lt;i&gt;Elem&lt;/i&gt;.style.MozUserSelect = &amp;quot;none&amp;quot;;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, this method only works in IE and Firefox. In addition, in IE, it doesn't work very well, and if a user tries hard, he will end up selecting the text. &lt;/p&gt;

&lt;p&gt;A slightly better way to do it is to handle the onselectstart event (for IE) and the onmousedown event (for everything else) and return false. This will prevent the browser from handling the events. This results in something like this: &lt;/p&gt;

&lt;table&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;DIV&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;onselectstart=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;return false;&quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;onmousedown=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;return false;&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
You can't select me.
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/DIV&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/td&gt;

      &lt;td&gt;
        &lt;div onselectstart=&quot;return false;&quot; onmousedown=&quot;return false;&quot;&gt;You can't select me.&lt;/div&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;The problem with these methods is that they do nothing to prevent a user from reading the HTML source. This is discussed in the &lt;a href=&quot;/2010/12/on-copy-prevention-in-html-part-2.html&quot;&gt;next&lt;/a&gt; part.&lt;/p&gt;  
&lt;/div&gt;
</content>
	</entry>
	

</feed>