<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Fabulous Adventures In Coding</title><link>http://blogs.msdn.com/ericlippert/default.aspx</link><description>Eric Lippert's Blog</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Making the code read like the spec</title><link>http://blogs.msdn.com/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx</link><pubDate>Mon, 08 Feb 2010 14:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9942629</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>44</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9942629.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9942629</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;As I mentioned a while back, there are &lt;A href="http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx"&gt;some bugs in the compiler code&lt;/A&gt; which analyzes whether a set of classes violates the “no cycles” rules for base classes. (That is, a class is not allowed to inherit from itself, directly or indirectly, and not allowed to inherit from one of its own nested classes, directly or indirectly.) The bugs are almost all of the form where we accidentally detect a cycle in suspicious-looking generic code but in fact there is no cycle; the bugs are the result of an attempt to modify the cycle detector from C# 1 rather than simply rewriting it from scratch to handle generics. Unfortunately we were unable to get the fixes into C# 4; these are obscure corner-case scenarios and the risk of doing the fix was considered too high.&lt;/P&gt;
&lt;P&gt;There are a number of tricky issues here, mostly around the fact that obviously we cannot know whether a set of base types are circular until we know what the base types are, but resolving the program text to determine what type the base type string “C.D&amp;lt;E.F&amp;gt;” requires us to know the base types of C, because D might be a nested type of C’s base class, not of C, so we have a bit of a chicken-and-egg problem. The code which turns strings into types has to be robust in the face of circular base types because the base type cycle detector depends on its output!&lt;/P&gt;
&lt;P&gt;So like I said, I’ve come up with a new algorithm that implements the spec more exactly, and I wanted to test it out. Rather than modifying the existing compiler to use it, I mocked it up in C# quickly first, just to give me something to play with. One of the problems that we have with the existing compiler is that it is not at all clear which parts of the code are responsible for implementing any given line in the spec. In my “maquette” of the compiler I wanted to make sure that I really was exactly implementing the spec; that might show up logical problems with either the implementation or the spec. I therefore wanted the code to read much like the spec.&lt;/P&gt;
&lt;P&gt;This little hunk of code that I wrote made me inordinately happy. Here’s the spec:&lt;/P&gt;&lt;SPAN class=spec&gt;
&lt;P&gt;A class &lt;EM&gt;directly&lt;/EM&gt; &lt;EM&gt;depends on&lt;/EM&gt; its direct base class (if any) and &lt;EM&gt;directly depends on&lt;/EM&gt; the class within which it is immediately nested (if any). The complete set of classes upon which a class &lt;EM&gt;depends&lt;/EM&gt; is the &lt;EM&gt;reflexive and transitive closure&lt;/EM&gt; of the directly-depends-upon relationship. &lt;/SPAN&gt;
&lt;P&gt;First off, what is this thing called the “reflexive and transitive closure”?&lt;/P&gt;
&lt;P&gt;Consider a “relation” – a function that takes two things and returns a Boolean that tells you whether the relation holds. A relation, call it ~&amp;gt;, is &lt;EM&gt;reflexive&lt;/EM&gt; if X~&amp;gt;X is true for every X. It is &lt;EM&gt;symmetric&lt;/EM&gt; if A~&amp;gt;B necessarily implies that B~&amp;gt;A. And it is transitive if A~&amp;gt;B and B~&amp;gt;C necessarily implies that A~&amp;gt;C. (*)&lt;/P&gt;
&lt;P&gt;For example, the relation “less than or equal to” on integers is reflexive: X≤X is true for all X. It is not symmetric: 1≤2 is true, but 2≤1 is false. And it is transitive: if A≤B and B≤C then it is necessarily true that A≤C.&lt;/P&gt;
&lt;P&gt;The relation “is equal to” is reflexive, symmetric and transitive; a relation with all three properties is said to be an “equivalence relation” because it allows you to partition a set into mutually-exclusive “equivalence classes”. &lt;/P&gt;
&lt;P&gt;The relation “is the parent of” on people is not reflexive: no one is their own parent. It is not symmetric: if A is the parent of B, then B is not the parent of A. And it is not transitive: if A is the parent of B and B is the parent of C, then A is not the parent of C. (Rather, A is the grandparent of C.)&lt;/P&gt;
&lt;P&gt;It is possible to take a nontransitive relation like “is the parent of” and from it produce a transitive relation. Basically, we simply make up a new relation that is exactly the same as the parent relation, except that we &lt;EM&gt;enforce&lt;/EM&gt; that it be transitive. This is the “is the ancestor of” relation: if A is the ancestor of B, and B is the ancestor of C, then A is necessarily the ancestor of C. The “ancestor” relation is said to be the transitive closure of the “parent” relation. &lt;/P&gt;
&lt;P&gt;Similarly we can define the reflexive closure, and so on.&lt;/P&gt;
&lt;P&gt;When we’re talking about closures, we’re often interested not so much in the relation itself as the &lt;EM&gt;set of things which satisfy the relation with a given item&lt;/EM&gt;. That’s what we mean in the spec when we say “The complete set of classes upon which a class &lt;EM&gt;depends&lt;/EM&gt; is the &lt;EM&gt;reflexive and transitive closure&lt;/EM&gt; of the directly-depends-upon relationship.”&amp;nbsp; Given a class, we want to compute the set that contains the class itself (because the closure is reflexive), its base class, its outer class, the base class of the base class, the outer class of the base class, the base class of the outer class, the outer class of the outer class… and so on. &lt;/P&gt;
&lt;P&gt;So the first thing I did was I wrote up a helper method that takes an item and a function which identifies all the items that have the non-transitive relation with that item, and computes from that the set of all items that satisfy the transitive closure relation with the item:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;static HashSet&amp;lt;T&amp;gt; TransitiveClosure&amp;lt;T&amp;gt;(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; this Func&amp;lt;T, IEnumerable&amp;lt;T&amp;gt;&amp;gt; relation,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T item)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var closure = new HashSet&amp;lt;T&amp;gt;();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var stack = new Stack&amp;lt;T&amp;gt;();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack.Push(item); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(stack.Count &amp;gt; 0)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T current = stack.Pop();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach(T newItem in relation(current))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!closure.Contains(newItem))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; closure.Add(newItem);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack.Push(newItem);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return closure;&lt;BR&gt;} &lt;BR&gt;static HashSet&amp;lt;T&amp;gt; TransitiveAndReflexiveClosure&amp;lt;T&amp;gt;(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; this Func&amp;lt;T, IEnumerable&amp;lt;T&amp;gt;&amp;gt; relation,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T item)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; var closure = TransitiveClosure(relation, item);&lt;BR&gt;&amp;nbsp; closure.Add(item);&lt;BR&gt;&amp;nbsp; return closure;&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Notice that essentially what we’re doing here is a &lt;STRONG&gt;depth-first traversal&lt;/STRONG&gt; of the graph defined by the transitive closure relation, avoiding descent into regions we’ve visited before.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Now I have a mechanism which I can use to write code that implements the policies described in the specification. &lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;static IEnumerable&amp;lt;TypeSymbol&amp;gt; DirectDependencies(TypeSymbol symbol)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC: A class directly depends on its direct base class (if any) ...&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (symbol.BaseTypeSymbol != null) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield return symbol.BaseTypeSymbol;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC: ...and directly depends on the class within which it&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC: is immediately nested (if any).&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (symbol.OuterTypeSymbol!= null) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield return symbol.OuterTypeSymbol;&lt;BR&gt;} &lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Great, we now have a method that exactly implements one sentence of the spec – given a type symbol, we can determine what its direct dependencies are. Now we need another method to implement the next sentence of the spec:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;static HashSet&amp;lt;TypeSymbol&amp;gt; Dependencies(TypeSymbol classSymbol)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC:&amp;nbsp; The complete set of classes upon which a class&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC:&amp;nbsp; depends is the reflexive and transitive closure of&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC:&amp;nbsp; the directly-depends-upon relationship.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return TransitiveAndReflexiveClosure(DirectDependencies, classSymbol);&lt;BR&gt;} &lt;/SPAN&gt;
&lt;P&gt;That’s what I like to see: &lt;STRONG&gt;code that reads almost exactly like the spec&lt;/STRONG&gt;. 
&lt;P&gt;Note also that I’ve made the design choice here to make these methods static methods of a policy class, rather than methods on the TypeSymbol class. I&lt;STRONG&gt; want to keep my policies logically and textually separated from my mechanisms.&lt;/STRONG&gt; For example, I could be using the same classes that represent classes, structs, namespaces, and so on, to implement VB instead of C#. I want the policies of the C# language to be in a class whose sole responsibility is implementing these policies.&lt;/P&gt;
&lt;P&gt;Another nice aspect of this approach is that I can now re-use my transitive closure computing mechanism when I come across this bit of the spec that talks about base interfaces of an interface:&lt;/P&gt;&lt;SPAN class=spec&gt;
&lt;P&gt;The set of base interfaces is the transitive closure of the explicit base interfaces. &lt;/SPAN&gt;
&lt;P&gt;Unsurprisingly, the code in my prototype that computes this looks like: &lt;SPAN class=code&gt;
&lt;P&gt;static HashSet&amp;lt;TypeSymbol&amp;gt; BaseInterfaces(TypeSymbol interfaceSymbol)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC: The set of base interfaces is the transitive closure&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SPEC: of the explicit base interfaces. &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return TransitiveClosure(ExplicitBaseInterfaces, interfaceSymbol);&lt;BR&gt;} &lt;/SPAN&gt;
&lt;P&gt;In fact, there are transitive closures in a number of places in the C# spec, and now I have a mechanism that I can use to implement all of them, should I need to for my prototype. 
&lt;P&gt;One final note: notice that I am returning a mutable collection here. Were I designing these methods for public consumption, I would probably choose to return an immutable set, rather than a mutable one, so that I could cache the result safely; these methods could be memoized since the set of dependencies does not change over time. But for the purposes of my quick little prototype, I’m just being lazy and returning a mutable set and not caching the result. 
&lt;P&gt;Hopefully we'll get this new algorithm into the hypothetical next compiler release. 
&lt;P mce_keep="true"&gt;*************&lt;/P&gt;
&lt;P mce_keep="true"&gt;(*) Incidentally, we are holding on to the wavy arrow ~&amp;gt; as a potential operator for future hypothetical versions of C#. We recently considered it to have the meaning a~&amp;gt;b() means ((dynamic)a).b(). The operator is known as “the naj operator” after Cyrus Najmabadi, who advocated the use of this operator to the C# design team. Anyone who has a great idea for what the naj should mean, feel free to leave a comment.&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9942629" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category></item><item><title>How Many Passes?</title><link>http://blogs.msdn.com/ericlippert/archive/2010/02/04/how-many-passes.aspx</link><pubDate>Thu, 04 Feb 2010 14:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941619</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>48</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941619.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941619</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Large bodies of code written in the C/C++ languages typically divide up the code into “header” files, which are just &lt;EM&gt;declarations&lt;/EM&gt; of methods and types (and definitions of macros). The actual &lt;EM&gt;bodies&lt;/EM&gt; of those methods and types are in completely different files.&lt;/P&gt;
&lt;P&gt;People sometimes ask me &lt;EM&gt;why doesn’t C# need header files?&lt;/EM&gt; which is a bit of an odd way to phrase the question; I would have asked the equivalent question &lt;EM&gt;why does C++ need header files?&lt;/EM&gt; Header files seem like a huge potential point of failure; all the time I edit C++ code and change the signature of a method; if I forget to update the header file, then the code doesn’t compile and often gives some cryptic error message. Hopefully this large cost actually buys you something.&lt;/P&gt;
&lt;P&gt;It buys the compiler writer one thing, and the user one thing. &lt;/P&gt;
&lt;P&gt;What it buys the user is that you can compile each individual “cpp” file into a “obj” file independently, provided that you have all the necessary headers. All the information necessary to generate the bodies that are in a given cpp file can be gleaned from the set of headers. This means that the build system can recompile just those cpp files that changed, provided that no header changed. &lt;/P&gt;
&lt;P&gt;What it buys the compiler writer is that every file can be compiled in “one pass”. Because every type and method &lt;EM&gt;declaration&lt;/EM&gt; is processed before its first &lt;EM&gt;usage&lt;/EM&gt;, the compiler can simply start from the top of the file, pull in all the included headers, and proceed from the top down, spitting out the obj file as it goes, never having to go back and revisit something its seen already.&lt;/P&gt;
&lt;P&gt;The C# language does not require that declarations occur before usages, which has two impacts, again, on the user and on the compiler writer. The impact on the user is that you don’t get to recompile just the IL that changed when you change a file; the whole assembly is recompiled. Fortunately the C# compiler is sufficiently fast that this is seldom a huge issue. (Another way to look at this is that the “granularity” of recompilation in C# is at the project level, not the file level.)&lt;/P&gt;
&lt;P&gt;The impact on the compiler writer is that we have to have a “two pass” compiler. In the first pass, we look for declarations and ignore bodies. Once we have gleaned all the information from the declarations that we would have got from the headers in C++, we take a second pass over the code and generate the IL for the bodies. &lt;/P&gt;
&lt;P&gt;A good way to think about this is that the first pass computes metadata: the “top level” stuff, like namespaces, classes, structs, enums, interfaces, delegates, methods, type parameters, formal parameters, constructors, events, attributes, and so on. The second pass computes the IL: the code that goes in the method bodies, constructor bodies, and so on.&lt;/P&gt;
&lt;P&gt;In practice, though we only do “two passes” over the &lt;EM&gt;raw text&lt;/EM&gt;, we do many, many passes over the resulting &lt;EM&gt;data structures&lt;/EM&gt;. I thought I might lay out for you just what passes we do in order to implement the C# language.&lt;/P&gt;
&lt;P&gt;The first “metadata” phase goes like this:&lt;/P&gt;
&lt;P&gt;The first thing we do is take the text of the sources and break it up into a stream of tokens. That is, we do lexical analysis to determine that "&lt;SPAN class=code&gt;class c : b { }&lt;/SPAN&gt;" is class, identifier, colon, identifier, left curly, right curly.&lt;/P&gt;
&lt;P&gt;We then do a "top level parse" where we verify that the token streams define a grammatically-correct C# program. However, we skip parsing method bodies.&amp;nbsp; When we hit a method body, we just blaze through the tokens until we get to the matching close curly. We'll come back to it later; we only care about getting enough information to generate metadata at this point.&lt;/P&gt;
&lt;P&gt;We then do a "declaration" pass where we make notes about the location of every namespace and type declaration in the program. At this point we're done looking at the source code for the first phase; every subsequent pass is over the set of "symbols" deduced from the declarations. 
&lt;P&gt;We then do a pass where we verify that all the types declared have no cycles in their base types. We need to do this first because in every subsequent pass we need to be able to walk up type hierarchies without having to deal with cycles. 
&lt;P&gt;We then do a pass where we verify that all generic parameter constraints on generic types are also acyclic. 
&lt;P&gt;We then do a pass where we check whether every member of every type -- methods of classes, fields of structs, enum values, and so on -- is consistent. No cycles in enums, every overriding method overrides something that is actually virtual, and so on. At this point we can compute the "vtable" layouts of all interfaces, classes with virtual methods, and so on. 
&lt;P&gt;We then do a pass where we work out the values of all "const" fields. 
&lt;P&gt;At this point we have enough information to emit almost all the metadata for this assembly. We still do not have information about the metadata for iterator/anonymous function closures or anonymous types; we do those late. 
&lt;P&gt;We can now start generating IL. For each method body (and properties, indexers, constructors, and so on), we rewind the lexer to the point where the method body began and parse the method body. 
&lt;P&gt;[UPDATE: A number of people have asked me about how the passes below are optimized. During the initial binding pass we make notes on things like "there's nullable arithmetic in here", or "there's an object initializer in here" and so on. When we go to do object initializer rewriting, say, we first check to see if we even have to, and skip the pass entirely if we know it will be a no-op. (And testing runs a special version of the compiler that does the pass anyway, and reports back whether it&amp;nbsp;had any effect or not, so that we can test that our pass-skipping logic is correct.)&amp;nbsp;] 
&lt;P&gt;Once the method body is parsed, we do an initial "binding" pass, where we attempt to determine the types of every expression in every statement. We then do a whole pile of passes over each method body. Again, at this point we are done looking at the source code; we're now passing over the "annotated parse tree" many times, and rewriting it as we go. 
&lt;P&gt;We first run a pass to transform loops into gotos and labels. 
&lt;P&gt;(The next few passes look for bad stuff.) 
&lt;P&gt;Then we run a pass to look for use of deprecated types, for warnings. 
&lt;P&gt;Then we run a pass that searches for uses of anonymous types that we haven't emitted metadata for yet, and emit those. 
&lt;P&gt;Then we run a pass that searches for bad uses of expression trees. For example, using a ++ operator in an expression tree. 
&lt;P&gt;Then we run a pass that looks for all local variables in the body that are defined, but not used, to report warnings. 
&lt;P&gt;Then we run a pass that looks for illegal patterns inside iterator blocks. 
&lt;P&gt;Then we run the reachability checker, to give warnings about unreachable code, and tell you when you've done something like forgotten the return at the end of a non-void method. 
&lt;P&gt;Then we run a pass that verifies that every goto targets a sensible label, and that every label is targeted by a reachable goto. 
&lt;P&gt;Then we run a pass that checks that all locals are definitely assigned before use, notes which local variables are closed-over outer variables of an anonymous function or iterator, and which anonymous functions are in reachable code. (This pass does too much. I have been meaning to refactor it for some time now.) 
&lt;P&gt;At this point we're done looking for bad stuff, but we still have way more passes to go before we sleep. 
&lt;P&gt;Next we run a pass that detects missing ref arguments to calls on COM objects and fixes them. (This is a new feature in C# 4.) 
&lt;P&gt;Then we run a pass that looks for stuff of the form "new MyDelegate(Foo)" and rewrites it into a call to CreateDelegate. 
&lt;P&gt;Then we run a pass that transforms expression trees into the sequence of factory method calls necessary to create the expression trees at runtime. 
&lt;P&gt;Then we run a pass that rewrites all nullable arithmetic into code that tests for HasValue, and so on. 
&lt;P&gt;Then we run a pass that finds all references of the form base.Blah() and rewrites them into code which does the non-virtual call to the base class method. 
&lt;P&gt;Then we run a pass which looks for object and collection initializers and turns them into the appropriate property sets, and so on. 
&lt;P&gt;Then we run a pass which looks for dynamic calls (in C# 4) and rewrites them into dynamic call sites that use the DLR. 
&lt;P&gt;Then we run a pass that looks for calls to removed methods. (That is, partial methods with no actual implementation, or conditional methods that don't have their conditional compilation symbol defined.) Those are turned into no-ops. 
&lt;P&gt;Then we look for unreachable code and remove it from the tree. No point in codegenning IL for it. 
&lt;P&gt;Then we run an optimization pass that rewrites trivial "is" and "as" operators. 
&lt;P&gt;Then we run an optimization pass that looks for switch(constant) and rewrites it as a branch directly to the correct case. 
&lt;P&gt;Then we run a pass which turns string concatenations into calls to the correct overload of String.Concat. 
&lt;P&gt;(Ah, memories. These last two passes were the first things I worked on when I joined the compiler team.) 
&lt;P&gt;Then we run a pass which rewrites uses of named and optional parameters into calls where the side effects all happen in the correct order. 
&lt;P&gt;Then we run a pass which optimizes arithmetic; for example, if we know that M() returns an int, and we have 1 * M(), then we just turn it into M(). 
&lt;P&gt;Then we do generation of the code for anonymous types first used by this method. 
&lt;P&gt;Then we transform anonymous functions in this body into methods of closure classes. 
&lt;P&gt;Finally, we transform iterator blocks into switch-based state machines. 
&lt;P&gt;Then we emit the IL for the transformed tree that we've just computed. 
&lt;P&gt;UPDATE: I've glossed over a number of additional passes that happen during IL generation. Basically, the IL generator turns the program tree into a set of "basic blocks" -- that is, sequences of code where nothing "in the middle" of the block&amp;nbsp;leaves the sequence, and no other sequences branch to "the middle" of any other sequence. Once we have the code in basic block form we can start doing additional analysis on it: rewriting branches to be more efficient, removing&amp;nbsp;basic blocks that the arithmetic optimizer has proven to be dead code,&amp;nbsp;and so on. See&lt;A href="http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx"&gt; my article on the optimize switch&lt;/A&gt; for more details of what we do during this pass. 
&lt;P&gt;Easy as pie! &lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941619" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Generation/default.aspx">Code Generation</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category></item><item><title>Style follows semantics</title><link>http://blogs.msdn.com/ericlippert/archive/2010/02/01/style-follows-semantics.aspx</link><pubDate>Mon, 01 Feb 2010 16:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941958</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>38</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941958.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941958</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Which is better style?&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;bool abc;&lt;BR&gt;if (Foo())&lt;BR&gt;&amp;nbsp; abc = Bar();&lt;BR&gt;else&lt;BR&gt;&amp;nbsp; abc = false;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;vs&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;bool abc = Foo() &amp;amp;&amp;amp; Bar();&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;To me, this comes down to the question “is Bar useful solely for obtaining its value, or also for its side effects?” &lt;STRONG&gt;The stylistic choices should typically be driven by a desire to clearly communicate the semantics of the program fragment.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The metasyntatic names are therefore making this harder to answer, not easier. Suppose the choice were in fact between:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;bool loginSuccessful;&lt;BR&gt;if (NetworkAvailable())&lt;BR&gt;&amp;nbsp; loginSuccessful= LogUserOn();&lt;BR&gt;else&lt;BR&gt;&amp;nbsp; loginSuccessful= false;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;and&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;bool loginSuccessful= NetworkAvailable() &amp;amp;&amp;amp; LogUserOn();&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;I would always choose the former, because I want LogUserOn to be in a statement of its own. Statements emphasize “I am useful for my side effects”. Statements emphasize control flow and provide convenient places to put breakpoints.&lt;/P&gt;
&lt;P&gt;If however the choice were between&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;bool canUseCloud;&lt;BR&gt;if (NetworkAvailable())&lt;BR&gt;&amp;nbsp; canUseCloud = UserHasFreeSpaceInCloud();&lt;BR&gt;else&lt;BR&gt;&amp;nbsp; canUseCloud = false;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;and&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;bool canUseCloud = NetworkAvailable() &amp;amp;&amp;amp; UserHasFreeSpaceInCloud();&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;I would always choose the latter; here we’re using &amp;amp;&amp;amp; idiomatically to compute a value safely.&lt;BR&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941958" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category></item><item><title>Calling constructors in arbitrary places</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/28/calling-constructors-in-arbitrary-places.aspx</link><pubDate>Thu, 28 Jan 2010 15:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941737</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>28</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941737.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941737</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;C# lets you call another constructor from a given constructor, but only before the body of the calling constructor runs: &lt;SPAN class=code&gt;
&lt;P&gt;public C(int x) : this(x, null)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; // …&lt;BR&gt;}&lt;BR&gt;public C(int x, string y)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; // …&lt;BR&gt;} &lt;/SPAN&gt;
&lt;P&gt;Why can you call another constructor at the beginning of a constructor block, but not at the end of the block, or in the middle of the block? 
&lt;P&gt;Well, let's break it down into two cases. (1) You're calling a "base" constructor, and (2) you're calling a "this" constructor. 
&lt;P&gt;For the "base" scenario, it's quite straightforward. You almost never want to call a base constructor after a derived constructor. That's an inversion of the normal dependency rules. Derived code should be able to depend on the base constructor having set up the "base" state of the object; the base constructor should never depend on the derived constructor having set up the derived state.&lt;/P&gt;
&lt;P&gt;Suppose you're in the second case. The typical usage pattern for this scenario is to have a bunch of constructors that take different arguments and then all "feed" into one master constructor (often private) that does all the real work. Typically the public constructors have no bodies of their own, so there's no difference between calling the other constructor "before" or "after" the empty block. 
&lt;P&gt;Suppose you're in the second case &lt;EM&gt;and&lt;/EM&gt; you are doing work in each constructor, &lt;EM&gt;and&lt;/EM&gt; you want to call other constructors at some point other than the start of the current constructor. 
&lt;P&gt;In that scenario you can easily accomplish this by extracting the work done by the different constructors into methods, and then &lt;EM&gt;calling the methods&lt;/EM&gt; in the constructors in whatever order you like. That is superior to inventing a syntax that allows you to call other constructors at arbitrary locations. There are a number of design principles that support this decision. Two are: 
&lt;P&gt;1) Having two ways to do the same thing creates confusion; it adds mental cost. We often have two ways of doing the same thing in C#, but in those situations we want the situation to "pay for itself" by having the two different ways of doing the thing each be compelling, interesting and powerful features that have clear pros and cons. (For example, "query comprehensions" vs "fluent queries" are two very different-looking ways of building a query.)&amp;nbsp; Having a way to call a constructor the way you'd call any other method seems like having two ways of doing something -- calling an initialization method -- but without a compelling or interesting "payoff".&amp;nbsp; 
&lt;P&gt;2) We'd have to add new language syntax to do it. New syntax comes at a very high cost; it's got to be designed, implemented, tested, documented -- those are our costs. But it comes at a higher cost to you because &lt;EM&gt;you have to learn what the syntax means, otherwise you cannot read or maintain other people's code&lt;/EM&gt;.&amp;nbsp; That's another cost; again, we only take the huge expense of adding syntax if we feel that there is a clear, compelling, large benefit for our customers.&amp;nbsp; I don't see a huge benefit here. 
&lt;P&gt;In short, achieving the desired construction control flow is easy to do without adding the feature, and there's no compelling benefit to adding the feature. No new interesting representational power is added to the language. &lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941737" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Language+Design/default.aspx">Language Design</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/constructors/default.aspx">constructors</category></item><item><title>Why are unused using directives not a warning?</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/25/why-are-unused-using-directives-not-a-warning.aspx</link><pubDate>Mon, 25 Jan 2010 15:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941732</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>55</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941732.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941732</wfw:commentRss><description>&lt;div class="mine"&gt; &lt;p&gt;As I’ve discussed before, we try to reserve warnings for only those situations where we can say with almost certainty that the code is broken, misleading or useless. One reason for trying to ensure that warnings are not “false positives” is that we don’t ever want to encourage someone to take working, correct code and break it so as to remove the warning. Another is that since many people compile with “warnings are errors” turned on, we do not want to introduce a whole lot of unnecessary build breakages should the warning turn out to be unwarranted.&lt;/p&gt; &lt;p&gt;An unused using directive is almost certainly not “broken”, and it’s probably not misleading. It certainly is useless though. Why not produce a warning for this, so that you can remove the useless directive from your program?&lt;/p&gt; &lt;p&gt;Suppose we make an unused using into a warnings. Suppose you do have “warnings are errors” turned on. And suppose you create a new console project in Visual Studio.&amp;nbsp; We generate you this code:&lt;/p&gt;&lt;span class="code"&gt; &lt;p&gt;using System;&lt;br&gt;using System.Text;&lt;br&gt;using System.Linq;  &lt;p&gt;class Program&lt;br&gt;{&lt;br&gt;&amp;nbsp; static void Main(string[] arguments)&lt;br&gt;&amp;nbsp; {&lt;br&gt;&amp;nbsp; }&lt;br&gt;} &lt;/span&gt; &lt;p&gt;… which doesn’t compile. Which is better, generating code that doesn't compile, or omitting the using directives and making you type "using System;" the first time you try to use Console.WriteLine?  &lt;p&gt;Either way is a really bad user experience. So, no such feature. &lt;/p&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941732" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/using+directive/default.aspx">using directive</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/warnings/default.aspx">warnings</category></item><item><title>What’s the difference between a destructor and a finalizer?</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/21/what-s-the-difference-between-a-destructor-and-a-finalizer.aspx</link><pubDate>Thu, 21 Jan 2010 14:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941672</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>17</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941672.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941672</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Today, another dialogue, and another episode of my ongoing series "what's the difference?"&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What’s the difference, if any, between a “destructor” and a “finalizer”?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Both are mechanisms for cleaning up a resource when it is no longer in use. When I was asked this, at first I didn’t think there was a difference. But some Wikipedia searches turned up a difference; the term “destructor” is typically used to mean a &lt;STRONG&gt;deterministically-invoked&lt;/STRONG&gt; cleanup, whereas a “finalizer” runs when the &lt;STRONG&gt;garbage collector&lt;/STRONG&gt; says to run it.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Doesn’t that mean that the C# spec uses the term “destructor” incorrectly?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Yes, by these definitions, the C# spec gets it wrong. What we call a “destructor” in the spec is actually a finalizer, and what we call the “Dispose()” method invoked by a “using” statement is in fact a “destructor”. &lt;/P&gt;
&lt;P&gt;The CLI spec calls the finalizer by its right name.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Why did the authors of the C# spec get it wrong?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I don't know, but I can guess. I have two guesses. 
&lt;P&gt;Guess #1 is that on May 12th, 1999 there was not a Wikipedia article clearly describing the subtle difference between these two concepts. That's because there wasn't a Wikipedia. Remember back when there wasn't a Wikipedia? Dark ages, man. The error might simply have been an honest mistake, believing that the two terms were identical. 
&lt;P&gt;Heck, for all I know, the two terms were identical on May 12th, 1999, and the difference in definitions only evolved later, as it became obvious that there was a need to disambiguate between eager/deterministic and lazy/nondeterministic cleanup methods. Anyone who has more historical perspective on this than I do, feel free to chime in here. 
&lt;P&gt;Guess #2 is that on May 12th, 1999, the language design committee wished to leave open the possibility that a C# "destructor" could be implemented as something other than a CLR finalizer. That is, the "destructor" was designed to be a C# language concept that did not necessarily map one-to-one with the CLR’s "finalizer" concept. 
&lt;P&gt;When designing a language at the same time as the framework it sits atop is also being designed, sometimes you want to insulate yourself against late-breaking design changes in your subsystems. Deliberately preventing name conflation is one way to do that. 
&lt;P&gt;&lt;STRONG&gt;What’s your sudden obsession with May 12th, 1999 about?&lt;/STRONG&gt; 
&lt;P&gt;The language committee's notes for May 12th 1999 read in part: 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;We're going to use the term "destructor" for the member which executes when an instance is reclaimed. Classes can have destructors; structs can't. Unlike in C++, a destructor cannot be called explicitly. Destruction is non-deterministic – you can't reliably know when the destructor will execute, except to say that it executes at some point after all references to the object have been released. The destructors in an inheritance chain are called in order, from most descendant to least descendant.&amp;nbsp; There is no need (and no way) for the derived class to explicitly call the base destructor. The C# compiler compiles destructors to the appropriate CLR representation.&amp;nbsp; For this version that probably means an instance finalizer that is distinguished in metadata.&amp;nbsp; &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Notice that this supports my hypothesis that the language design team was attempting to insulate themselves from becoming tied to a particular CLR term.&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941672" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Memory+Management/default.aspx">Memory Management</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Dialogue/default.aspx">Dialogue</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/What_2700_s+The+Difference_3F00_/default.aspx">What's The Difference?</category></item><item><title>A Definite Assignment Anomaly</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/18/a-definite-assignment-anomaly.aspx</link><pubDate>Mon, 18 Jan 2010 14:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941761</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941761.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941761</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE: I have discovered that this issue is considerably weirder than the initial bug report led me to believe.&amp;nbsp;I've rewritten the examples in this article; the previous ones did not actually demonstrate the bug.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Consider the following code:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;struct S &lt;BR&gt;{&lt;BR&gt;&amp;nbsp; private&amp;nbsp;string blah;&lt;BR&gt;&amp;nbsp; public S(string blah)&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.blah = blah;&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;&amp;nbsp; public void Frob()&lt;BR&gt;&amp;nbsp; { // whatever&lt;BR&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;This method body code fragment is legal (though probably ill-advised):&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;S s1&amp;nbsp;= new S();&lt;BR&gt;s1.Frob();&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Every struct has a default constructor which initializes its fields to their default values; this frobs the zero handle.&lt;/P&gt;
&lt;P&gt;What about this?&lt;/P&gt;&lt;SPAN class=code&gt;S s2;&lt;BR&gt;s2.Frob();&lt;/SPAN&gt; 
&lt;P&gt;Looks like a definite assignment error, doesn’t it? &lt;/P&gt;
&lt;P&gt;An interesting and little-known fact about the C# compiler is that this is reported as a definite assignment error if the struct is compiled from &lt;STRONG&gt;source code&lt;/STRONG&gt;, but not if the struct is in a referenced assembly! What is the reasoning behind this seemingly anomalous behaviour?&lt;/P&gt;
&lt;P&gt;Well, consider the following related situation:&lt;/P&gt;&lt;SPAN class=code&gt;struct SS&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; public int x;&lt;BR&gt;&amp;nbsp; public int y;&lt;BR&gt;&amp;nbsp; public void Bar() {…}&lt;BR&gt;} &lt;/SPAN&gt;
&lt;P&gt;Here we have the pure unadulterated evil that is a mutable value type. Is this legal?&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;SS ss;&lt;BR&gt;ss.x = 123;&lt;BR&gt;ss.y = 456;&lt;BR&gt;ss.Bar();&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Yes, that is legal. The local variable s essentially has two sub-variables, x and y. If x and y are both definitely assigned, then s is also considered to be definitely assigned. &lt;STRONG&gt;When checking definite assignment on a struct, the compiler actually checks to see whether we know that every &lt;EM&gt;field&lt;/EM&gt; of the struct has been initialized.&lt;/STRONG&gt; A constructor call is assumed to automatically initialize every field, but if there is not a constructor call, then we simply track every field independently and then pool the results. (And if the fields are themselves of value type, we do so recursively of course.)&lt;/P&gt;
&lt;P&gt;The anomaly happens because when the compiler sees that the type in question is in source code, it tracks the definite assignment of all the fields, detects that “s1.blah” is an unassigned field, and gives a definite assignment error on s1. But when the type in question is from a referenced assembly, &lt;STRONG&gt;the compiler ignores inaccessible fields of reference type&lt;/STRONG&gt;. We see that there were zero fields initialized out of the zero accessible fields that needed to be initialized, and declare that s1 must be fully assigned!&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE: Initially I thought this behaviour was odd and undesirable but plausibly justifiable. However I have since learned that in fact, for reasons still obscure to me, the compiler only ignores inaccessible fields of &lt;EM&gt;reference &lt;/EM&gt;type. Inaccessible fields of &lt;EM&gt;value&lt;/EM&gt; type are still tracked for definite assignment. This weird inconsistency points strongly in the direction of this being a bug, not a feature. Continuing with my original text...&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;This behaviour is undoubtedly &lt;EM&gt;weird&lt;/EM&gt;; whether it is &lt;EM&gt;desirable&lt;/EM&gt; or not is a judgment call. There certainly is an argument to be made that the user has no ability to do anything about a private field on an imported type; a type they almost certainly did not author themselves, do not know the details of, and cannot change. Whereas if the type is in source code, then the user running the compiler has personal knowledge of its internals. So, the argument goes, we should give the user a pass on having to prove that the fields they cannot access and know nothing about are assigned; just let them be assigned to their default values and don’t worry about them.&lt;/P&gt;
&lt;P&gt;There is also the (more compelling to me) argument that we ought to be consistent here and either consider &lt;EM&gt;all&lt;/EM&gt; &lt;EM&gt;the fields&lt;/EM&gt; all the time, or consider &lt;EM&gt;only the accessible fields&lt;/EM&gt; all the time, rather than swapping back and forth between the two choices depending on the details of the compilation process. My choice would be the former; force the user to call the default constructor or use the “default(T)” operator if they really want the all-zero default version of the struct. That makes the intention very clear in the code.&lt;/P&gt;
&lt;P&gt;Unfortunately, though I think I’ve argued that this behaviour is plausibly undesirable, we’re stuck with it. There are numerous BCL structs which have no public fields, and &lt;EM&gt;lots&lt;/EM&gt; of existing code which uses them without calling the default constructor. Changing now to make this an error is a breaking change with no compelling benefit, and we’re not going to do that. Particularly since the anomaly is essentially harmless; whether we force you to say “s1 = new S()” or not, the handle field is in practice always initialized to its default value. &lt;/P&gt;
&lt;P&gt;Nor do I think we should change this by making the code legal in all cases, even if the type is in source code; that’s making a bad situation worse merely in order to achieve consistency. This seems like the very definition of “foolish consistency”.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941761" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Breaking+Changes/default.aspx">Breaking Changes</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Value+Types/default.aspx">Value Types</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/definite+assignment/default.aspx">definite assignment</category></item><item><title>Why Can't I Access A Protected Member From A Derived Class? Part Six</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/14/why-cant-i-access-a-protected-member-from-a-derived-class-part-six.aspx</link><pubDate>Thu, 14 Jan 2010 14:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941634</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>21</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941634.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941634</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Reader Jesse McGrew asks an excellent follow-up question to &lt;A href="http://blogs.msdn.com/ericlippert/archive/tags/protected/default.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/tags/protected/default.aspx"&gt;my 2005 post about why you cannot access a protected member from a derived class&lt;/A&gt;. (You probably want to re-read that post in order to make sense of this one.)&lt;/P&gt;
&lt;P&gt;I want to be clear in my terminology, so I’m going to define some terms. Suppose we have a call foo.Bar() inside class C. The value of foo is the “receiver” of the call. The compile-time type of foo is the “compile time type of the receiver”. The “runtime type of the receiver” could potentially be more derived. The “call site type” is C. &lt;/P&gt;
&lt;P&gt;The rule in C# is that if Bar is protected, then the compile-time type of the receiver must be the call site type, or a type derived from the call site type.&amp;nbsp; It cannot be a base type of the call site type, because then the runtime type of the receiver might have a “cousin” (or sibling, or uncle… but let’s not split hairs, let’s just call them all cousins) relationship to the call site type, rather than an “ancestor/descendant” relationship.&lt;/P&gt;
&lt;P&gt;Jesse quite rightly points out that my original answer to the question was not really complete. There are two questions unanswered:&lt;/P&gt;
&lt;P&gt;1) Why would it be a bad thing to allow calling a protected method from a receiver whose runtime type is a “cousin” class?&lt;/P&gt;
&lt;P&gt;2) Supposing for the sake of argument that there is a good answer to (1) – why doesn’t that argument apply equally well to calling a protected method via a receiver whose compile-time type is the same as the call site type? The runtime type of the receiver still could be more derived.&lt;/P&gt;
&lt;P&gt;I’ll answer the first question first. In this answer I’m going to use a humorous and exaggerated example to illustrate the problem; I want to emphasize that this is &lt;STRONG&gt;not&lt;/STRONG&gt; how you should actually design applications that need “big S” Security, like bank accounts. What I want to illustrate here is simply that allowing protected methods to be called via “cousins” makes it difficult to maintain invariants and therefore difficult to write correct, robust code.&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;// Good.dll:&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public abstract class BankAccount&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; abstract protected void DoTransfer(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BankAccount destinationAccount,&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User authorizedUser,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; decimal amount);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public abstract class SecureBankAccount : BankAccount&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected readonly int accountNumber;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public SecureBankAccount(int accountNumber)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.accountNumber = accountNumber;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Transfer(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BankAccount destinationAccount,&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User authorizedUser,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; decimal amount)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!Authorized(user, accountNumber)) throw something;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.DoTransfer(destinationAccount, user, amount);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public sealed class SwissBankAccount : SecureBankAccount&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public SwissBankAccount(int accountNumber) : base(accountNumber) {}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; override protected void DoTransfer(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BankAccount destinationAccount,&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User authorizedUser,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; decimal amount)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Code to transfer money from a Swiss bank account here.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // This code can assume that authorizedUser is authorized.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // We are guaranteed this because SwissBankAccount is sealed, and&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // all callers must go through public version of Transfer from base&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // class SecureBankAccount.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Evil.exe:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; class HostileBankAccount : BankAccount&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; override protected void Transfer(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BankAccount destinationAccount,&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User authorizedUser,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; decimal amount) {&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void Main()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User drEvil = new User("Dr. Evil");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BankAccount yours = new SwissBankAccount(1234567);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BankAccount mine = new SwissBankAccount(66666666);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yours.DoTransfer(mine, drEvil, 1000000.00m); // compilation error&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // You don't have the right to access the protected member of&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // SwissBankAccount just because you are in a &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // type derived from BankAccount. &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Dr. Evil's attempt to steal &lt;A href="http://www.youtube.com/watch?v=cKKHSAE1gIs" mce_href="http://www.youtube.com/watch?v=cKKHSAE1gIs"&gt;ONE... MILLION... DOLLARS&lt;/A&gt;... from your Swiss bank account has been foiled by the C# compiler. 
&lt;P&gt;Obviously this is a silly example, and obviously, fully-trusted code could do anything it wants to your types -- fully-trusted code can start up a debugger and change the code as its running. Full trust means &lt;EM&gt;full trust&lt;/EM&gt;. Again, do not actually design a real security system this way!&amp;nbsp; 
&lt;P&gt;But my point is simply that the "attack" that is foiled here is&lt;STRONG&gt; someone attempting to do an end-run around the invariants set up by SecureBankAccount in order to access the code in SwissBankAccount directly. &lt;/STRONG&gt;
&lt;P&gt;The second question is "Why doesn't SecureBankAccount also have this restriction?"&amp;nbsp; In my example, SecureBankAccount says “this.DoTransfer(destinationAccount, user, amount);” 
&lt;P&gt;Clearly "this" is of type SecureBankAccount or something more derived. It could be any value of a more derived type, including a new SwissBankAccount. Doesn’t the same concern apply? &lt;STRONG&gt;Couldn't SecureBankAccount be doing an end-run around SwissBankAccount's invariants?&lt;/STRONG&gt; 
&lt;P&gt;Yes, absolutely! And because of that, &lt;EM&gt;the authors of SwissBankAccount are required to understand and approve of everything that their base class does!&lt;/EM&gt;&amp;nbsp; You can't just go deriving from some class willy-nilly and hope for the best. The implementation of your base class is allowed to call the set of protected methods exposed by the base class. If you want to derive from it then you are required to read the documentation for that class, or the code, and understand under what circumstances your protected methods will be called, and write your code accordingly. &lt;STRONG&gt;Derivation is a way of sharing implementation details; if you don't understand the implementation details of the thing you are deriving from then don't derive from it.&lt;/STRONG&gt; 
&lt;P&gt;And besides, the base class is always written &lt;EM&gt;before&lt;/EM&gt; the derived class. The base class isn't up and changing on you, and presumably you trust the author of the class to not attempt to break you sneakily with a future version. (Of course, a change to a base class can always cause problems; this is yet another version of the &lt;A href="http://blogs.msdn.com/ericlippert/archive/tags/Brittle+Base+Classes/default.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/tags/Brittle+Base+Classes/default.aspx"&gt;brittle base class&lt;/A&gt; problem.) 
&lt;P&gt;The difference between the two cases is that when you derive from a base class, you have the behaviour of &lt;EM&gt;one&lt;/EM&gt; class &lt;EM&gt;of your choice&lt;/EM&gt; to understand and approve of. That is a tractable amount of work. The authors of SwissBankAccount are required to precisely understand what SecureBankAccount guarantees to be invariant before the protected method is called. But they should not have to understand and trust every possible behaviour of every possible cousin class that just happens to be derived from the same base class. Those guys could be implemented by anyone and do anything. You would have no ability whatsoever to understand any of their pre-call invariants, and therefore you would have no ability to successfully write a working protected method. Therefore, we save you that bother and disallow that scenario. 
&lt;P&gt;And besides, we &lt;EM&gt;have&lt;/EM&gt; to allow you to call protected methods on receivers of potentially more-derived classes. Suppose we didn't allow that and deduce something absurd. Under what circumstances could a protected method ever be called, if we disallowed calling protected methods on receivers of potentially-more-derived classes?&amp;nbsp; The only time you could ever call a protected method in that world is if you were calling your own protected method from a sealed class! Effectively, protected methods could almost never be called, and the implementation that was called would always be the most derived one. What's the point of "protected" in that case? Your "protected" means the same thing as "private, and can only be called from a sealed class". That would make them rather less useful. 
&lt;P&gt;So, the short answer to both questions is "because if we didn't do that, it would be impossible to use protected methods at all."&amp;nbsp; We restrict calls through less-derived receiver compile-time types because if we don't, it's impossible to safely implement any protected method that depends on an invariant.&amp;nbsp; We allow calls through potential subtypes because if we do not allow this, then we don't allow hardly any calls at all. 
&lt;P&gt;Finally, an unasked question: what if you are the person writing the base class with a protected method? Essentially you are in the same boat as the person writing any &lt;EM&gt;public&lt;/EM&gt; or &lt;EM&gt;virtual&lt;/EM&gt; method; then you have to accept that anyone can call your public method in any way it chooses, or that derived class can override your virtual method and make it do something completely different, at their discretion. &lt;STRONG&gt;If you write a protected method, you have to accept that any derived class can call that method in any way it chooses and write the base class code accordingly.&lt;/STRONG&gt; 
&lt;P&gt;When you write a public method, you have to consider the consequences of bad callers; if there are ways that bad callers can misuse your public method, then you need to consider the cost to the user vs the cost of hardening the method against the potentially bad caller. The same is true of virtual methods; you have to consider the consequences of bad overriders. And the same is true of protected methods; you have to consider the consequence of bad derived classes calling your code. 
&lt;P&gt;Designing robust code that has public methods is hard. Designing code that is robust and has virtual or protected methods is even harder; designing for extendability is a difficult problem in general, and shouldn’t be taken on lightly. &lt;EM&gt;Consider sealing classes that aren’t designed for extension.&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941634" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Breaking+Changes/default.aspx">Breaking Changes</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Brittle+Base+Classes/default.aspx">Brittle Base Classes</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/protected/default.aspx">protected</category></item><item><title>Continuing to an outer loop</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/11/continuing-to-an-outer-loop.aspx</link><pubDate>Mon, 11 Jan 2010 14:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941967</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>37</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941967.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941967</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;When you have a nested loop, sometimes you want to “continue” the outer loop, not the inner loop. For example, here we have a sequence of criteria and a sequence of items, and we wish to determine if there is any item which matches every criterion:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;match = null;&lt;BR&gt;foreach(var item in items)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; foreach(var criterion in criteria)&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!criterion.IsMetBy(item))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // No point in checking anything further; this is not&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a match. We want to “continue” the outer loop. How?&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;&amp;nbsp; match = item;&lt;BR&gt;&amp;nbsp; break;&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;There are many ways to achieve this. Here are a few, in order of increasing awesomeness:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Technique #1 (ugly):&lt;/STRONG&gt; When you have a loop, the “continue” statement is essentially a “goto” which branches to the bottom of the loop, just before it does the loop condition test and pops back up to the top of the loop. (Apparently when you spell “goto” as “continue”, the “all gotos are all evil all the time” crowd doesn’t bother you as much.) You can of course simply make this explicit:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;match = null;&lt;BR&gt;foreach(var item in items)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; foreach(var criterion in criteria)&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!criterion.IsMetBy(item))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; goto OUTERCONTINUE;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;&amp;nbsp; match = item;&lt;BR&gt;&amp;nbsp; break;&lt;BR&gt;OUTERCONTINUE:&lt;BR&gt;&amp;nbsp; ;&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;STRONG&gt;Technique #2 (better):&lt;/STRONG&gt; When I see a nested loop, I almost always consider refactoring the interior loop into a method of its own.&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;match = null;&lt;BR&gt;foreach(var item in items)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; if (MeetsAllCriteria(item, critiera))&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; match = item;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;where the body of MeetsAllCriteria is obviously&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;foreach(var criterion in criteria)&lt;BR&gt;&amp;nbsp; if (!criterion.IsMetBy(item))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;BR&gt;return true;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;STRONG&gt;Technique #3 (awesome):&lt;/STRONG&gt; The “mechanism” code here is emphasizing how the code works and hiding the &lt;EM&gt;meaning&lt;/EM&gt; of the code. The &lt;EM&gt;purpose&lt;/EM&gt; of the code is to answer the question “what is the first item, if any, that meets all the criteria?” If that’s the meaning of the code, then write code that reads more like that meaning:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;var matches = from item in items &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where criteria.All(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; criterion=&amp;gt;criterion.IsMetBy(item)) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select item;&lt;BR&gt;match = matches.FirstOrDefault();&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;That is, search the items for items that meet every criterion, and take the first of them, if there is one. The code now reads like what it is supposed to be implementing, and of course, if there are no loops in the first place then there’s no need to “continue” at all!&lt;/P&gt;
&lt;P&gt;The biggest thing I love about LINQ is that it enables a whole new way to think about problem solving in C#. It’s gotten to the point now that whenever I write a loop, I stop and think about two things before I actually type “for”:&lt;/P&gt;
&lt;P&gt;* Have I described anywhere what this code is doing &lt;STRONG&gt;semantically&lt;/STRONG&gt;, or does the code emphasize more what &lt;STRONG&gt;mechanisms&lt;/STRONG&gt; I am using at the expense of obscuring the semantics?&lt;/P&gt;
&lt;P&gt;* Is there any way to characterize the solution to my problem as the result of a &lt;STRONG&gt;query against a data source&lt;/STRONG&gt;, rather than as the result of &lt;STRONG&gt;a set of steps to be followed procedurally&lt;/STRONG&gt;?&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941967" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/goto/default.aspx">goto</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/queries/default.aspx">queries</category></item><item><title>Is there such a thing as too much precision?</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/07/is-there-such-a-thing-as-too-much-precision.aspx</link><pubDate>Thu, 07 Jan 2010 15:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9937135</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9937135.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9937135</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Well, enough chit-chat, back to programming language design.&lt;/P&gt;
&lt;P&gt;Suppose you’re building electronic piano software. As we’ve discussed before, the “equal temperament” tuning for a piano goes like this: the 49th note from the left on a standard 88 key piano is A, and its frequency is 440 Hz. Each octave above or below that doubles or halves the frequency. Why? Because humans perceive the ratio between two frequencies as the relevant factor, not the (subtractive) difference. There are twelve semitones in the chromatic scale, so to make each one sound like it has the same relationship with its previous note, each one multiplies the frequency of the previous one by the twelfth root of two. &lt;/P&gt;
&lt;P&gt;In short, the frequency of the nth key is 440 x 2 &lt;SUP&gt;(n-49) / 12&lt;/SUP&gt;&lt;/P&gt;
&lt;P&gt;Now, on a real piano, its a bit more complicated than that. The high notes have some interesting problems associated with them. For the long strings in the middle of the piano, the ratio of the diameter of the string to its length is really small; zero, for practical purposes. But the short strings at the high end are short enough that the ratio is no longer practically zero. The thickness of the string causes its harmonic vibrations to be flatter than they ought to be; this is called “inharmonicity”. In addition, humans tend to perceive high notes as being flatter than they really are; our ability to determine relative pitches is somewhat skewed at the high end. For both these reasons, piano tuners often “stretch” the octaves at the high end, making the fundamental frequencies of the high notes slightly sharper than the equal temperament would suggest. Obviously electronic pianos do not have inharmonicity, and we’ll ignore the psychoacoustic problems for our purposes. Because I actually want to talk about floating point arithmetic.&lt;/P&gt;
&lt;P&gt;Suppose you say “our expression above is equivalent to 440 x R&lt;SUP&gt;(n-49) &lt;/SUP&gt;where R is the twelfth root of two. You compute the 12th root of 2 using calc.exe, and write some code:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;static double Frequency(int key)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; const double TwelfthRootOfTwo = 1.0594630943592952645618252949463;&lt;BR&gt;&amp;nbsp; const double A4Frequency = 440.0;&lt;BR&gt;&amp;nbsp; return A4Frequency * Math.Pow(TwelfthRootOfTwo, key-49);&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Now, this code is fine, it works, and there’s no need to mess with it. It might be nice to validate the parameter to make sure that its between 1 and 88, but, whatever. However, does it strike you as odd how much precision is in the constant? There are about 20 more decimal digits of precision in there than can be represented in a double! A double is only accurate to around 14 or 15 digits of precision. &lt;/P&gt;
&lt;P&gt;One thing that we can deduce from this is that calc.exe does not do its calculations in doubles, which I suppose is interesting. It must use some much higher-precision internal format. &lt;/P&gt;
&lt;P&gt;Another thing we can note is that for this application, even fifteen digits is way more precision than we need. The highest value we’re going to get out of this is less than 4200 Hz; a rounding error after the fifteenth digit is going to be far, far too small for a human to hear the difference. If you divide an octave up into 1200 notes instead of 12, each note is called a “cent” (because 100 of them fit into a semitone). Most humans cannot hear a difference of less than a handful of cents, and this is plenty of precision to be accurate to within a few cents.&lt;/P&gt;
&lt;P&gt;But if the compiler is simply throwing away all that precision, then that portion of the program is essentially meaningless. Shouldn’t the compiler be warning you that it is&amp;nbsp; throwing away your precision?&lt;/P&gt;
&lt;P&gt;Well, no, not really. And here’s why.&lt;/P&gt;
&lt;P&gt;Suppose you say&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;const double d = 108.595;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;We wish to represent the exact mathematical value 108595 / 1000 in a double. But doubles use binary arithmetic; we need a power of two on the denominator, not a power of ten. The closest we can get using the 52 precision bits of a double is 1101100.1001100001010001111010111000010100011110101110 or, if you prefer fractions, that’s 3820846886986711 / 35184372088832. That fraction has the exact value in decimal of 108.594999999999998863131622783839702606201171875, which you must admit is awfully close to 108.595, though a touch lower than it.&lt;/P&gt;
&lt;P&gt;So here’s the thing. You want to represent the exact value of 108.595, but we don’t let you; we make you accrue some error. And we don’t warn about that; we assume that you know that some error will be accrued. (If you wanted an exact result then you should have used decimal, not double.) But what if the exact value you actually wanted to represent was in fact 108.594999999999998863131622783839702606201171875 ?&amp;nbsp; That number &lt;EM&gt;can&lt;/EM&gt; be represented in binary &lt;EM&gt;exactly&lt;/EM&gt;! We certainly should not give a warning that you have too much precision if you say&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;const double d = 108.594999999999998863131622783839702606201171875;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;because you have &lt;EM&gt;exactly the right amount of precision in order to have zero representation error. &lt;/EM&gt;It seems wacky to say “please round that off to fifteen decimal digits so that we can give you this exact value right back again.” Why should we punish you for being &lt;EM&gt;more&lt;/EM&gt; accurate?&lt;/P&gt;
&lt;P&gt;The best we could do is give a warning when there was more precision than could be represented, *and* the representation error was non-zero, *and* losing the extra precision makes the representation error &lt;EM&gt;smaller&lt;/EM&gt;, *and*… now we’ve written a whole lot of complicated math code, the result of which makes the user scratch their head and say “how the heck am I supposed to make the compiler happy with this constant?” Warnings that are nothing but a source of pain are bad warnings, and we try to avoid them.&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9937135" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Floating+Point+Arithmetic/default.aspx">Floating Point Arithmetic</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Music/default.aspx">Music</category></item><item><title>First Cousins Once Removed</title><link>http://blogs.msdn.com/ericlippert/archive/2010/01/04/first-cousins-once-removed.aspx</link><pubDate>Mon, 04 Jan 2010 15:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9941664</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>26</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9941664.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9941664</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Happy New Year all, and welcome to 2010, or, as my friend Professor Orbifold prefers it, MMX. I hope your festive holiday season was as festive and enjoyable as mine. &lt;/P&gt;
&lt;P&gt;The extended Lippert family continues to grow; this year at the annual Boxing Day party we needed two overflow tables for dinner instead of the usual one. The older cousins are now all married, some of them are&amp;nbsp;bringing new babies, and&amp;nbsp;some of the younger cousins are starting to bring boyfriends and girlfriends. I pointed out that the youngest person at the table was my brand-new “first cousin once removed”, which touched off a long discussion of how exactly one computes degree of cousin-hood and removed-ness. As a public service, here’s how it works. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Recursive explanation for computer programmers and mathematicians:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Base case&lt;/STRONG&gt;: if X and Y are zeroth cousins no times removed then X and Y are siblings.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Recursive case 1&lt;/STRONG&gt;: if n &amp;gt; 0 and X and Y are nth cousins no times removed then X.Parent and Y.Parent are (n-1)th cousins no times removed.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Recursive case 2&lt;/STRONG&gt;: if m &amp;gt; 0 and X and Y are nth cousins m times removed then WOLOG assume that X is of a later generation than Y. In this case, X.Parent and Y are nth cousins (m-1) times removed.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Explanation for normal people:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Take two different&amp;nbsp;people who have a common ancestor but who are not related by direct succession (that is, neither is&amp;nbsp;the mother, father, grandmother, and so on, of the other):&lt;/P&gt;
&lt;P&gt;Degree of cousin-hood is the &lt;STRONG&gt;minimum&lt;/STRONG&gt; of the numbers of generations back that you have to go to find the nearest common ancestor, minus one.&lt;/P&gt;
&lt;P&gt;Removed-ness is the &lt;STRONG&gt;absolute difference&lt;/STRONG&gt; between the numbers of generations back you have to go to find the nearest common ancestor.&lt;/P&gt;
&lt;P&gt;For example, consider this fragment of a family tree:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Mary&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; \&lt;BR&gt;&amp;nbsp;&amp;nbsp; Laura&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Bob&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;BR&gt;&amp;nbsp;&amp;nbsp; John&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Helen&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;BR&gt;&amp;nbsp; Xerxes&amp;nbsp;&amp;nbsp; Melinda&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Take Helen and Xerxes for example. Their nearest common ancestor is Mary. Helen has to go back two generations to get to Mary. Xerxes has to go back three. The minimum of two and three, minus one, is one. The absolute difference of two and three is one. So Helen and Xerxes are first cousins, once removed. So are John and Melinda.&lt;/P&gt;
&lt;P&gt;“Zeroth cousins” are not called cousins, but have special names for the relationship. “Zeroth cousins no times removed” are of course brothers and sisters. “Zeroth cousins once removed” are uncles, aunts, nieces and nephews. “Zeroth cousins twice removed” are great-uncles, great-aunts, great-nieces and great-nephews. (Or sometimes grand-uncles, and so on.)&lt;/P&gt;
&lt;P&gt;Summing up this family tree:&lt;/P&gt;
&lt;P&gt;Mary is Laura and Bob’s mother, John and Helen’s grandmother, and Xerxes and Melinda’s great-grandmother.&lt;/P&gt;
&lt;P&gt;Laura is Mary’s daughter, Bob’s sister, John’s mother, Helen’s aunt,&amp;nbsp; Xerxes’ grandmother, and Melinda’s great-aunt.&lt;/P&gt;
&lt;P&gt;Bob is Mary’s son, Laura’s brother, John’s uncle, Helen’s father, Xerxes’ great-uncle, and Melinda’s grandfather.&lt;/P&gt;
&lt;P&gt;John is Mary’s grandson, Laura’s son, Bob’s nephew, Helen’s first cousin (no times removed), Xerxes’ father, and Melinda’s first cousin once removed.&lt;/P&gt;
&lt;P&gt;Helen is Mary’s granddaughter, Laura’s niece, Bob’s daughter, John’s first cousin (no times removed), Xerxes’ first cousin once removed, and Melinda’s mother.&lt;/P&gt;
&lt;P&gt;Xerxes is Mary’s great-grandson, Laura’s grandson, Bob’s great-nephew, John’s son, Helen’s first cousin once removed, and Melinda’s second cousin (no times removed). &lt;/P&gt;
&lt;P&gt;Melinda is Mary’s great-granddaughter, Laura’s great-niece, Bob’s granddaughter, John’s first cousin once removed, Helen’s daughter, and Xerxes’ second cousin (no times removed).&lt;/P&gt;
&lt;P&gt;Of course, this is all perfectly straightforward. If, say, your mother dies and your father marries her sister and has more children, then working out who is whose cousin can get rather trickier. And heaven only knows what happens &lt;A href="http://www.youtube.com/watch?v=eYlJH81dSiw&amp;amp;feature=fvw" mce_href="http://www.youtube.com/watch?v=eYlJH81dSiw&amp;amp;feature=fvw"&gt;if you are your own grandfather&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Incidentally, it is &lt;EM&gt;illegal&lt;/EM&gt; in the state of Washington for a man to marry his widow’s sister. Anyone care to guess why?&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9941664" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Non-computer/default.aspx">Non-computer</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/bad+jokes/default.aspx">bad jokes</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/cousins/default.aspx">cousins</category></item><item><title>It's the most wonderful time of the year</title><link>http://blogs.msdn.com/ericlippert/archive/2009/12/26/it-s-the-most-wonderful-time-of-the-year.aspx</link><pubDate>Sat, 26 Dec 2009 16:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9934105</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9934105.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9934105</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Here's a little holiday cheer for you all. Or, at least for you all in Commonwealth countries.&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;static object&amp;nbsp;M&amp;lt;T&amp;gt;(T t) where T : struct&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; return t;&lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;int ii = 10;&lt;BR&gt;int? jj = 20;&lt;BR&gt;object xx = ii;&lt;BR&gt;object yy = jj;&lt;BR&gt;System.ValueType zz = ii;&lt;BR&gt;IComparable aa = ii;&lt;BR&gt;System.Enum bb = MidpointRounding.ToEven;&lt;BR&gt;object cc = M(ii);&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;I hope you're having a festive holiday season. Happy Boxing Day, and we'll see you in the New Year for more fabulous adventures!&lt;/P&gt;
&lt;P&gt;[Eric is on vacation; this posting is pre-recorded.]&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9934105" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Conversions/default.aspx">Conversions</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/bad+jokes/default.aspx">bad jokes</category></item><item><title>Use the right tool for the job</title><link>http://blogs.msdn.com/ericlippert/archive/2009/12/14/use-the-right-tool-for-the-job.aspx</link><pubDate>Mon, 14 Dec 2009 14:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9933799</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>18</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9933799.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9933799</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Consider the following scheme:&lt;/P&gt;
&lt;P&gt;I have some client software which I sell. When the client software starts up for the first time, it obtains a “token” from the user. The token string can be anything; the user can choose their name, their cat’s name, their password, the contents of some disk file, whatever. How the client software obtains the token from the user is not particularly relevant; perhaps they type it in, perhaps it is set in a registry key, but whatever it is, it is the user’s choice what information they use to identify themselves.&lt;/P&gt;
&lt;P&gt;Anyway, the first time the client software starts up it obtains the token, and then phones home over the internet to my server. My server does some unspecified expensive and painful process by which I satisfy myself that the user is authorized to use my client software. (Assume for the sake of argument that such a process exists.) &lt;/P&gt;
&lt;P&gt;I encrypt the token message with my RSA private key and send it back to the client. The client writes the encrypted message out to disk.&lt;/P&gt;
&lt;P&gt;I do not want to go through the expensive and painful process of contacting the server again every subsequent time the software starts up. Instead, the client software decrypts the stored encrypted token using my RSA public key, and then compares it byte-by-byte to the user’s token. (Again, perhaps they type it in, or perhaps it is in a file or registry key, or whatever.) If the encrypted token matches the decrypted token then I know that I’ve authorized this user at least once already, so I don’t need to do it again.&lt;/P&gt;
&lt;P&gt;If the token cannot be authenticated then I go into some error path and deny access to the software.&lt;/P&gt;
&lt;P&gt;Does any of this seem like a good idea to you? Because &lt;EM&gt;none of this seems like a good idea to me.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The &lt;EM&gt;fundamental&lt;/EM&gt; problem here is that I am attempting to build my own security system instead of purchasing a solution off the shelf that was written by security experts. There are plenty of third-party software licensing solutions available; just buy one.&lt;/P&gt;
&lt;P&gt;But it gets worse. Consider the attacks that are possible against this system.&lt;/P&gt;
&lt;P&gt;First off, clearly there is nothing stopping any hostile client from simply decompiling my client software, removing the security checks, recompiling, and partying on. &lt;EM&gt;The premise of the scenario is that the client is hostile; doing the security check on the client and trusting the result seems ill-advised.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Second, suppose the attacker simply obtains a valid encrypted token once, and then puts the pair up on the internet. Any hostile user can now use the unmodified software without contacting the server. If I notice, I suppose I can check my records to see who put their token up on the internet and sue that person. Or can I? As we’ll see later, a clever attacker can get around this.&lt;/P&gt;
&lt;P&gt;Third, suppose there is a hostile eavesdropper Eve, listening in on the connection between a benign client (Alice) and the server (Bob). Alice’s token goes out, the encrypted token comes back, and the eavesdropper has now captured enough information to use the software.&lt;/P&gt;
&lt;P&gt;One could mitigate this attack by encrypting the token with the public key first. Then only the server can determine the contents of the token, right? As we’ll see, that’s maybe not true either…&lt;/P&gt;
&lt;P&gt;Fourth, there’s no mechanism proposed whereby encrypted tokens expire or can otherwise be treated as “known to be compromised” in some way. There’s no way to identify hostile clients and revoke their ability to run the software.&lt;/P&gt;
&lt;P&gt;All of these are pretty fundamental problems with this licensing scheme. Those aren’t the specific problem I want to get at today though. &lt;STRONG&gt;There’s also an extremely dangerous mistake in the design of the crypto mechanism.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The expression “use the right tool for the job” never applies more than when considering crypto solutions to real-world problems. &lt;STRONG&gt;Cryptosystems are designed to solve very specific problems very well&lt;/STRONG&gt;, but as soon as you deviate even slightly from the by-design scenario of a cryptosystem, you are off the trail and skiing downhill through the trees; you hit a tree, that’s your fault – you shouldn’t have gone off the trail.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The RSA cryptosystem is extremely good at solving two problems:&lt;/P&gt;
&lt;P&gt;1) Alice (again, the benign client) wishes to send a message of her choice to Bob (the server), encrypted with Bob’s public key. The message can be decrypted by Bob, but not by Eve, the hostile eavesdropper.&lt;/P&gt;
&lt;P&gt;2) Bob wishes to publish a message of his choice that could only have come from Bob. The message is encrypted with Bob’s private key and can be decrypted by anyone with his public key. The fact that the message can be decrypted by Bob’s public key is evidence that the message came from Bob.&lt;/P&gt;
&lt;P&gt;(And obviously these scenarios can be combined; Alice’s message to Bob could be encrypted with &lt;EM&gt;her&lt;/EM&gt; private key, so that Bob is the only one who can read it, and Alice is the only one who could have sent it. &lt;EM&gt;Provided, of course, that Alice and Bob have found some secure mechanism by which to reliably exchange public keys&lt;/EM&gt;. We assume that such a mechanism exists and has been used. This might not be a valid assumption in the real world!) &lt;/P&gt;
&lt;P&gt;Does the scheme outlined above actually fall into either of these scenarios? No, absolutely not. It might appear that we are in scenario two, but we’re not. Scenario two is that Bob is encrypting &lt;STRONG&gt;a message of Bob’s choice &lt;/STRONG&gt;with Bob’s private key. But in the scheme outlined here, the server is encrypting a message of the presumed-hostile client’s choice! &lt;STRONG&gt;The RSA cryptosystem was not designed to be secure against attacks wherein you allow an attacker to encrypt an arbitrary message with your private key.&lt;/STRONG&gt; Such attacks are called “chosen ciphertext” attacks because the attacker chooses a text which is then “decrypted” -- that is, encrypted with the key that Bob would normally use to decrypt a message from Alice.&lt;/P&gt;
&lt;P&gt;And in fact, RSA is not secure against such attacks. If you allow an arbitrary attacker to send you an arbitrary message, which you then encrypt with your private key, then the attacker can carefully craft messages which allow her to do all kinds of crazy things:&lt;/P&gt;
&lt;P&gt;* Most obviously, an attacker can simply make a token which is a document that says something that Bob would never willingly say. The existence of such a service means that no one can use the fact that a message is decryptable with Bob's public key to conclude that this message is vouched for by Bob. But it is far worse than that.&lt;/P&gt;
&lt;P&gt;* Suppose Eve has captured a token message from Alice to Bob, encrypted with Bob's public key. Since encrypting a message with the private key decrypts messages encrypted with the public key, Eve simply sends that as her token to Bob. Bob decrypts Alice's token for Eve. Now Eve knows Alice's token. Of course, Bob can detect that Eve has submitted a token which looks suspiciously like it decrypts to Alice's token, so this might not be the smartest thing for Eve to do. But...&lt;/P&gt;
&lt;P&gt;* An attacker can contrive a &lt;EM&gt;novel&lt;/EM&gt; message which, when encrypted with the private key, results in &lt;EM&gt;enough&lt;/EM&gt; information to decrypt a previously-captured message that was encrypted with the public key. That is: Alice encrypts her token with Bob's public key and sends it to the server (Bob), who decrypts it with the private key, encrypts the result with the private key, and sends it back. Eve intercepts both messages. Eve then crafts a very special message based on a transformation of the captured content&amp;nbsp;and&amp;nbsp;asks Bob to encrypt&amp;nbsp;it with Bob’s private key. &lt;EM&gt;The contents of the result can be used by Eve to determine what Alice’s token is&lt;/EM&gt;. Bob has no easy way of knowing that Eve’s special message is in fact an attempt to decrypt Alice’s token.&lt;/P&gt;
&lt;P&gt;* A hostile authenticated client can choose a token she would like to be encrypted with Bob’s private key, but she does not want Bob to actually see the token. It is possible to contrive a random-seeming token such that you can figure out what a chosen token’s encryption with Bob's private key&amp;nbsp;&lt;EM&gt;would&lt;/EM&gt; be. The hostile client can use this attack in order to generate a token/encrypted token pair that can be distributed to other attackers, but which does not point back to the authenticated client.&lt;/P&gt;
&lt;P&gt;* And so on. We haven’t even gotten into issues like correct padding of the message to prevent replay attacks. There are potentially many more issues here.&lt;/P&gt;
&lt;P&gt;I don’t know much about cryptography; the most important thing that I do know on the subject is that &lt;STRONG&gt;I don’t know nearly enough about cryptography to safely design or implement a crypto-based security system&lt;/STRONG&gt;. &lt;/P&gt;
&lt;P&gt;So, what have we learned?&lt;/P&gt;
&lt;P&gt;0) If you possibly can, simply don’t go there. &lt;STRONG&gt;Encryption is extremely difficult to get right and is frequently the wrong solution in the first place.&lt;/STRONG&gt; Use other techniques to solve your security problems.&lt;/P&gt;
&lt;P&gt;1) If the problem is an untrustworthy client then &lt;STRONG&gt;don’t build a security solution which requires trusting the client.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;2) If you can use &lt;STRONG&gt;off-the-shelf parts&lt;/STRONG&gt; then do so.&lt;/P&gt;
&lt;P&gt;3) If you cannot use off-the-shelf-parts and do have to use a cryptosystem then &lt;STRONG&gt;don’t use a cryptosystem that you don’t fully understand&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;4) If you have to use a cryptosystem that you don’t fully understand, then at least &lt;STRONG&gt;don’t use it to solve problems it was not designed to solve&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;5) If you have to use a cryptosystem to ski through the trees, then at least &lt;STRONG&gt;don’t allow the presumably hostile client to choose the message which is encrypted.&lt;/STRONG&gt; Choose the token yourself. If the token must include information from the client then sanitize it in some way; require it to be only straight ASCII text, insert random whitespace, and so on.&lt;/P&gt;
&lt;P&gt;6) If you have to allow the client to choose the token, then &lt;STRONG&gt;don’t encrypt the token itself&lt;/STRONG&gt;. Sign a cryptographically-secure hash of the token. Its much harder for the attacker to choose a token which produces a desired hash.&lt;/P&gt;
&lt;P&gt;7) &lt;STRONG&gt;Don’t use the same key pair for encrypting&amp;nbsp;outgoing messages as you do for protecting incoming messages&lt;/STRONG&gt;. Get a key pair for each logically different operation you're going to perform.&lt;/P&gt;
&lt;P&gt;8) Encrypt the communications &lt;STRONG&gt;both ways&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;9) Consider having a &lt;STRONG&gt;revocation&lt;/STRONG&gt; mechanism, so that once you know that Eve is attacking you, you can at least revoke her license. (Or you can revoke a known-to-be compromised license, and so on.)&lt;/P&gt;
&lt;P mce_keep="true"&gt;*********&lt;/P&gt;
&lt;P mce_keep="true"&gt;And with those cheerful thoughts, I'm off on my annual flight south to Canada to spend a whirlwind Christmas vacation with family and friends back in Waterloo. I hope you all have a festive holiday season!&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9933799" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/digital+signatures/default.aspx">digital signatures</category></item><item><title>Constraints are not part of the signature</title><link>http://blogs.msdn.com/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx</link><pubDate>Thu, 10 Dec 2009 17:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9932645</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>109</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9932645.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9932645</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;What happens here? &lt;SPAN class=code&gt;
&lt;P&gt;class Animal { } &lt;BR&gt;class Mammal : Animal { } &lt;BR&gt;class Giraffe : Mammal { }&lt;BR&gt;class Reptile : Animal { } &lt;BR&gt;…&lt;BR&gt;static void Foo&amp;lt;T&amp;gt;(T t) where T : Reptile { }&lt;BR&gt;static void Foo(Animal animal) { }&lt;BR&gt;static void Main() &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo(new Giraffe()); &lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Most people assume that overload resolution will choose the second overload. In fact, this program produces a compile error saying that T cannot be Giraffe. Is this a compiler bug?&lt;/P&gt;
&lt;P&gt;No, this behaviour is correct according to the spec. First we attempt to determine the candidate set. Clearly the second overload is a member of the candidate set. The first overload is a member of the candidate set if type inference succeeds.&lt;/P&gt;
&lt;P&gt;The method type inference algorithm considers &lt;STRONG&gt;only&lt;/STRONG&gt; whether the method type arguments can be consistently inferred from the types of the arguments. Method type inference cares not a bit about whether the resulting method is malformed in some other way. Its only job is to work out the best possible type arguments given the arguments to the method. Clearly the best type for T is Giraffe, so that’s what we infer. &lt;/P&gt;
&lt;P&gt;So we now have two methods in the candidate set, Foo&amp;lt;Giraffe&amp;gt;, and the second overload. Which is better?&lt;/P&gt;
&lt;P&gt;Again, overload resolution looks only at the arguments you passed in, and compares them to the types of all the candidates. The argument is of type Giraffe. We have a choice: argument of type Giraffe goes to parameter of type Giraffe, or argument of type Giraffe goes to parameter of type Animal. Clearly the former is better; it’s an exact match.&lt;/P&gt;
&lt;P&gt;Therefore we discard the second overload because it is worse than another candidate. That leaves one candidate left, which is an exact match. Only then, after overload resolution, do we check to see whether the generic constraints are violated. &lt;/P&gt;
&lt;P&gt;When I try to explain this to people, they often bring up this portion of the specification as evidence that I am wrong, wrong, wrong:&lt;/P&gt;&lt;SPAN class=spec&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;If F is generic and M has no type argument list, F is a candidate when:&lt;BR&gt;1) type inference succeeds, and &lt;BR&gt;2) once the inferred type arguments are substituted for the corresponding method type parameters, &lt;EM&gt;all constructed types in the parameter list of F satisfy their constraints&lt;/EM&gt;, and the parameter list of F is applicable with respect to A. [emphasis added]&lt;/SPAN&gt; &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This appears at first glance to say that Foo&amp;lt;Giraffe&amp;gt; cannot be a candidate because the constraints are not satisfied. That is a misreading of the specification; the bit “in the parameter list” is referring to the &lt;EM&gt;formal&lt;/EM&gt; &lt;EM&gt;parameter&lt;/EM&gt; list, not the &lt;EM&gt;type parameter&lt;/EM&gt; list.&lt;/P&gt;
&lt;P&gt;Let me give you an example of where this rule comes into play, so that it’s clear. Suppose we have&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;class C&amp;lt;T&amp;gt; where T : Mammal {}&lt;BR&gt;…&lt;BR&gt;static void Bar&amp;lt;T&amp;gt;(T t, C&amp;lt;T&amp;gt; c) where T : Mammal&amp;nbsp;{}&lt;BR&gt;static void Bar(Animal animal, string s) { }&lt;BR&gt;…&lt;BR&gt;Bar(new Iguana(), null);&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Type inference infers that Bar&amp;lt;Iguana&amp;gt; might be a candidate. But that would mean that we are calling a method that converts null to C&amp;lt;Iguana&amp;gt;, which violates the constraint in the declaration of C&amp;lt;T&amp;gt;. Therefore the results of type inference are discarded, and Bar&amp;lt;Iguana&amp;gt; is not added to the candidate set. &lt;/P&gt;
&lt;P&gt;So if that’s not the relevant bit of the spec, what is? The relevant bit happens &lt;EM&gt;after&lt;/EM&gt; the best method has been determined, not before.&lt;/P&gt;&lt;SPAN class=spec&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;If the best method is a generic method, the type arguments (supplied or inferred) are checked against the constraints declared on the generic method. If any type argument does not satisfy the corresponding constraints on the type parameter, a compile-time error occurs. &lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It is often surprising to people that an invalid method can be chosen as the best method, chosen over a less good method that would be valid. (*) &lt;STRONG&gt;The principle here is overload resolution (and method type inference) find the best possible match between a list of arguments and each candidate method’s list of formal parameters.&lt;/STRONG&gt; That is, they look at the &lt;EM&gt;signature&lt;/EM&gt; of the candidate method. If the best possible match between the arguments and the signature of the method identify a method that is for whatever reason not possible to call, then &lt;EM&gt;you need to choose your arguments more carefully so that the bad thing is no longer the best match&lt;/EM&gt;. We figure that you want to be told that there's a problem, rather than silently falling back to a less-good choice. 
&lt;P&gt;UPDATE: My Twilight-reading friend Jen from a couple episodes back points out that this is not just good language design, it's good dating advice. &lt;STRONG&gt;If the best possible match between your criteria and an available guy on Match.com identifies a guy who, for whatever reason, is impossible to call, then you need to choose your criteria more carefully so that the bad guy is no longer the best match.&lt;/STRONG&gt;&amp;nbsp; Words to live by. 
&lt;HR&gt;

