<?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>Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx</link><description>Pop quiz! 
 What do you expect the output of this program to be? 
 
 using System; 
 class Foo { public Foo(string s) { Console.WriteLine("Foo constructor: {0}", s); } public void Bar() { } } class Base { readonly Foo baseFoo = new Foo("Base initializer</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#10088912</link><pubDate>Wed, 10 Nov 2010 15:55:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10088912</guid><dc:creator>Syed Salman Akbar</dc:creator><description>&lt;p&gt;It is actually very sound to run initializes first from derived to base while constructors from base to derived. The basic purpose of the constructor is to assign some values to the member fields. Right? Now to assign the member fields the runtime must first be able to see what are those member fields? Now just have a look at the initalizer. What is an initializer? Very simply it is a statement assigning a value upon declaration.&lt;/p&gt;
&lt;p&gt;Now when an object is create for a particular class the fields of the class should be first declared correctly. Then it’s base class and then base of that base class until we reach system. Object. This is like constructing a building where engineers create the outer most walls of the structure first which in this case will be the fields of the outer most class. And then the support pillars inside the building. Now think of constructors as creating floors in the building. You create first floor that is the system. Object’s constructor, then you create the first floor (next class after system. object) and then you create the next till you get to last class which is the class whose object is being initialized.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10088912" width="1" height="1"&gt;</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#10088908</link><pubDate>Wed, 10 Nov 2010 15:52:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10088908</guid><dc:creator>Syed Salman Akbar</dc:creator><description>&lt;p&gt;It is actually very sound to run initializes first from derived to base while constructors from base to derived. The basic purpose of the constructor is to assign some values to the member fields. Right? Now to assign the member fields the runtime must first be able to see what are those member fields? Now just have a look at the initalizer. What is an initializer? Very simply it is a statement assigning a value upon declaration.&lt;/p&gt;
&lt;p&gt;Now when an object is create for a particular class the fields of the class should be first declared correctly. Then it’s base class and then base of that base class until we reach system. Object. This is like constructing a building where engineers create the outer most walls of the structure first which in this case will be the fields of the outer most class. And then the support pillars inside the building. Now think of constructors as creating floors in the building. You create first floor that is the system. Object’s constructor, then you create the first floor (next class after system. object) and then you create the next till you get to last class which is the class whose object is being initialized.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10088908" width="1" height="1"&gt;</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#9806812</link><pubDate>Sun, 28 Jun 2009 01:08:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806812</guid><dc:creator>lance7</dc:creator><description>&lt;p&gt;it always that initializers run first- this is a key point&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9806812" width="1" height="1"&gt;</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#9597214</link><pubDate>Fri, 08 May 2009 18:54:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9597214</guid><dc:creator>Java Guy</dc:creator><description>&lt;p&gt;Exactly as Bill Sanders said. This init order is to support a polymorhic method call in the constructor.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9597214" width="1" height="1"&gt;</description></item><item><title>Mutating Readonly Structs </title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#9339011</link><pubDate>Mon, 19 Jan 2009 03:03:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9339011</guid><dc:creator>Fabulous Adventures In Coding</dc:creator><description>&lt;p&gt;Consider this program which attempts to mutate a readonly mutable struct. What happens? struct Mutable&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9339011" width="1" height="1"&gt;</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8972694</link><pubDate>Thu, 02 Oct 2008 05:50:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8972694</guid><dc:creator>Biggles</dc:creator><description>&lt;p&gt;This is why i always initialize variables in the constructor. That way u dont have to &lt;/p&gt;
&lt;p&gt;worry about it.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8972694" width="1" height="1"&gt;</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8914022</link><pubDate>Sun, 31 Aug 2008 21:19:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8914022</guid><dc:creator>Atreya A</dc:creator><description>&lt;p&gt;I just noticed that 'PK' and me are trying to convey almost one and the same thing.&lt;/p&gt;
&lt;p&gt;Before this i was doubtfull about my post but now i am 100% sure that mine (and PK's as well) is the right answer for the question asked in this post :)&lt;/p&gt;
&lt;p&gt;namaste!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8914022" width="1" height="1"&gt;</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8914008</link><pubDate>Sun, 31 Aug 2008 21:14:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8914008</guid><dc:creator>Atreya A</dc:creator><description>&lt;p&gt;Hi All,&lt;/p&gt;
&lt;p&gt;What i feel is that it always that initializers run first; and the pointer to base is also a member of derived class and in order to initialize that member the control passes to base class.&lt;/p&gt;
&lt;p&gt;All i mean to say is that the process of object creation and initialization always starts first at the derived class only. Where as in order to construct or initialize the derived object completely we need instance of base class as well; that is why the partially initialized derived object is pushed to stack for time being and control moves to base class.&lt;/p&gt;
&lt;p&gt;As soon as the base class is completely initialized the partially initialized object is popped from the stack and initialized completely using the base object which is now available.&lt;/p&gt;
&lt;p&gt;So in the meantime when our partially initialized object of derived class is taking some rest in the stack, the initialization of base object completes and we feel that the process has started from the base class only. Whereas this is just an illusion.&lt;/p&gt;
&lt;p&gt;Have a look at the sample code below and then just read the sequence of activities:&lt;/p&gt;
&lt;p&gt;class classBase&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int a = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int b = 1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public void classBase()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&amp;quot;Base Class Constructor called&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;class classDerived : classBase&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; int c = 2;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; int d = 3;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public void classDerived()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(&amp;quot;Derived Class Constructor called&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;1.) Initialization of derived class object starts &lt;/p&gt;
&lt;p&gt;2.) All members except the reference to base class are initialized&lt;/p&gt;
&lt;p&gt;3.) When .NET tries to initialize the reference to object base class; it is found to be NULL&lt;/p&gt;
&lt;p&gt;4.) The partially initialized object of derived class is pushed to STACK&lt;/p&gt;
&lt;p&gt;5.) Initialization of Base class starts&lt;/p&gt;
&lt;p&gt;6.) All members are initialized since there is no dependency&lt;/p&gt;
&lt;p&gt;7.) CONSTRUCTOR of base class is called to complete process of object creation of Base class &amp;nbsp;&lt;/p&gt;
&lt;p&gt;8.) Code in CONSTRUCTOR of base class executes.&lt;/p&gt;
&lt;p&gt;9.) Now control returns to derived class object&lt;/p&gt;
&lt;p&gt;10.) Partially initialized object of derived class is POPPED from the Stack.&lt;/p&gt;
&lt;p&gt;11.) Initialization of Derived class object completes as now we have reference to base class object as well.&lt;/p&gt;
&lt;p&gt;12.) CONSTRUCTOR of derived class is called to complete process of object creation of Derived class &amp;nbsp;&lt;/p&gt;
&lt;p&gt;13.) Code in CONSTRUCTOR of Derived class executes.&lt;/p&gt;
&lt;p&gt;namaste!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8914008" width="1" height="1"&gt;</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8832515</link><pubDate>Tue, 05 Aug 2008 03:03:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8832515</guid><dc:creator>Bill Sanders</dc:creator><description>&lt;p&gt;If someone has mentioned this above and I missed it, I apologize for repeating, but one potentially &amp;quot;snagly&amp;quot; situation I can think of is this: constructor of Base calls a method of Base which is overridden in Derived and whose overridden behavior references a (non-static initonly) member of Derived which has not yet been initialized.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8832515" width="1" height="1"&gt;</description></item><item><title>C# 小测试（一）：类成员实例化与构造函数执行的顺序</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8724009</link><pubDate>Sat, 12 Jul 2008 15:27:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8724009</guid><dc:creator>Anders Cui</dc:creator><description>&lt;p&gt;我们在实现类的继承时，创建派生类的实例时，基类与派生类的实例字段都要进行实例化，他们的构造函数都需要调用，那执行的顺序是怎样的呢？一起来做做这个测试题吧。&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8724009" width="1" height="1"&gt;</description></item></channel></rss>