<?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>Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx</link><description>I get a fair number of questions about the C# cast operator. The most frequent question I get is: short sss = 123; object ooo = sss; // Box the short. int iii = (int) sss; // Perfectly legal. int jjj = (int) (short) ooo; // Perfectly legal int kkk = (int)</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9490610</link><pubDate>Thu, 19 Mar 2009 20:28:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9490610</guid><dc:creator>Goran</dc:creator><description>&lt;p&gt;Just what I was wondering about last week. As always - clear, precise and informative post. Thank you!&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9490655</link><pubDate>Thu, 19 Mar 2009 20:42:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9490655</guid><dc:creator>holatom</dc:creator><description>&lt;p&gt;Very nice article. I have been waiting for someone to &amp;quot;deal&amp;quot; with this issue a very long time.&lt;/p&gt;
&lt;p&gt;Additionaly the only difficulties with this is in generics classes - best way to address this problem is technique like this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static class DynamicConverter&amp;lt;TFrom, TTo&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static Func&amp;lt;TFrom, TTo&amp;gt; converter = CreateExpression&amp;lt;TFrom, TTo&amp;gt;(body =&amp;gt; Expression.Convert(body, typeof(TTo)));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static TTo Convert(TFrom valueToConvert)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return converter(valueToConvert);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;TFrom, TTo&amp;gt; Converter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get { return converter; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static Func&amp;lt;TArg1, TResult&amp;gt; CreateExpression&amp;lt;TArg1, TResult&amp;gt;(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func&amp;lt;Expression, UnaryExpression&amp;gt; body)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ParameterExpression inp = Expression.Parameter(typeof(TArg1), &amp;quot;inp&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Expression.Lambda&amp;lt;Func&amp;lt;TArg1, TResult&amp;gt;&amp;gt;(body(inp), inp).Compile();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;catch (Exception ex)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string msg = ex.Message; // avoid capture of ex itself&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return delegate { throw new InvalidOperationException(msg); };&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static class DynamicConverter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static TTo Convert&amp;lt;TFrom, TTo&amp;gt;(TFrom valueToConvert)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return DynamicConverter&amp;lt;TFrom, TTo&amp;gt;.Convert(valueToConvert);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;than you can do something like:&lt;/p&gt;
&lt;p&gt;var converted = DynamicConverter.Convert&amp;lt;sometype, T&amp;gt;(source);&lt;/p&gt;
&lt;p&gt;in you generic class if you know than this conversation from sometype to T exists.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9490881</link><pubDate>Thu, 19 Mar 2009 21:56:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9490881</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;You've run out of foot note characters here's a few more ‖, &amp;#182;. :)&lt;/p&gt;
&lt;p&gt;Lovely reasoning, I've often been annoyed at the unboxing convention so it's nice to see a rationale why it doesn't do it.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9490952</link><pubDate>Thu, 19 Mar 2009 22:36:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9490952</guid><dc:creator>configurator</dc:creator><description>&lt;p&gt;Great. Now can you please explain to the people I used to work with that unboxing an int by using Convert.ToInt32(obj) is bad for their health?&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491080</link><pubDate>Thu, 19 Mar 2009 23:35:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491080</guid><dc:creator>John Rayner</dc:creator><description>&lt;p&gt;Great post, Eric, as always. &amp;nbsp;Very informative and thought-provoking. &amp;nbsp;And I think this one must get the record for the most footnotes ever used in a blog post! &amp;nbsp; :-)&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491187</link><pubDate>Fri, 20 Mar 2009 00:33:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491187</guid><dc:creator>Kevin Westhead</dc:creator><description>&lt;p&gt;Interesting post. Do you know what the reasoning was behind the unbox instruction throwing InvalidCastException rather than having another exception type specifically for unboxing failures?&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491296</link><pubDate>Fri, 20 Mar 2009 01:40:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491296</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;&amp;gt; Do you know what the reasoning was behind the unbox instruction throwing InvalidCastException rather than having another exception type specifically for unboxing failures?&lt;/p&gt;
&lt;p&gt;Why not? Semantically, unboxing is a downcast (&amp;quot;this is B which I know is a D - give me that D&amp;quot; - &amp;quot;this is Object which I know is an Int32 - give me that Int32&amp;quot;). It's not identity-preserving simply because value types do not have inherent identity, but otherwise I consider it the same thing, so it makes sense to me that the same exception is used to indicate failure in both cases.&lt;/p&gt;
&lt;p&gt;Of course, it is also possible to set the issue of identity preservation aside entirely, and just say that _all_ conversions deal with representations, and Base-&amp;gt;Derived cast is also a conversion that translates a value of type &amp;quot;reference to Base&amp;quot; to _another_ value of type &amp;quot;reference to Derived&amp;quot; (the fact that the bit pattern may remain the same as a result is irrelevant). If you look at it that way, identity does not even enter into the question, because the value converted - which is the reference, not the object - does not have any identity. Also, from this POV, it makes more sense to have a single cast/convert operator.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491336</link><pubDate>Fri, 20 Mar 2009 02:00:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491336</guid><dc:creator>Rob</dc:creator><description>&lt;p&gt;I had a need for calling the conversion operators on a boxed value back in .net 2 so I wrote a basic dynamic cast method similar to what holatom is suggesting. &amp;nbsp;It's here:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://codegoeshere.blogspot.com/2007/05/dynamic-cast-in-c.html"&gt;http://codegoeshere.blogspot.com/2007/05/dynamic-cast-in-c.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This was a very targeted scenario, though, and I don't think I've needed it since.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491465</link><pubDate>Fri, 20 Mar 2009 03:24:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491465</guid><dc:creator>commongenius</dc:creator><description>&lt;P&gt;VB's CType operator caters to those who would rather not think about the different kinds of casting by generating the smallest amount of conversion code possible given the type information known at compile time. For example, CType(1, Integer) will actually not generate a cast at all, since the compiler knows it is unnecessary. CType(myInt, Long) will generate a conversion operation, the same as (long)myInt; while CType(myStream, MemoryStream) will generate a preserving conversion ("castclass" IL instruction), the same as (MemoryStream)myStream. Additionally, CType(myBoxedInt, Long) will generate a call to a VB helper function which will eventually cast the boxed int to IConvertible, and use that interface to convert to an Int64; this essentially performs the same function as (long)(int)myBoxedInt, although there is a performance cost. But, CType will still give you a compile time error if you try a conversion which is not possible under any circumstances (say, CType(myBoxedInt, &lt;/P&gt;
&lt;P&gt;I am not generally in favor of CType; I prefer explicitly stating the kind of conversion that I know is necessary (which is probably why, or indicative of why, I prefer C# over VB). But the existence of CType is interesting to me because it dramatically demonstrates the difference in philosophies between the languages. C# forces you to prove that you know what you are doing (and in the process often forces you to think through what you are doing, and maybe do it better). VB allows you to say, "I don't care how you do it, don't bother me with the details, just get it done." Of course, if it CAN'T be done, you still get an exception, which you may have been able to find at compile time if you were forced to think about it.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Indeed, this does highlight an important difference in design philosophies. I like to say that VB is a "do what I mean" language, C# is a "do what I say" language. -- Eric&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491493</link><pubDate>Fri, 20 Mar 2009 03:47:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491493</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;&amp;gt; VB allows you to say, &amp;quot;I don't care how you do it, don't bother me with the details, just get it done.&amp;quot;&lt;/p&gt;
&lt;p&gt;From what Eric says, it seems that (T)(dynamic)x would do just that in C# 4.0.&lt;/p&gt;
</description></item><item><title>Fabulous Adventures In Coding : Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491707</link><pubDate>Fri, 20 Mar 2009 06:55:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491707</guid><dc:creator>DotNetShoutout</dc:creator><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from DotNetShoutout&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9491875</link><pubDate>Fri, 20 Mar 2009 09:42:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9491875</guid><dc:creator>Senthil Kumar</dc:creator><description>&lt;P&gt;As I understand it, the issue is that the actual type of the object is unknown until runtime, so the compiler can't possibly know how to convert an unknown type to the type specified in the program.&lt;/P&gt;
&lt;P&gt;Would a virtual method defined on object, say T Cast&amp;lt;T&amp;gt;(), solve the problem? That way, the compiler can issue an unbox, emit the virtual call to the unknown type and let it deal with problem if it wants to?&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Sure, if we had generics in .NET v1 that would have worked. Basically you would be putting the onus on the developer of the type to provide conversions to arbitrary types. I like that in theory; I like thinking of a type as "a set of values associated with a set of conversion rules". Such a system encapsulates that concept nicely. But that ship has sailed; we did not have generics in v1 and we're not going to add new virtual methods to object now. -- Eric&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9493016</link><pubDate>Fri, 20 Mar 2009 20:58:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9493016</guid><dc:creator>commongenius</dc:creator><description>&lt;p&gt;pminaev said:&lt;/p&gt;
&lt;p&gt;&amp;quot;&amp;gt; VB allows you to say, &amp;quot;I don't care how you do it, don't bother me with the details, just get it done.&amp;quot;&lt;/p&gt;
&lt;p&gt;From what Eric says, it seems that (T)(dynamic)x would do just that in C# 4.0.&amp;quot;&lt;/p&gt;
&lt;p&gt;Not exactly. VB uses CType as a universal conversion operator, which will generate the most efficient form of conversion possible wherever it is used. You don't have to think about which type of conversion you are doing in different circumstances, because you are using the same operator. In the worst case, where no type information is known (i.e. casting from Object), the least efficient form of conversion is used (which basically tries each kind of conversion in turn). Casting to dynamic in C# 4.0 essentially removes all type information, forcing you into the least efficient conversion, even if a more efficient conversion could have been performed had the type information been preserved (such as in the (int)(long)myBoxedLong case). The dynamic version is slightly better though, since it caches the results of the conversion overload resolution.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9493589</link><pubDate>Sat, 21 Mar 2009 00:20:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9493589</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;P&gt;&amp;gt; Casting to dynamic in C# 4.0 essentially removes all type information, forcing you into the least efficient conversion, even if a more efficient conversion could have been performed had the type information been preserved&lt;/P&gt;
&lt;P&gt;I don't see any reason why the compiler wouldn't be able to optimize the case of casting a value of type definitely known at compile-time to dynamic, as in my example - at least in theory. I doubt that the actual implementation in .NET 4 will do that - it seems to require too much effort for very dubious value - but it is certainly possible to optimize it in precisely the same way as CType does (since, after all, it has all the same inputs!).&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Indeed. Our philosophy for "dynamic" is "you &lt;EM&gt;said&lt;/EM&gt; dynamic, so&amp;nbsp;you &lt;EM&gt;meant&lt;/EM&gt; dynamic, so you'll &lt;EM&gt;get&lt;/EM&gt; dynamic." We have certainly considered doing compiler work to detect situations where we know at compile time that a dynamic call cannot possibly succeed, or detect situations where the compiler can deduce enough type information about the dynamic thing to skip making a dynamic call. As you correctly call out, this is an immense amount of work for bizarre corner cases that directly contradict the stated intention of the programmer -- if the programmer says they want dynamic dispatch that might fail at runtime, that's what they'll get. -- Eric&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9494876</link><pubDate>Sat, 21 Mar 2009 09:33:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9494876</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;&amp;gt; As you correctly call out, this is an immense amount of work for bizarre corner cases that directly contradict the stated intention of the programmer -- if the programmer says they want dynamic dispatch that might fail at runtime, that's what they'll get.&lt;/p&gt;
&lt;p&gt;Will that be a hard requirement in the language spec, however? I.e. will the conforming compiler be required to defer the error until execution even if it can see that it is going to fail at compile time already? Or is it implementation-defined?&lt;/p&gt;
&lt;p&gt;I think it doesn't touch the original case we discussed either way, though, as it wasn't about an error case - it was merely about optimization for the successful case. Something like (int)(dynamic)1.0 cannot fail, and if the compiler is smart enough to handle that at compile-time, the &amp;quot;as if&amp;quot; rule should kick in.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9527920</link><pubDate>Thu, 02 Apr 2009 00:34:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9527920</guid><dc:creator>Anthony D. Green</dc:creator><description>&lt;p&gt;&amp;quot;VB allows you to say, 'I don't care how you do it, don't bother me with the details, just get it done.'&amp;quot; (commongenius).&lt;/p&gt;
&lt;p&gt;Indeed we call that declarative programming. This is a quality of markup languages, SQL, RegularExpressions and highly desirable in many cases. &lt;/p&gt;
&lt;p&gt;This is not to say that it's always desirable indeed I appreciate the recognition of a valid though different philosophy. I've recently come to terms with CType - until recently I was more fond of DirectCast because of its explicitness. I also sort of liked that the VB implementation of CType vs DirectCast separated out the two functions of the C# cast syntax - i.e. Conversion (representation-changing) versus Casting (identity-preserving). I've since decided that I was just being needlessly obsessive and that differing to the tool to exercise the most appropriate (hopefully optimized path) method was maintenence-wise more concise (or consistent).&lt;/p&gt;
&lt;p&gt;Ultimately it's a choice between who gets the burden for optimizing implementation - the SQL Server team has to be deligent when emitting query plans to use all information to produce the query plan that's most efficient whereas with the more explicit - imperative style the burden is shifted to the user. I prefer the former but always appreciate the option to go explicit as required but I think it's most important for the programmer to be aware that in these cases they are delegating responsibility to the tool - that it's not just magic.&lt;/p&gt;
&lt;p&gt;&amp;quot;VB is a 'do what I mean' language, C# is a 'do what I say' language &amp;quot; (Eric).&lt;/p&gt;
&lt;p&gt;I like this summarization. I rings well with my discoveries about some of the finer rules of Option Strict Off in VB (which is an advanced language feature, btw, not a novice one - which is why it should be off by default). A lot of people characterize it as a feature that let's you just willy nilly do anything and everything and the compiler will just make things happen by parsing horoscopes. This is not true. When you look at the rules it won't let you do anything that you couldn't say explicitly (and can still give compile time warnings for things it knows you definitely can't do) and even being explicit about the cast/conversion it's still just as possible to ask to do something that will fail (I would argue that having to type CType doesn't actually make me more likely to realize a conversion will fail at runtime but then again I don't do a lot of conversions anyway)&lt;/p&gt;
&lt;p&gt;It's a philosophical difference of whether I have to say Dim i As Integer = CType(o, Integer) or whether it's sensible for the tool to infer from my assining o to i that I would naturally want - need - mean - to convert o to an Integer first - whether I should have to say something that I'd have to say anyway.&lt;/p&gt;
&lt;p&gt;Good read.&lt;/p&gt;
</description></item><item><title>Fabulous Adventures In Coding : Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9554181</link><pubDate>Fri, 17 Apr 2009 15:41:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9554181</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9602423</link><pubDate>Mon, 11 May 2009 14:20:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9602423</guid><dc:creator>Wardy</dc:creator><description>&lt;p&gt;I think that all went straight over my head ... dam I feel stupid now, time to get those mcts books out.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9859149</link><pubDate>Thu, 06 Aug 2009 16:41:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9859149</guid><dc:creator>Tom</dc:creator><description>&lt;p&gt;I'm a new fan of your series. &amp;nbsp;Keep these fantastic articles coming!&lt;/p&gt;
&lt;p&gt;One correction: &amp;nbsp;Nullable&amp;lt;ValueType&amp;gt; is not imiplicitly convertable to ValueType. &amp;nbsp;It's the other way around.&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9859158</link><pubDate>Thu, 06 Aug 2009 16:46:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9859158</guid><dc:creator>Tom</dc:creator><description>&lt;p&gt;Sorry for the repost, wanted to provide a sample. &amp;nbsp;If you can merge these two posts, please do.&lt;/p&gt;
&lt;p&gt;int i = 0;&lt;/p&gt;
&lt;p&gt;int? j = null;&lt;/p&gt;
&lt;p&gt;int i = j; &amp;nbsp; &amp;nbsp;// won't compile&lt;/p&gt;
&lt;p&gt;int i = (int)j; &amp;nbsp; &amp;nbsp;// throws InvalidOperationException&lt;/p&gt;
&lt;p&gt;int i = j ?? 0; &amp;nbsp; &amp;nbsp;// works&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9935841</link><pubDate>Fri, 11 Dec 2009 19:01:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9935841</guid><dc:creator>Eric Ouellet</dc:creator><description>&lt;P&gt;Hello Eric,&lt;/P&gt;
&lt;P&gt;I red your nice article.&lt;/P&gt;
&lt;P&gt;I don't know if it is because I missed somthing important but I feel that C# could support something like that:&lt;/P&gt;
&lt;P&gt;&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;public class Something&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;}&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;public class SomethingWrapper&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;private Something _something;&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;public static implicit operator Something(SomethingWrapper wrapper)&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;return wrapper.GetSomething();&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;public Something GetSomething()&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;return _something;&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;public &amp;nbsp;SomethingWrapper(Something something)&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;_something = something;&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;// More code here...&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;// ******************************************************************&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void button1_Click(object sender, RoutedEventArgs e)&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;SomethingWrapper sw = new SomethingWrapper(new Something());&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;Object o = sw;&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;Something s = (Something)o; // InvalidCastException.&lt;/P&gt;
&lt;P&gt;If it exists only one way where the compiler could pass from "Object"(real object: SomethingWrapper) to "Something" object, &lt;/P&gt;
&lt;P&gt;then why the compiler does not create the code and just do it ? &lt;/P&gt;
&lt;P&gt;Why it would need dynamic type for that ? &lt;/P&gt;
&lt;P&gt;Why not doing it on object ?&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;In order to make your scenario work, the generated code has to start the C# compiler, run overload resolution to determine that there is a conversion from the runtime type of the object, somehow call the conversion, and return the result. Remember, we have to run this code on EVERY cast. That would make C# a very slow language indeed. Now, if you want to start up the compiler again at runtime, in C# 4 you can do that if you choose to take that performance hit.&amp;nbsp;Make the argument dynamic, and we'll do&amp;nbsp;all the analysis at runtime.&amp;nbsp;-- Eric&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;If it would be supported, it would open the door to a fantastic world of generic wrapper classes.&lt;/P&gt;
&lt;P&gt;SomethingWrapper could be generics to works on anything, not only Something. &lt;/P&gt;
&lt;P&gt;Am I wrong ? Did I missed something somewhere ?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9935923</link><pubDate>Fri, 11 Dec 2009 21:59:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9935923</guid><dc:creator>Eric Ouellet</dc:creator><description>&lt;p&gt;Thanks Eric for your feedback, it was very quick !&lt;/p&gt;
&lt;p&gt;I absolutely do not want to use &amp;quot;dynamic&amp;quot; typed object. It's untyped (no intellisense and many more, you know all that...). By definition is bad to use it!&lt;/p&gt;
&lt;p&gt;Althought the compiler is able to do portion of the discovery on the fly, actually (it could change), I do not want to touche dynamic object with a 10' pole.&lt;/p&gt;
&lt;p&gt;Related to my previous sample...&lt;/p&gt;
&lt;p&gt;I would probably be able to do: &lt;/p&gt;
&lt;p&gt;something s = (dynamic)o; // Pass throught dynamic object to do dynamic cast to typed type ?&lt;/p&gt;
&lt;p&gt;Does the language will offer a new keyword of the style &amp;quot;dynamic_cast&amp;quot;... Will I be able to write:&lt;/p&gt;
&lt;p&gt;c#3.5 : Something s = (Something)o; // InvalidCastException.&lt;/p&gt;
&lt;p&gt;c#4 &amp;nbsp; : Something s = dynamic_cast&amp;lt;Something&amp;gt;o; // Find the path to the object, throw an exception if more than one path available (no dynamic objec)&lt;/p&gt;
&lt;p&gt;That dynamic_cast is very nice... Hope Anders thought about it for C#4 !!!&lt;/p&gt;
</description></item><item><title>re: Representation and Identity</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx#9938369</link><pubDate>Thu, 17 Dec 2009 20:32:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9938369</guid><dc:creator>Eric Ouellet</dc:creator><description>&lt;p&gt;In fact, I do not understand why cast could not cast its source to its real underlying object type source before trying to cast ? &lt;/p&gt;
&lt;p&gt;Why I can't do that, and why it would cost a lot to do it ?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public class EnumValueWrapper&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// **************************************************************&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private readonly T _enumValue;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// **************************************************************&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public EnumValueWrapper(T enumValue)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_enumValue = enumValue;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// **************************************************************&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static implicit operator T(EnumValueWrapper&amp;lt;T&amp;gt; enumValue)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return enumValue.GetEnumValue();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// **************************************************************&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public T GetEnumValue()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return _enumValue;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;	 &amp;nbsp; &amp;nbsp;... // Elided for clarity&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;	...&lt;/p&gt;
&lt;p&gt;	public enum FirstLast { first, last };&lt;/p&gt;
&lt;p&gt;	FirstLast firstLast;&lt;/p&gt;
&lt;p&gt;	EnumValueWrapper&amp;lt;FirstLast&amp;gt; firstLatsWrapper = new EnumValueWrapper&amp;lt;FirstLast&amp;gt;(firstLast);&lt;/p&gt;
&lt;p&gt;	Object objFirstLast = firstLatsWrapper;&lt;/p&gt;
&lt;p&gt;	firstLast = (FirstLast)objFirstLast; // Exception&lt;/p&gt;
&lt;p&gt;The compiler already as to do a check at runtime to ensure inheritance compatibility. There is already a kind of performance penalty.&lt;/p&gt;
&lt;p&gt;It could pregenerate code like (pre IL): &lt;/p&gt;
&lt;p&gt;	firstLast = (FirstLast)objFirstLast; &amp;nbsp; &amp;nbsp; &amp;nbsp;==&amp;gt; &amp;nbsp; &amp;nbsp; firstLast = (FirstLast)(objFirstLast.GetType())objFirstLast;&lt;/p&gt;
&lt;p&gt;Everything would work perfectly and the cost would be minimal (I think). Are you agree ?&lt;/p&gt;
</description></item></channel></rss>