<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom"
	  xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
	  xmlns:blogger="http://schemas.google.com/blogger/2008"
	  xmlns:georss="http://www.georss.org/georss"
	  xmlns:gd="http://schemas.google.com/g/2005"
	  xmlns:thr="http://purl.org/syndication/thread/1.0">

	<title>SLaks.Blog</title>
	<link rel="alternate"	type="text/html"			href="//"/>
	<link rel="self"		type="application/atom+xml"	href="//feeds/posts/default.xml"/>
	<updated>Tue, 11 Jun 2013 20:26:14 -0700</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>_posts/2013-06-11-readonly-vs-immutable.md</id>
		<link rel="alternate" type="text/html" href="//2013-06-11/readonly-vs-immutable"/>
		<title type="text">Immutability, part 1: Read-only vs. Immutable</title>
		<updated>Tue, 11 Jun 2013 00:00:00 -0700</updated>
		<published>Tue, 11 Jun 2013 00:00:00 -0700</published>

		

		<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&gt;ReadOnlyCollection&amp;lt;T&amp;gt;&lt;/code&gt; (returned by &lt;code&gt;AsReadOnly()&lt;/code&gt;) is a good example).  However, &lt;code&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&gt;list.Add(new Test())&lt;/code&gt;, your read-only collection (which just wraps &lt;code&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&amp;#39;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&amp;#39;t count).  &lt;code&gt;string&lt;/code&gt; is an excellent example of an immutable class; the value of an existing &lt;code&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;http://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 &amp;ldquo;unchangeable&amp;rdquo; things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;readonly&lt;/code&gt; (&lt;code&gt;ReadOnly&lt;/code&gt; in VB.Net; &lt;code&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;http://msdn.microsoft.com/en-us/library/ms132474.aspx&quot;&gt;&lt;code&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&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&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&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;em&gt;Next time: Creating a simple immutable stack&lt;/em&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<id>_posts/2013-06-10-jekyll-endraw-in-code.md</id>
		<link rel="alternate" type="text/html" href="//2013-06-10/jekyll-endraw-in-code"/>
		<title type="text">Writing the endraw tag in Jekyll code blocks</title>
		<updated>Mon, 10 Jun 2013 00:00:00 -0700</updated>
		<published>Mon, 10 Jun 2013 00:00:00 -0700</published>

		

		<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&gt;{% raw %}&lt;/code&gt; block.  &lt;/p&gt;

&lt;p&gt;These techniques cannot be used in syntax-highlighted code blocks (Jekyll&amp;#39;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;jekyll&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text language-text&quot; data-lang=&quot;text&quot;&gt;{% raw %}
Liquid uses tags like {% if %} or {% for %}.
It also supports variable interpolation: {{ someVariable }}
{% endraw %}
&lt;/code&gt;&lt;/pre&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&amp;#39;t put that in a &lt;code&gt;&amp;#123;% raw %}&lt;/code&gt; block, because it will close the block.  You can&amp;#39;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;jekyll&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text language-text&quot; data-lang=&quot;text&quot;&gt;{% raw %}
This is how you show the termination of the `{% raw %}` tag inside itself: 
{% {% endraw %}endraw %}{% raw %}
&lt;/code&gt;&lt;/pre&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&amp;#39;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&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&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&gt;{%&lt;/code&gt; before the &lt;code&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&gt;{%&lt;/code&gt; text outside the &lt;code&gt;{% raw %}&lt;/code&gt; block.  However, Liquid does not have any way to escape a &lt;code&gt;{&lt;/code&gt;, and we can&amp;#39;t use HTML escaping inside the highlighted code block, so there is no obvious way to write the &lt;code&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&gt;{%&lt;/code&gt;, then interpolate this variable outside the &lt;code&gt;{% raw %}&lt;/code&gt; block.
For example:&lt;/p&gt;

&lt;div class=&quot;jekyll&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text language-text&quot; data-lang=&quot;text&quot;&gt;{% assign openTag = &amp;#39;{%&amp;#39; %}
{% raw %}
This is how you show the termination of the `{% raw %}` tag inside itself: 
{% endraw %}{{ openTag }} endraw %}{% raw %}
This content is back inside the {% raw %} block
{% endraw %}
&lt;/code&gt;&lt;/pre&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&gt;{% raw %}&lt;/code&gt; block with &lt;code&gt;{% endraw %}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Write the value of the &lt;code&gt;openTag&lt;/code&gt; variable (&lt;code&gt;{%&lt;/code&gt;) with &lt;code&gt;{{ openTag }}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Write the remainder of the &lt;code&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&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 worksarounds seem completicated, 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>_posts/2013-06-09-writing-about-jekyll-in-jekyll.md</id>
		<link rel="alternate" type="text/html" href="//2013-06-09/writing-about-jekyll-in-jekyll"/>
		<title type="text">Writing about Jekyll in Jekyll</title>
		<updated>Sun, 09 Jun 2013 00:00:00 -0700</updated>
		<published>Sun, 09 Jun 2013 00:00:00 -0700</published>

		

		<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;http://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&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;jekyll&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text language-text&quot; data-lang=&quot;text&quot;&gt;Liquid includes a tag specifically designed to solve this problem: 
the `{% raw %}{% raw %}{% endraw %}` tag.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I wrap the text I&amp;#39;m trying to produce (&lt;code&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&amp;#39;t recognize it as a tag: &lt;code&gt;&amp;amp;#123;% tag %}&lt;/code&gt;.  The browser will display the HTML entity as a regular &lt;code&gt;{&lt;/code&gt;, but Liquid won&amp;#39;t recognize it.  However, this does mean that we can&amp;#39;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&gt;&amp;amp;#123;&lt;/code&gt;.&lt;br&gt;
Thus, the final approach is &lt;code&gt;&amp;lt;code&amp;gt;&amp;amp;#123;% tag %}&amp;lt;/code&amp;gt;&lt;/code&gt;.  Although this isn&amp;#39;t much shorter than the Liquid raw tag, I find it more readable, since it doesn&amp;#39;t nest Liquid content within Liquid commands.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;#&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>_posts/2013-06-02-migrating-syntax-highlighting-to-jekyll.md</id>
		<link rel="alternate" type="text/html" href="//2013-06-02/migrating-syntax-highlighting-to-jekyll"/>
		<title type="text">Migrating client-side syntax highlighting to Jekyll</title>
		<updated>Sun, 02 Jun 2013 00:00:00 -0700</updated>
		<published>Sun, 02 Jun 2013 00:00:00 -0700</published>

		

		<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&amp;#39;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&gt;&amp;lt;pre class=&amp;quot;brush: someLanguage&amp;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;http://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;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp language-csharp&quot; data-lang=&quot;csharp&quot;&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;@&amp;quot;.../_posts&amp;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;&amp;quot;vb&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;vb.net&amp;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;n&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;n&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;@&amp;quot;\s*\&amp;lt;pre\s*class=&amp;quot;&amp;quot;brush:\s*(\w+);?\s*&amp;quot;&amp;quot;&amp;gt;\n*(.*?)\n*&amp;lt;/pre&amp;gt;\s*&amp;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;&amp;quot;\n{% endraw %}\n{% highlight &amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&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;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;&amp;quot; %}\n&amp;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;n&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;&amp;quot;\n{% endhighlight %}\n{% raw %}\n&amp;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;n&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;n&quot;&gt;Regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&amp;quot;{% raw %}\s*{% endraw %}\s*&amp;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;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;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;n&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;n&quot;&gt;Regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&amp;quot;&amp;lt;font face=&amp;quot;&amp;quot;Courier New&amp;quot;&amp;quot;&amp;gt;([^&amp;lt;]+)&amp;lt;/font&amp;gt;&amp;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;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;code&amp;gt;$1&amp;lt;/code&amp;gt;&amp;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;n&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;&amp;quot;*.html&amp;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;n&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;n&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;n&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;n&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;p&gt;(This kind of script is most easily run in &lt;a href=&quot;http://linqpad.net&quot;&gt;LINQPad&lt;/a&gt; or &lt;a href=&quot;http://scriptcs.net/&quot;&gt;scriptcs&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;This code loops through every HTML file in &lt;code&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&gt;&amp;lt;pre class=&amp;quot;brush: someLanguage&amp;quot;&amp;gt;...&amp;lt;/pre&amp;gt;&lt;/code&gt;, and convers each to Jekyll &lt;code&gt;{% highlight someLanguage %}&lt;/code&gt; blocks.  Since SyntaxHighlighter uses different language names than &lt;a href=&quot;http://pygments.org/docs/lexers/#lexers-for-net-languages&quot;&gt;Pygments&amp;#39;&lt;/a&gt;, I have a &lt;code&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&gt;{% raw %}&lt;/code&gt; tag, it terminates the &lt;code&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&gt;{% highligh %}&lt;/code&gt; tag in a &lt;code&gt;{% raw %}&lt;/code&gt; block too.  &lt;/p&gt;

&lt;p&gt;The next regex strips empty &lt;code&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&gt;&amp;lt;font&amp;gt;&lt;/code&gt; tags created by Windows Live Writer with &lt;code&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>_posts/2013-05-31-migrating-from-blogger-to-jekyll.md</id>
		<link rel="alternate" type="text/html" href="//2013-05-31/migrating-from-blogger-to-jekyll"/>
		<title type="text">Migrating from Blogger to Jekyll</title>
		<updated>Fri, 31 May 2013 00:00:00 -0700</updated>
		<published>Fri, 31 May 2013 00:00:00 -0700</published>

		

		<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&amp;#39;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&amp;#39;ll need to install &lt;a href=&quot;http://nodejs.org&quot;&gt;Node.js&lt;/a&gt; first)&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash language-bash&quot; data-lang=&quot;bash&quot;&gt;npm install -g blogger2jekyll
blogger2jekyll  /path/to/blog-dd-mm-yyyy.xml ./_posts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you aren&amp;#39;t running it from the directory containing your Jekyll site, you&amp;#39;ll need to specify the full path to Jekyll&amp;#39;s &lt;code&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&gt;layout: &amp;quot;post&amp;quot;&lt;/code&gt; in the Jekyll &lt;a href=&quot;http://jekyllrb.com/docs/frontmatter/&quot;&gt;front matter&lt;/a&gt;; if you have a different layout name, you&amp;#39;ll need to do a bulk replace within the resulting files.  It will also set the &lt;code&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&amp;#39;t add a command-line interface for the comment option; to use it, you&amp;#39;ll need to manually add &lt;code&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&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>_posts/2013-05-27-about-this-design.md</id>
		<link rel="alternate" type="text/html" href="//2013-05-27/about-this-design"/>
		<title type="text">About the new design</title>
		<updated>Mon, 27 May 2013 00:00:00 -0700</updated>
		<published>Mon, 27 May 2013 00:00:00 -0700</published>

		

		<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;ul&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;/ul&gt;&lt;/li&gt;
&lt;li&gt;No build step

&lt;ul&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;/ul&gt;&lt;/li&gt;
&lt;li&gt;No Javascript

&lt;ul&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;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Don%27t_repeat_yourself&quot; title=&quot;Don&amp;#39;t repeat yourself&quot;&gt;DRY&lt;/a&gt; implementation

&lt;ul&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;/ul&gt;&lt;/li&gt;
&lt;li&gt;Rich category support

&lt;ul&gt;
&lt;li&gt; I feel that category listing pages are a great way to make posts more discoverable&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Clean, minimalist, design

&lt;ul&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;/ul&gt;&lt;/li&gt;
&lt;li&gt;Easy navigation within series of multiple posts

&lt;ul&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;/ul&gt;&lt;/li&gt;
&lt;li&gt;Excessive use of bulleted lists

&lt;ul&gt;
&lt;li&gt;Bulleted lists are an excellent way to concisely present structured data&lt;/li&gt;
&lt;/ul&gt;&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>_posts/2013-05-26-relaunch.md</id>
		<link rel="alternate" type="text/html" href="//2013-05-26/relaunch"/>
		<title type="text">Relaunch!</title>
		<updated>Sun, 26 May 2013 00:00:00 -0700</updated>
		<published>Sun, 26 May 2013 00:00:00 -0700</published>

		

		<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&amp;#39;t like Blogger&amp;#39;s compose options.  After spending so much time on StackOverflow and GitHub, I find Markdown far more convenient than Windows Live Writer or Blogger&amp;#39;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;http://jekyllrb.com&quot;&gt;Jekyll&lt;/a&gt; on &lt;a href=&quot;http://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&amp;#39;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="//2012/06/visual-studio-2012-and-webpagesversion.html"/>
		<title type="text">Visual Studio 2012 and webpages:Version</title>
		<updated>Mon, 11 Jun 2012 22:33:00 -0700</updated>
		<published>Tue, 12 Jun 2012 12:32:59 -0700</published>

		

		<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="//2012/03/exploring-caller-info-attributes.html"/>
		<title type="text">Exploring Caller Info Attributes</title>
		<updated>Thu, 01 Mar 2012 06:57:00 -0800</updated>
		<published>Thu, 01 Mar 2012 06:57:54 -0800</published>

		

		<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;http://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;http://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;http://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;http://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="//2012/02/aspnet-mvc-unobtrusive-validation-bug.html"/>
		<title type="text">ASP.Net MVC Unobtrusive Validation Bug</title>
		<updated>Tue, 21 Feb 2012 18:15:00 -0800</updated>
		<published>Tue, 21 Feb 2012 18:15:54 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;s2&quot;&gt;&amp;quot;equalto&amp;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;s2&quot;&gt;&amp;quot;other&amp;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;s2&quot;&gt;&amp;quot;:input[name=&amp;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;s2&quot;&gt;&amp;quot;]&amp;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;s2&quot;&gt;&amp;quot;equalTo&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;s2&quot;&gt;&amp;quot;equalto&amp;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;s2&quot;&gt;&amp;quot;other&amp;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;s2&quot;&gt;&amp;quot; not found&amp;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;s2&quot;&gt;&amp;quot;equalTo&amp;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;/div&gt;
&lt;/div&gt;</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4405908569432660011</id>
		<link rel="alternate" type="text/html" href="//2012/01/protecting-against-csrf-attacks-in.html"/>
		<title type="text">Protecting against CSRF attacks in ASP.Net MVC</title>
		<updated>Wed, 25 Jan 2012 23:18:00 -0800</updated>
		<published>Wed, 25 Jan 2012 23:18:59 -0800</published>

		

		<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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;na&quot;&gt;[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]&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;/div&gt;

&lt;p&gt;Then, add the following unit test:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;[TestMethod]&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;n&quot;&gt;GetTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&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;n&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;n&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;n&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;n&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;n&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;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;n&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;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;n&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;GetParameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&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;n&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;n&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;n&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;n&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;n&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;&amp;quot; is [HttpPost] but not [ValidateAntiForgeryToken]&amp;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;/div&gt;

typeof(MvcApplication) 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;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-8770635603144624525</id>
		<link rel="alternate" type="text/html" href="//2011/12/dark-side-of-covariance.html"/>
		<title type="text">The Dark Side of Covariance</title>
		<updated>Thu, 15 Dec 2011 04:14:00 -0800</updated>
		<published>Thu, 15 Dec 2011 04:17:32 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&quot;&gt;ExecuteScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;//http://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;n&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;n&quot;&gt;ExecuteScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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="//2011/12/captchas-do-not-mitigate-xss-worms.html"/>
		<title type="text">CAPTCHAs do not mitigate XSS worms</title>
		<updated>Thu, 08 Dec 2011 02:32:00 -0800</updated>
		<published>Thu, 08 Dec 2011 02:32:19 -0800</published>

		

		<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;http://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;http://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="//2011/11/one-of-most-useful-additions-to.html"/>
		<title type="text">About Concurrent Collections</title>
		<updated>Sun, 20 Nov 2011 18:42:00 -0800</updated>
		<published>Sun, 20 Nov 2011 18:42:58 -0800</published>

		

		<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;http://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;http://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;http://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="//2011/11/beware-of-responseredirecttoroute-in.html"/>
		<title type="text">Beware of Response.RedirectToRoute in MVC 3.0</title>
		<updated>Wed, 09 Nov 2011 04:00:00 -0800</updated>
		<published>Wed, 09 Nov 2011 04:00:26 -0800</published>

		

		<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;http://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;http://msdn.microsoft.com/en-us/library/system.web.httpcontextwrapper.aspx&quot;&gt;HttpContextWrapper&lt;/a&gt;, which wraps &lt;a href=&quot;http://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;http://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;http://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;http://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;http://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="//2011/10/caller-info-attributes-vs-stack-walking.html"/>
		<title type="text">Caller Info Attributes vs. Stack Walking</title>
		<updated>Sun, 23 Oct 2011 23:55:00 -0700</updated>
		<published>Sun, 23 Oct 2011 23:55:37 -0700</published>

		

		<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;http://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="//2011/10/subtleties-of-c-5s-new-callermembername.html"/>
		<title type="text">Subtleties of C# 5’s new [CallerMemberName]</title>
		<updated>Tue, 18 Oct 2011 00:31:00 -0700</updated>
		<published>Thu, 01 Mar 2012 07:02:51 -0800</published>

		

		<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;http://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="//2011/10/subtleties-of-c-5s-new-callerlinenumber.html"/>
		<title type="text">Subtleties of C# 5’s new [CallerLineNumber]</title>
		<updated>Fri, 07 Oct 2011 15:20:00 -0700</updated>
		<published>Thu, 01 Mar 2012 07:02:20 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;na&quot;&gt;                        [CallerLineNumber]&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;n&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;&amp;quot;: &amp;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;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;n&quot;&gt;Funny&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
         &lt;span class=&quot;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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="//2011/10/subtleties-of-new-caller-info.html"/>
		<title type="text">Subtleties of the new Caller Info Attributes in C# 5</title>
		<updated>Thu, 06 Oct 2011 13:02:00 -0700</updated>
		<published>Thu, 01 Mar 2012 07:01:28 -0800</published>

		

		<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;http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-816T&quot;&gt;presented&lt;/a&gt; at //Build/, you can write&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;na&quot;&gt;    [CallerFilePath]&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;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;    [CallerLineNumber]&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;na&quot;&gt;    [CallerMemberName]&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;&amp;quot;&amp;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;n&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;{0}:{1} – {2}: {3}&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&quot;&gt;printer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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="//2011/09/using-default-controller-in-aspnet-mvc.html"/>
		<title type="text">Using a default controller in ASP.Net MVC</title>
		<updated>Mon, 19 Sep 2011 02:01:00 -0700</updated>
		<published>Mon, 19 Sep 2011 02:01:23 -0700</published>

		

		<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;http://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;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;User routes&amp;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;&amp;quot;{userName}/{action}&amp;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;&amp;quot;Index&amp;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;/div&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="//2011/09/xregexp-breaks-jquery-animations.html"/>
		<title type="text">XRegExp breaks jQuery Animations</title>
		<updated>Thu, 15 Sep 2011 01:12:00 -0700</updated>
		<published>Thu, 15 Sep 2011 01:12:34 -0700</published>

		

		<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;http://xregexp.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://xregexp.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;http://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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&gt;

&lt;a href=&quot;http://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="//2011/09/clarifying-boolean-parameters-part-2.html"/>
		<title type="text">Clarifying Boolean Parameters, part 2</title>
		<updated>Wed, 14 Sep 2011 15:30:00 -0700</updated>
		<published>Wed, 14 Sep 2011 15:30:38 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//C# 4.0:&lt;/span&gt;
&lt;span class=&quot;n&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;/div&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;vbnet&quot;&gt;&lt;span class=&quot;c&quot;&gt;&amp;#39;VB.Net:&lt;/span&gt;
&lt;span class=&quot;n&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;o&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;s2&quot;&gt;&amp;quot;With animation&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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="//2011/09/clarifying-boolean-parameters-part-1.html"/>
		<title type="text">Clarifying Boolean Parameters, part 1</title>
		<updated>Mon, 12 Sep 2011 21:31:00 -0700</updated>
		<published>Wed, 14 Sep 2011 15:31:24 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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="//2011/09/c-is-not-type-safe.html"/>
		<title type="text">C# is not type-safe</title>
		<updated>Thu, 08 Sep 2011 02:12:00 -0700</updated>
		<published>Thu, 08 Sep 2011 02:12:06 -0700</published>

		

		<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;http://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;http://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;http://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;http://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;http://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;http://msdn.microsoft.com/en-us/library/system.single.aspx&quot;&gt;Single&lt;/a&gt; or a &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.double.aspx&quot;&gt;Double&lt;/a&gt; to a &lt;a href=&quot;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;http://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="//2011/08/delegates-vs-function-pointers-addendum.html"/>
		<title type="text">Delegates vs. Function Pointers, Addendum: Multicast Delegates</title>
		<updated>Tue, 16 Aug 2011 01:02:00 -0700</updated>
		<published>Tue, 16 Aug 2011 01:02:38 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&gt;

&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-3943524177840514075</id>
		<link rel="alternate" type="text/html" href="//2011/07/tracking-event-handler-registrations.html"/>
		<title type="text">Tracking Event Handler Registrations</title>
		<updated>Fri, 29 Jul 2011 19:58:00 -0700</updated>
		<published>Fri, 29 Jul 2011 19:58:48 -0700</published>

		

		<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;http://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;http://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="//2011/07/about-net-events.html"/>
		<title type="text">About .Net Events</title>
		<updated>Fri, 29 Jul 2011 02:14:00 -0700</updated>
		<published>Fri, 29 Jul 2011 22:38:04 -0700</published>

		

		<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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;n&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;n&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;/div&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;http://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;http://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;http://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;http://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="//2011/07/creating-local-extension-methods.html"/>
		<title type="text">Creating Local Extension Methods</title>
		<updated>Wed, 27 Jul 2011 02:08:00 -0700</updated>
		<published>Wed, 27 Jul 2011 02:08:23 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;&amp;quot;(null &amp;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;&amp;quot;)&amp;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;n&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;&amp;quot;: &amp;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;n&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;&amp;quot;{declared as &amp;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;&amp;quot;}&amp;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;n&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;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;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;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="//2011/07/dont-modify-other-controls-during-wpf.html"/>
		<title type="text">Don’t modify other controls during a WPF layout pass</title>
		<updated>Tue, 26 Jul 2011 02:18:00 -0700</updated>
		<published>Tue, 26 Jul 2011 02:18:56 -0700</published>

		

		<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;http://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;http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride.aspx&quot;&gt;MeasureOverride&lt;/a&gt; and &lt;a href=&quot;http://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;http://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;http://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;http://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;http://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;http://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="//2011/07/delegates-vs-function-pointers-part-5.html"/>
		<title type="text">Delegates vs. Function Pointers, part 5: Javascript</title>
		<updated>Mon, 25 Jul 2011 00:33:00 -0700</updated>
		<published>Mon, 25 Jul 2011 00:33:16 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Target!&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Target!&amp;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;/div&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="//2011/06/delegates-vs-function-pointers-part-4-c.html"/>
		<title type="text">Delegates vs. Function Pointers, part 4: C# 2.0+</title>
		<updated>Sun, 19 Jun 2011 19:49:00 -0700</updated>
		<published>Tue, 16 Aug 2011 01:09:05 -0700</published>

		

		<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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;/div&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;http://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;http://wiki.sharpdevelop.net/ILSpy.ashx&quot;&gt;ILSpy&lt;/a&gt; to decompile the more-familiar C# 3 version: (I simplified the &lt;a href=&quot;http://stackoverflow.com/q/6402491/34397&quot;&gt;compiler-generated names&lt;/a&gt; for readability)&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;[CompilerGenerated]&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;n&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;n&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;/div&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="//2011/06/open-delegates-vs-closed-delegates.html"/>
		<title type="text">Open Delegates vs. Closed Delegates</title>
		<updated>Tue, 14 Jun 2011 14:49:00 -0700</updated>
		<published>Wed, 31 Aug 2011 01:36:14 -0700</published>

		

		<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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;a&amp;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;n&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;n&quot;&gt;GetMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;ToUpperInvariant&amp;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;closed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;//Returns &amp;quot;A&amp;quot;&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;s&quot;&gt;&amp;quot;abc&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//Returns &amp;quot;ABC&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Closed delegates are created by calling the &lt;a href=&quot;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&quot;&gt;GetMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Equals&amp;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;/div&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;http://stackoverflow.com/q/1016033/34397&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;/div&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="//2011/06/delegates-vs-function-pointers-part-3-c.html"/>
		<title type="text">Delegates vs. Function Pointers, part 3: C# 1.0</title>
		<updated>Mon, 13 Jun 2011 21:29:00 -0700</updated>
		<published>Sun, 19 Jun 2011 19:49:57 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;n&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;n&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;n&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;/div&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="//2011/06/delegates-vs-function-pointers-part-2-c.html"/>
		<title type="text">Delegates vs. Function Pointers, part 2: C</title>
		<updated>Wed, 01 Jun 2011 21:08:00 -0700</updated>
		<published>Tue, 14 Jun 2011 02:37:22 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&gt;

&lt;p&gt;With this function, one can easily find all elements that are greater than some constant:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;http://en.wikipedia.org/wiki/Thread-local_storage&quot;&gt;thread-local variable&lt;/a&gt; (or in &lt;a href=&quot;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;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;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;/div&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="//2011/06/delegates-vs-function-pointers-part-1.html"/>
		<title type="text">Delegates vs. Function Pointers, part 1</title>
		<updated>Wed, 01 Jun 2011 20:21:00 -0700</updated>
		<published>Tue, 16 Aug 2011 01:10:34 -0700</published>

		

		<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;http://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="//2011/05/htmlforeach-in-razor.html"/>
		<title type="text">Html.ForEach in Razor</title>
		<updated>Wed, 11 May 2011 02:56:00 -0700</updated>
		<published>Wed, 11 May 2011 02:56:12 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;aspx-cs&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;%&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;ForEach&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;n&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;n&quot;&gt;item&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;nt&quot;&gt;%&amp;gt;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&amp;gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;%&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;&amp;lt;%&lt;/span&gt; &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;%&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;n&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;n&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;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;/div&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="//2011/05/creating-markup-actions-in-razor.html"/>
		<title type="text">Creating Markup Actions in Razor</title>
		<updated>Mon, 02 May 2011 18:06:00 -0700</updated>
		<published>Mon, 02 May 2011 18:06:18 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;aspx-cs&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;%&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;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;%&amp;gt;&amp;lt;b&amp;gt;&lt;/span&gt;I&amp;#39;m from a lambda!&lt;span class=&quot;nt&quot;&gt;&amp;lt;/b&amp;gt;&amp;lt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&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;n&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&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;nt&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;#39;&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;n&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;n&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;&amp;#39;&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;n&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&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;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;/code&gt;&lt;/pre&gt;&lt;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&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;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;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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;&amp;#39;&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;n&quot;&gt;pageWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&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;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;/code&gt;&lt;/pre&gt;&lt;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&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;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;/div&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="//2011/04/dissecting-razor-part-9-inline-helpers.html"/>
		<title type="text">Dissecting Razor, part 9: Inline Helpers</title>
		<updated>Fri, 29 Apr 2011 14:08:00 -0700</updated>
		<published>Fri, 29 Apr 2011 19:08:58 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;&amp;quot;@Href(&amp;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;&amp;quot;)&amp;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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&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;&amp;quot;@Href(&amp;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;&amp;quot;)&amp;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;&amp;quot;post&amp;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;n&quot;&gt;TextArea&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Comment&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Type a comment&amp;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;&amp;quot;submit&amp;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;&amp;quot;Submit&amp;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;&amp;quot;Add Comment&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&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;n&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;n&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;&amp;quot;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&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;&amp;quot;&amp;lt;form action=\&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&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;Href&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Add-Comment&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;n&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;&amp;quot;\&amp;quot; method=\&amp;quot;post\&amp;quot;&amp;gt;\r\n        &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&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;n&quot;&gt;TextArea&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Comment&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Type a comment&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;n&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;&amp;quot;\r\n   ...  &amp;lt;/form&amp;gt;\r\n&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&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;/div&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="//2011/03/dissecting-razor-part-8-static-helpers.html"/>
		<title type="text">Dissecting Razor, part 8: Static Helpers</title>
		<updated>Tue, 08 Mar 2011 16:41:00 -0800</updated>
		<published>Fri, 29 Apr 2011 14:09:11 -0700</published>

		

		<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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;&amp;quot;@Href(pair[1])&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;&amp;quot;Home&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;s&quot;&gt;&amp;quot;~/&amp;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;&amp;quot;About&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;s&quot;&gt;&amp;quot;~/About&amp;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;&amp;quot;Contact&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;~/Contact&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;&amp;quot;    &amp;lt;ul&amp;gt;\r\n&amp;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;n&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;&amp;quot;            &amp;lt;li&amp;gt;&amp;lt;a href=\&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&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;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;n&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;&amp;quot;\&amp;quot;&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&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;n&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;&amp;quot;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;\r\n&amp;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;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;&amp;quot;    &amp;lt;/ul&amp;gt;\r\n&amp;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;/div&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="//2011/03/boot-camp-just-got-better.html"/>
		<title type="text">Boot Camp just got better</title>
		<updated>Sun, 06 Mar 2011 16:33:00 -0800</updated>
		<published>Sun, 06 Mar 2011 16:33:36 -0800</published>

		

		<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;http://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;http://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;http://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="//2011/02/dissecting-razor-part-7-helpers.html"/>
		<title type="text">Dissecting Razor, part 7: Helpers</title>
		<updated>Mon, 28 Feb 2011 16:08:00 -0800</updated>
		<published>Fri, 29 Apr 2011 13:51:31 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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 ? &amp;quot;Even&amp;quot; : &amp;quot;Odd&amp;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; 10&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;@&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;&amp;quot;            &amp;lt;tr&amp;gt;\r\n                &amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&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;n&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;&amp;quot;&amp;lt;/td&amp;gt;\r\n                &amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&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;n&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;&amp;quot;&amp;lt;/td&amp;gt;\r\n                &amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&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;&amp;quot;Even&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Odd&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;n&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;&amp;quot;&amp;lt;/td&amp;gt;\r\n            &amp;lt;/tr&amp;gt;\r\n&amp;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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;!DOCTYPE html&amp;gt;\r\n&amp;lt;html&amp;gt;\r\n    &amp;lt;body&amp;gt;\r\n        &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;\r\n        &amp;lt;table&amp;gt;\r\n            &amp;lt;thead&amp;gt;\r\n                &amp;lt;tr&amp;gt;\r\n                   &amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&amp;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&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&amp;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&amp;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;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;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&amp;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;/div&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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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="//2011/02/dissecting-razor-part-6-function-blocks.html"/>
		<title type="text">Dissecting Razor, part 6: Function Blocks</title>
		<updated>Fri, 18 Feb 2011 02:46:00 -0800</updated>
		<published>Mon, 28 Feb 2011 16:09:33 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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&amp;#39;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line 4 &amp;quot;...\Functions.cshtml&amp;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&amp;#39;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;n&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&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_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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;!DOCTYPE html&amp;gt;\r\n&amp;lt;html&amp;gt;\r\n    &amp;lt;body&amp;gt;\r\n        &amp;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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;\r\n        &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 10 &amp;quot;...\Functions.cshtml&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&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&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line hidden&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot; characters have been written so far.\r\n    &amp;lt;/body&amp;gt;\r\n&amp;lt;/html&amp;gt;\r\n&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 13 &amp;quot;...\Functions.cshtml&amp;quot;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#error&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line default&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line hidden&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;     &amp;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;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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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="//2011/02/dissecting-razor-part-5-use-source-luke.html"/>
		<title type="text">Dissecting Razor, part 5: Use the Source, Luke</title>
		<updated>Wed, 16 Feb 2011 16:54:00 -0800</updated>
		<published>Fri, 18 Feb 2011 02:50:23 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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 = &amp;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;&amp;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;/div&gt;

&lt;p&gt;This page is rendered like this: (after removing &lt;code&gt;@{ #error }&lt;/code&gt;)&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;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 = &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 4 &amp;quot;...\SimplePage.cshtml&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;n&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&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line hidden&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;br /&amp;gt;\r\n&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 5 &amp;quot;...\SimplePage.cshtml&amp;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;&amp;quot;&amp;lt;b&amp;gt;bold &amp;amp;amp; fancy&amp;lt;/b&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line hidden&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;        &amp;lt;code&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 6 &amp;quot;...\SimplePage.cshtml&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;n&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&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line hidden&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;/code&amp;gt; is rendered as\r\n        &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 7 &amp;quot;...\SimplePage.cshtml&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;n&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;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&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line hidden&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;\r\n    &amp;lt;/body&amp;gt;\r\n&amp;lt;/html&amp;gt;\r\n&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#line 10 &amp;quot;...\SimplePage.cshtml&amp;quot;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#error&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#line default&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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; 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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;!DOCTYPE html&amp;gt;\r\n&amp;lt;html&amp;gt;\r\n    &amp;lt;body&amp;gt;\r\n&amp;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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;        &amp;lt;table&amp;gt;\r\n&amp;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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;                &amp;lt;tr&amp;gt;\r\n                    &amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;n&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;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;/td&amp;gt;\r\n                    &amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;n&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;n&quot;&gt;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;/td&amp;gt;\r\n                &amp;lt;/tr&amp;gt;\r\n&amp;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;WriteLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;        &amp;lt;/table&amp;gt;\r\n    &amp;lt;/body&amp;gt;\r\n&amp;lt;/html&amp;gt;\r\n&amp;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;/div&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="//2011/02/dissecting-razor-part-4-anatomy-of.html"/>
		<title type="text">Dissecting Razor, part 4: Anatomy of a Razor Page</title>
		<updated>Wed, 09 Feb 2011 14:08:00 -0800</updated>
		<published>Fri, 18 Feb 2011 03:43:51 -0800</published>

		

		<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;http://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;http://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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;MyProject.MyWebPageRazorHost&amp;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;&amp;quot;MyProject.CustomWebPage&amp;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;&amp;quot;MyProject.SomeNamespace&amp;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;/div&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="//2011/02/dissecting-razor-part-3-razor-and-mvc.html"/>
		<title type="text">Dissecting Razor, part 3: Razor and MVC</title>
		<updated>Thu, 03 Feb 2011 18:49:00 -0800</updated>
		<published>Wed, 09 Feb 2011 14:09:35 -0800</published>

		

		<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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&amp;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;&amp;quot;System.Web.Mvc.WebViewPage&amp;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;&amp;quot;System.Web.Mvc&amp;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;&amp;quot;System.Web.Mvc.Ajax&amp;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;&amp;quot;System.Web.Mvc.Html&amp;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;&amp;quot;System.Web.Routing&amp;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;/div&gt;

&lt;h2&gt;MVC Side&lt;/h2&gt;

&lt;p&gt;On the MVC side, MVC3 includes a new &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.web.mvc.razorviewengine.aspx&quot;&gt;RazorViewEngine&lt;/a&gt; which creates &lt;a href=&quot;http://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;http://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;http://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="//2011/02/dissecting-razor-part-2-gluing-pieces.html"/>
		<title type="text">Dissecting Razor, part 2: Gluing the pieces together</title>
		<updated>Wed, 02 Feb 2011 16:46:00 -0800</updated>
		<published>Thu, 03 Feb 2011 18:51:34 -0800</published>

		

		<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;&lt;/h2&gt;  &lt;h2&gt;&lt;/h2&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;http://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;http://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;http://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="//2011/01/dissecting-razor-part-1-pieces-of.html"/>
		<title type="text">Dissecting Razor, part 1: Parts of the framework</title>
		<updated>Tue, 01 Feb 2011 03:51:00 -0800</updated>
		<published>Wed, 02 Feb 2011 16:47:51 -0800</published>

		

		<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;http://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;http://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;http://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="//2011/01/modifying-html-strings-using-jquery.html"/>
		<title type="text">Modifying HTML strings using jQuery</title>
		<updated>Mon, 31 Jan 2011 13:55:00 -0800</updated>
		<published>Mon, 31 Jan 2011 14:02:52 -0800</published>

		

		<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;http://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;http://jsfiddle.net/SLaks/Q6VNH/&quot;&gt;demo&lt;/a&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;s1&quot;&gt;&amp;#39;a[href]&amp;#39;&lt;/span&gt;&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;/div&gt;

&lt;p&gt;After getting used to this, &lt;a href=&quot;http://stackoverflow.com/questions/4750703/cant-modify-returned-ajax-variable&quot;&gt;one&lt;/a&gt; &lt;a href=&quot;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;s1&quot;&gt;&amp;#39;a[href]&amp;#39;&lt;/span&gt;&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;s2&quot;&gt;&amp;quot;&amp;lt;div&amp;gt;&amp;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;s2&quot;&gt;&amp;quot;&amp;lt;/div&amp;gt;&amp;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;s1&quot;&gt;&amp;#39;a[href]&amp;#39;&lt;/span&gt;&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;/div&gt;

&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:blogger.com,1999:blog-4137132196361303955.post-4895941282151911436</id>
		<link rel="alternate" type="text/html" href="//2011/01/generic-base-classes-in-aspnet-mvc.html"/>
		<title type="text">Generic Base Classes in ASP.Net MVC</title>
		<updated>Sun, 30 Jan 2011 23:46:00 -0800</updated>
		<published>Sun, 30 Jan 2011 23:46:25 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;aspx-cs&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;%@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Page&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Language&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;Inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;ViewPage&amp;lt;IEnumerable&amp;lt;DataLayer.Product&amp;gt;&amp;gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;http://msdn.microsoft.com/en-us/library/system.web.ui.pageparserfilter.aspx&quot;&gt;PageParserFilter&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;false&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;pageParserFilterType=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&amp;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;/div&gt;

&lt;p&gt;&lt;a href=&quot;http://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;http://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="//2011/01/generic-base-classes-in-aspnet.html"/>
		<title type="text">Generic Base Classes in ASP.Net</title>
		<updated>Thu, 27 Jan 2011 16:55:00 -0800</updated>
		<published>Wed, 02 Feb 2011 16:48:18 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;aspx-cs&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;%@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Page&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Language&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;DataPage&amp;lt;string&amp;gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;aspx-cs&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;%@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Page&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Language&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;Inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;TestSite.DataPage`1[[System.String, mscorlib]]&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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="//2011/01/building-connection-strings-in-net.html"/>
		<title type="text">Building Connection Strings in .Net</title>
		<updated>Wed, 26 Jan 2011 14:20:00 -0800</updated>
		<published>Wed, 26 Jan 2011 14:20:29 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;Data Source=&amp;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;&amp;quot;; &amp;quot;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Provider=Microsoft.Jet.OLEDB.4.0;&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Extended Properties=\&amp;quot;Excel 8.0\&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;Microsoft.Jet.OLEDB.4.0&amp;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;&amp;quot;Extended Properties&amp;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;&amp;quot;Excel 12.0 Macro&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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="//2011/01/dont-call-htmlencode-in-razor.html"/>
		<title type="text">Don’t call Html.Encode in Razor Pages</title>
		<updated>Fri, 21 Jan 2011 01:35:00 -0800</updated>
		<published>Sun, 06 Mar 2011 16:35:13 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&quot;&gt;Encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Double-encoded &amp;lt;html&amp;gt; text!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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="//2011/01/optional-parameters-in-c-4.html"/>
		<title type="text">Optional Parameters in C# &lt; 4</title>
		<updated>Wed, 19 Jan 2011 22:04:00 -0800</updated>
		<published>Thu, 03 Feb 2011 18:55:24 -0800</published>

		

		<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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;na&quot;&gt;    [Optional, DefaultParameterValue(4)]&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;n&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;/div&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;http://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="//2011/01/writing-output-in-razor-helpers-using.html"/>
		<title type="text">Writing output in Razor helpers using code</title>
		<updated>Wed, 12 Jan 2011 20:22:00 -0800</updated>
		<published>Tue, 08 Mar 2011 16:44:06 -0800</published>

		

		<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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&gt;


&lt;p&gt;This helper will write out the first &lt;em&gt;count&lt;/em&gt; &lt;a href=&quot;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;n&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;/div&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;http://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="//2011/01/binding-to-lists-of-datarows.html"/>
		<title type="text">Binding to lists of DataRows</title>
		<updated>Mon, 10 Jan 2011 20:30:00 -0800</updated>
		<published>Mon, 10 Jan 2011 22:02:38 -0800</published>

		

		<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;http://msdn.microsoft.com/en-us/library/system.windows.forms.listbindinghelper.aspx&quot;&gt;ListBindingHelper&lt;/a&gt; and &lt;a href=&quot;http://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;http://http://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;http://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;http://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;http://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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&quot;&gt;AsDataView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;http://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="//2010/12/partial-type-inference-in-net.html"/>
		<title type="text">Partial Type Inference in .Net</title>
		<updated>Thu, 30 Dec 2010 01:58:00 -0800</updated>
		<published>Thu, 30 Dec 2010 02:06:02 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&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;&amp;quot;SELECT COUNT(*) FROM TableName WHERE Modified &amp;gt; someDate&amp;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;k&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;n&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;k&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;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;/code&gt;&lt;/pre&gt;&lt;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&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;&amp;quot;SELECT COUNT(*) FROM TableName WHERE Modified &amp;gt; someDate&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&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;/div&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="//2010/12/simplifying-value-comparison-semantics.html"/>
		<title type="text">Simplifying Value Comparison Semantics</title>
		<updated>Wed, 29 Dec 2010 03:10:00 -0800</updated>
		<published>Sun, 05 Feb 2012 03:00:37 -0800</published>

		

		<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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&quot;&gt;Equals&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;x&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;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;//http://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;n&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;n&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;n&quot;&gt;Compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;n&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;n&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;n&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;/div&gt;

&lt;p&gt;To simplify this task, I created a &lt;a href=&quot;http://www.slaks.net/Files/ValueComparer.snippet&quot;&gt;code snippet&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;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;&amp;quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&amp;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;&amp;quot;1.0.0&amp;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;&amp;quot;false&amp;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;&amp;quot;csharp&amp;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); }&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;        public int CompareTo($classname$ obj) { return Comparer.Compare(this, obj); }&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;        int IComparable.CompareTo(object obj) { return CompareTo(obj as $classname$); }&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;        public bool Equals($classname$ obj) { return Comparer.Equals(this, obj); }&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;        public override bool Equals(object obj) { return Equals(obj as $classname$); }&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;        static readonly ValueComparer&amp;lt;$classname$&amp;gt; Comparer = new ValueComparer&amp;lt;$classname$&amp;gt;(&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;            o =&amp;gt; o.$end$&lt;/span&gt;
&lt;span class=&quot;cp&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;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;/div&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="//2010/12/when-shouldnt-you-write-ref-this.html"/>
		<title type="text">When shouldn’t you write ref this?</title>
		<updated>Wed, 22 Dec 2010 22:54:00 -0800</updated>
		<published>Wed, 21 Dec 2011 00:51:40 -0800</published>

		

		<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;http://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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&quot;&gt;MutateWrong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&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;n&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;In Main(): &amp;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;n&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;n&quot;&gt;Mutable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&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;n&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Inside MutateWrong(): &amp;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;/div&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;http://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;http://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="//2010/12/when-can-you-write-ref-this.html"/>
		<title type="text">When can you write ref this?</title>
		<updated>Wed, 22 Dec 2010 20:50:00 -0800</updated>
		<published>Wed, 22 Dec 2010 22:55:03 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;n&quot;&gt;Mutable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; 
        &lt;span class=&quot;n&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;/div&gt;

&lt;p&gt;In practice, this should never come up, since &lt;a href=&quot;http://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="//2010/12/nothing-vs-null.html"/>
		<title type="text">Nothing vs Null</title>
		<updated>Wed, 22 Dec 2010 17:28:00 -0800</updated>
		<published>Wed, 22 Dec 2010 17:28:22 -0800</published>

		

		<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;http://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;http://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;http://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;http://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="//2010/12/animating-table-rows-with-jquery.html"/>
		<title type="text">Animating Table Rows with jQuery</title>
		<updated>Tue, 21 Dec 2010 17:19:00 -0800</updated>
		<published>Tue, 21 Dec 2010 17:24:01 -0800</published>

		

		<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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;s1&quot;&gt;&amp;#39;tr&amp;#39;&lt;/span&gt;&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;s1&quot;&gt;&amp;#39;td, th&amp;#39;&lt;/span&gt;&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;nx&quot;&gt;padding&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;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;s1&quot;&gt;&amp;#39;&amp;lt;div /&amp;gt;&amp;#39;&lt;/span&gt;&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;s1&quot;&gt;&amp;#39;tr&amp;#39;&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;/div&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;http://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="//2010/12/requiring-inherited-types-in-generic.html"/>
		<title type="text">Requiring Inherited Types in Generic Constraints</title>
		<updated>Fri, 17 Dec 2010 17:08:00 -0800</updated>
		<published>Fri, 17 Dec 2010 17:08:16 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;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;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;/div&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="//2010/12/nested-iterators-part-2.html"/>
		<title type="text">Nested Iterators, part 2</title>
		<updated>Thu, 16 Dec 2010 21:50:00 -0800</updated>
		<published>Fri, 17 Dec 2010 17:11:12 -0800</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;/div&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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;n&quot;&gt;Join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;, &amp;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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;/div&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="//2010/12/nested-iterators-part-1.html"/>
		<title type="text">Nested Iterators, part 1</title>
		<updated>Thu, 16 Dec 2010 16:26:00 -0800</updated>
		<published>Thu, 16 Dec 2010 16:27:22 -0800</published>

		

		<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;http://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;http://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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;n&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;n&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;n&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;/div&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="//2010/12/on-copy-prevention-in-html-part-3.html"/>
		<title type="text">On copy prevention in HTML, part 3</title>
		<updated>Fri, 27 Apr 2007 20:56:00 -0700</updated>
		<published>Fri, 27 Apr 2007 20:56:00 -0700</published>

		

		<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;http://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;http://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="//2010/12/on-copy-prevention-in-html-part-2.html"/>
		<title type="text">On copy prevention in HTML, part 2</title>
		<updated>Mon, 16 Apr 2007 21:49:00 -0700</updated>
		<published>Mon, 16 Apr 2007 21:49:00 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;position: absolute; left: 241px; top: 0px;&amp;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;&amp;quot;position: absolute; left: 263px; top: 0px;&amp;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;&amp;quot;position: absolute; left: 117px; top: 0px;&amp;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;&amp;quot;position: absolute; left: 17px; top: 20px;&amp;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;&amp;quot;position: absolute; left: 276px; top: 0px;&amp;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;&amp;quot;position: absolute; left: 287px; top: 0px;&amp;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;&amp;quot;position: absolute; left: 129px; top: 0px;&amp;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;&amp;quot;position: absolute; left: 9px; top: 20px;&amp;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;&amp;quot;position: absolute; left: 13px; top: 0px;&amp;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;/div&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;http://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="//2010/12/on-copy-prevention-in-html-part-1.html"/>
		<title type="text">On copy prevention in HTML, part 1</title>
		<updated>Sun, 08 Apr 2007 18:27:00 -0700</updated>
		<published>Sun, 08 Apr 2007 18:27:00 -0700</published>

		

		<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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;on&amp;quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;-moz-user-select:none;&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
You can&amp;#39;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;/div&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;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&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;&amp;quot;return false;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;onmousedown=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;return false;&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
You can&amp;#39;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;/div&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>