&lt;P&gt;(*) I already discussed the related situation of how it is the case that &lt;A href="http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx"&gt;we can choose a static method over an instance method even when there’s no hope of the static method being the right one.&lt;/A&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9932645" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Type+Inference/default.aspx">Type Inference</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Overload+Resolution/default.aspx">Overload Resolution</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Relationships/default.aspx">Relationships</category></item><item><title>Query transformations are syntactic</title><link>http://blogs.msdn.com/ericlippert/archive/2009/12/07/query-transformations-are-syntactic.aspx</link><pubDate>Mon, 07 Dec 2009 14:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9931506</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>16</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9931506.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9931506</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;As you probably know, there are two ways to write a LINQ query in C#. The way I personally prefer is to use the “query comprehension” syntax:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;from customer in customerList&lt;BR&gt;where customer.City == "London" &lt;BR&gt;select customer.Name&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Or you can, equivalently, use the “fluent method call” syntax:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;customerList&lt;BR&gt;.Where(customer=&amp;gt;customer.City == "London")&lt;BR&gt;.Select(customer=&amp;gt;customer.Name)&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;These are guaranteed to be equivalent because &lt;STRONG&gt;the compiler simply transforms the former syntax into the latter syntax before it compiles it&lt;/STRONG&gt;. An interesting aspect of this transformation is that it is (almost) entirely &lt;STRONG&gt;syntactic&lt;/STRONG&gt;. (The "transparent identifiers"&amp;nbsp;generated for certain queries require a small amount of semantic analysis to determine the corresponding anonymous type,&amp;nbsp;but for the most part, all we do is just pull the raw hunks of code out of each clause and reorganize the program into the method call form.) Once it is in a form that the rest of the compiler can understand, then semantic analysis proceeds as usual.&lt;/P&gt;
&lt;P&gt;This means that it is perfectly legal to do dumb things. For example, suppose we decide that by “where” we mean don’t mean “filter”, we actually mean “multiply”. And by “select” we actually mean “add”, not “project”. No problem!&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;static class ExtInt&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static&amp;nbsp;int Where(this int c, Func&amp;lt;int, int&amp;gt; f)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return f(0) * c;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static&amp;nbsp;int Select(this int c, Func&amp;lt;int, int&amp;gt; f)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return f(0) + c;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;…&lt;BR&gt;int ten = 10;&lt;BR&gt;int&amp;nbsp;twenty = 20;&lt;BR&gt;int&amp;nbsp;thirty = 30;&amp;nbsp;&lt;BR&gt;int result = from c in ten where twenty select thirty; &lt;BR&gt;Console.WriteLine(result); &lt;/SPAN&gt;
&lt;P&gt;And sure enough, ten where/times&amp;nbsp;twenty select/plus thirty is 230. This is a deeply strange way to write a mathematical expression, but legal. 
&lt;P&gt;The semantics of the Where method are supposed to be “takes a collection of T and a predicate mapping T to bool, and returns a filtered collection of those T items which match the predicate”. This method does not take a collection or a predicate, it does not return a collection, and it certainly does not have the semantics of filtering, but the compiler neither knows nor cares about any of those facts. All the compiler does is syntactically turn that line into &lt;SPAN class=code&gt;
&lt;P&gt;int&amp;nbsp;result = ten.Where(c=&amp;gt;twenty).Select(c=&amp;gt;thirty);&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;and compile it; &lt;EM&gt;that&lt;/EM&gt; line compiles just fine, so, no problem as far as the compiler is concerned. 
&lt;P&gt;The C# specification has a section which describes the pattern we expect a query provider to implement: that Where takes a predicate, and so on. But we perform no checks whatsoever that you successfully implemented a query provider that implements either the form or the semantics of our recommended pattern. If you do something crazy, like redefine “where” to take something other than a predicate and you get crazy results, then, well, what can I tell you? If it hurts when you do that then don’t do that! 
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9931506" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/queries/default.aspx">queries</category></item></channel></rss>