<?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>Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx</link><description>I hear a lot of myths about C#. Usually the myths have some germ of truth to them, like " value types are always allocated on the stack ". If you replace "always" with "sometimes", then the incorrect mythical statement becomes correct. 
 One I hear quite</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#10326824</link><pubDate>Wed, 04 Jul 2012 13:07:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10326824</guid><dc:creator>ruju</dc:creator><description>&lt;p&gt;gr8!! this is what i was searching for. I had a gr8 confusion about interface type.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10326824" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9955611</link><pubDate>Sat, 30 Jan 2010 02:25:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9955611</guid><dc:creator>甜番薯</dc:creator><description>&lt;p&gt;I translate this post to chinese:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.cnblogs.com/tianfan/archive/2010/01/28/not-everything-derives-from-object.html"&gt;http://www.cnblogs.com/tianfan/archive/2010/01/28/not-everything-derives-from-object.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;我已将此文章翻译成中文：&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.cnblogs.com/tianfan/archive/2010/01/28/not-everything-derives-from-object.html"&gt;http://www.cnblogs.com/tianfan/archive/2010/01/28/not-everything-derives-from-object.html&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9955611" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9954728</link><pubDate>Thu, 28 Jan 2010 15:50:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9954728</guid><dc:creator>AM</dc:creator><description>&lt;p&gt;The ability to call System.Object's members is a C# language implementation decision as opposed to the implementation of a language like VB.Net(@Patrick upcast to access System.Object members).&lt;/p&gt;
&lt;p&gt;Calling ToString() on an instance of an object implementing an interface (Iface) produces IL as such:&lt;/p&gt;
&lt;p&gt;L_000f: callvirt instance string [mscorlib]System.Object::ToString()&lt;/p&gt;
&lt;p&gt;Specifying &amp;quot;string ToString();&amp;quot; on the Interface changes the IL such:&lt;/p&gt;
&lt;p&gt;L_000f: callvirt instance string Iface::ToString()&lt;/p&gt;
&lt;p&gt;So as the author states the interface isn't inheriting those members from System.Object, it's the compiler that decides to resolve those members to the System.Object class because its a resonable assumption to make that any type that implements Iface is convertable to an object.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9954728" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9951241</link><pubDate>Thu, 21 Jan 2010 03:39:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9951241</guid><dc:creator>Bob Bedell</dc:creator><description>&lt;p&gt;13.2 &amp;quot;The members of class object are not, strictly speaking, members of any iinterface.&amp;quot;&lt;/p&gt;
&lt;p&gt;How about this one?&lt;/p&gt;
&lt;p&gt;interface MyInterface&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bool Equals(object obj);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int GetHashCode();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Type GetType();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string ToString();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;class MyClass : MyInterface&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // MyInterface is implicitly implemented &amp;nbsp;because MyClass implicitly derives from type object.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;Isn't it really the case that the members of type object are implicitly members of EVERY interface?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9951241" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9894897</link><pubDate>Mon, 14 Sep 2009 13:34:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9894897</guid><dc:creator>Rakesh</dc:creator><description>&lt;P&gt;Any idea why following code does not generate compile time error?&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;public interface ITest&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;void PrintMessage();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp; // Class does not implement ITest interface.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public class BaseClass&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/P&gt;
&lt;P&gt;&amp;nbsp; class Program&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;static void Main(string[] args)&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;BaseClass bclass = new BaseClass();&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;// Why compiler does not generate error here....&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;((ITest)bclass).PrintMessage();&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;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;First off, of course it generates an error at runtime. But why not at compile time?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Suppose we rewrite your program like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BaseClass bclass = GetSomething();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;((ITest)bclass).PrintMessage();&lt;/P&gt;
&lt;P&gt;Do you agree that the compiler cannot produce an error here? Since BaseClass is not sealed, there could be a derived class that implements ITest, and an instance of it could be returned by GetSomething.&lt;/P&gt;
&lt;P&gt;The compiler is not smart enough to deduce that bclass is always of type BaseClass and never of a more derived class&amp;nbsp;on the codepath that has the cast operator. We do flow analysis, but only for the purposes of determining reachability and definite assignment, not for analyzing the possible runtime types of variables. We could implement such a system, but it would be a lot of work for a small gain. &lt;/P&gt;
&lt;P&gt;-- Eric&lt;BR&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9894897" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9892743</link><pubDate>Tue, 08 Sep 2009 21:54:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9892743</guid><dc:creator>STJ</dc:creator><description>&lt;P&gt;What about the "null type"? (see C# Language Specification §9.4.4.6, §11.2.7)&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;We decided&amp;nbsp;during the C# 3 design and specification process&amp;nbsp;that having a special type that only contains one value, null, was a bad idea. It seems plausible, and other language specs also&amp;nbsp;do it this way, but ultimately it adds very little value. The null type is quite unusual in that its the only type that doesn't have a CLR type object associated with it. It is conceptually easier to simply have some expressions be typeless, and be OK with that, than to invent an ad-hoc type solely for the purpose of being able to say what the type of the null literal expression is. This language should have been removed from the C# 3 spec.&amp;nbsp;-- Eric&lt;/P&gt;&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9892743" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9892190</link><pubDate>Mon, 07 Sep 2009 14:18:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9892190</guid><dc:creator>antlypls</dc:creator><description>&lt;p&gt;Also, this myth is sustained by Jeff Richter's book &amp;quot;CLR via C#, 2nd ed&amp;quot;, there is section called &amp;quot;All types are derived from System.Object&amp;quot; in Chapter 4 &amp;quot;Type Fundamentals&amp;quot; of the book.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9892190" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9888049</link><pubDate>Fri, 28 Aug 2009 10:32:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9888049</guid><dc:creator>Orhan</dc:creator><description>&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/tr-tr/library/ms173156"&gt;http://msdn.microsoft.com/tr-tr/library/ms173156&lt;/a&gt;(en-us).aspx &amp;nbsp;this article says &amp;quot;An interface can itself inherit from multiple interfaces.&amp;quot;. so, how about that?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9888049" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9872774</link><pubDate>Mon, 17 Aug 2009 22:57:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9872774</guid><dc:creator>Vladimir Reshetnikov</dc:creator><description>&lt;P&gt;Even the authors of C# Language Specification were deceived by this myth. :-)&lt;/P&gt;
&lt;P&gt;Section 4.2.2 says: "The object class type is the ultimate base class of all other types . Every type in C# directly or indirectly derives from the object class type."&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Yep. Mads is aware of the error and will correct the spec in the next version. -- Eric&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;Actually, there are some very special value types (like System.TypedReference) that are not convertible to object.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;Yep. One of these days I'll do a blog post about those very special types. That will at the very least force me to figure out how they work. :-) -- Eric &lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9872774" width="1" height="1"&gt;</description></item><item><title>re: Not everything derives from object</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx#9863582</link><pubDate>Tue, 11 Aug 2009 01:33:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9863582</guid><dc:creator>pminaev</dc:creator><description>&lt;p&gt;@leo, it works because C# language spec requires it to; 7.3 &amp;quot;Member lookup&amp;quot;:&lt;/p&gt;
&lt;p&gt;&amp;quot;A member lookup of a name N with K type parameters in a type T is processed as follows:&lt;/p&gt;
&lt;p&gt;*	If T is a type parameter, then the set is the union of the sets of accessible members named N in each of the types specified as a primary constraint or secondary constraint (&amp;#167;10.1.5) for T, along with the set of accessible members named N in Object.&lt;/p&gt;
&lt;p&gt;*	Otherwise, the set consists of all accessible (&amp;#167;3.5) members named N in T, including inherited members and the accessible members named N in Object. If T is a constructed type, the set of members is obtained by substituting type arguments as described in &amp;#167;10.3.2. Members that include an override modifier are excluded from the set.&amp;quot;&lt;/p&gt;
&lt;p&gt;Note how it specifically calls for looking up members in Object, and at the same time makes a distinction between &amp;quot;inherited members&amp;quot; and &amp;quot;members in Object&amp;quot;. &lt;/p&gt;
&lt;p&gt;What is the rationale for this when T is an interface type is a different question, but it is understandable. After all, if you can call instance members of Object on a value of interface type as if they were &amp;quot;inherited&amp;quot; from Object, it would only be logical to extend the same pseudo-inheritance to static members.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9863582" width="1" height="1"&gt;</description></item></channel></rss>