<?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/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"); public</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7731279</link><pubDate>Sat, 16 Feb 2008 11:05:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7731279</guid><dc:creator>Jon Skeet</dc:creator><description>&lt;p&gt;Possible spoilers by implication, so ROT-13.&lt;/p&gt;
&lt;p&gt;V'ir nyjnlf yvxrq vg gur jnl vg vf - orpnhfr V'ir rkcrevraprq gur cnva bs vg orvat gur bgure jnl ebhaq.&lt;/p&gt;
&lt;p&gt;Gur rdhvinyrag (va grezf bs fbhepr) Wnin cebtenz jevgrf:&lt;/p&gt;
&lt;p&gt;Sbb pbafgehpgbe: Onfr vavgvnyvmre&lt;/p&gt;
&lt;p&gt;Onfr pbafgehpgbe&lt;/p&gt;
&lt;p&gt;Sbb pbafgehpgbe: Qrevirq vavgvnyvmre&lt;/p&gt;
&lt;p&gt;Qrevirq pbafgehpgbe&lt;/p&gt;
&lt;p&gt;Guvf orunivbhe pnhfrq n irel uneq-gb-qvntabfr reebe va n pbyyrnthr'f pbqr znal zbbaf ntb. Vg graqf abg gb znggre vs lbh qba'g znxr iveghny pnyyf jvguva gur onfr pbafgehpgbe, ohg gung'f rknpgyl jung gur zbfg angheny qrfvta pnyyrq sbe va guvf pnfr. (Gung jnf jura V svefg yrnearq gung vg'f n onq vqrn gb znxr iveghny pnyyf sebz n pbafgehpgbe!)&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7732706</link><pubDate>Sat, 16 Feb 2008 12:58:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7732706</guid><dc:creator>Barry Kelly</dc:creator><description>&lt;p&gt;The most obvious thing that comes to my mind is virtual method calls in ancestor constructors. Bad practice in any case, though sometimes it's the most natural solution.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7738099</link><pubDate>Sat, 16 Feb 2008 20:49:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7738099</guid><dc:creator>Alex Simkin</dc:creator><description>&lt;p&gt;Actually implementation is rather confusing.&lt;/p&gt;
&lt;p&gt;Make baseFoo and derivedFoo fields static and run the application. Derived will be initialized before base as with non-static fields.&lt;/p&gt;
&lt;p&gt;Then, modify constructor of Foo to take reference to Foo and pass baseFoo to derivedFoo constructor. The initialization sequence will change to Base first, then Derived.&lt;/p&gt;
&lt;p&gt;Confusing...&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7739365</link><pubDate>Sat, 16 Feb 2008 21:54:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7739365</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Ah, yes. I should have mentioned that. The rules for static constructors and static fields are subtly different, and I do not intend this article to discuss anything about the rules for statics. That's a great topic for a follow-up article.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7773442</link><pubDate>Mon, 18 Feb 2008 15:51:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7773442</guid><dc:creator>David Moles</dc:creator><description>&lt;p&gt;1. Accurate.&lt;/p&gt;
&lt;p&gt;2. Misleading. [Waves to James Gosling.]&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7774791</link><pubDate>Mon, 18 Feb 2008 17:54:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7774791</guid><dc:creator>Alex Simkin</dc:creator><description>&lt;p&gt;Just a note. VB.NET does initialization differently.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7774892</link><pubDate>Mon, 18 Feb 2008 18:02:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7774892</guid><dc:creator>Franck Jeannin</dc:creator><description>&lt;p&gt;I suspect there is a good reason behind every 'confusing' behavior... I think I know why the following program has to behave that way but I find it confusing nevertheless.&lt;/p&gt;
&lt;p&gt;class Program {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B b = new B();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b.Display();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b.OtherDisplay();&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;class A {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public void Display() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;from A&amp;quot;);&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;class B : A {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new void Display() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;from B&amp;quot;);&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 void OtherDisplay() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Display();&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;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7776011</link><pubDate>Mon, 18 Feb 2008 19:19:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7776011</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;I'll make a guess:&lt;/p&gt;
&lt;p&gt;If it's done the 'obvious' way, there exists a point (during the base constructor) where the derived readonly field exists but is 0. There exists a later point where the same (readonly) field has a different value. That's presumably not good.&lt;/p&gt;
&lt;p&gt;If it's done the other way around, then by the time any code runs which has access to the new object (the ObjectConstructor), all the readonly fields are fully initialized.&lt;/p&gt;
&lt;p&gt;I'll guess further that this behaviour is due to some kind of weirdness in C# whereby the object's dynamic type is the type of the full object throughout construction, rather than the dynamic type changing as more constructors get called, as happens in C++.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7777767</link><pubDate>Mon, 18 Feb 2008 21:20:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7777767</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Correct, though your characterization of the CLR semantics for the dynamic type of an object as &amp;quot;weirdness&amp;quot; is itself somewhat weird. I would argue that it is more weird for the dynamic type of an object to change throughout that object's lifetime! &amp;nbsp;The type of an object is a constant from the moment it is created through to the moment it is destroyed. Mutable objects are hard enough to reason about; objects that mutate their own runtime type are truly odd.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7781393</link><pubDate>Tue, 19 Feb 2008 01:11:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7781393</guid><dc:creator>Ian Marteens</dc:creator><description>&lt;p&gt;A useful addition to C# would be to allow field initializers in true C++ fashion:&lt;/p&gt;
&lt;p&gt;public class Stack&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private T[] items;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private int count;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public Stack&amp;lt;T&amp;gt;(int capacity) : items(new T[capacity]) { }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// ...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This corresponding IL code is currently tolerated by the CLR, and it solves several problems with assertions regarding nullability of fields.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7792743</link><pubDate>Tue, 19 Feb 2008 16:04:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7792743</guid><dc:creator>arepetti</dc:creator><description>&lt;p&gt;class Base&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; readonly int baseFooValue = 1;&lt;/p&gt;
&lt;p&gt; public Base()&lt;/p&gt;
&lt;p&gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Console.WriteLine(Foo().ToString());&lt;/p&gt;
&lt;p&gt; }&lt;/p&gt;
&lt;p&gt; protected virtual int Foo()&lt;/p&gt;
&lt;p&gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return baseFooValue ;&lt;/p&gt;
&lt;p&gt; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;class Derived : Base&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; readonly derivedFooValue = 2;&lt;/p&gt;
&lt;p&gt; public Derived()&lt;/p&gt;
&lt;p&gt; {&lt;/p&gt;
&lt;p&gt; }&lt;/p&gt;
&lt;p&gt; protected override int Foo()&lt;/p&gt;
&lt;p&gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return derivedFooValue ;&lt;/p&gt;
&lt;p&gt; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This will print &amp;quot;2&amp;quot; but with the expected behavior it would print &amp;quot;0&amp;quot;!&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7837177</link><pubDate>Thu, 21 Feb 2008 16:20:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7837177</guid><dc:creator>Luke</dc:creator><description>&lt;p&gt;Removing readonly modifier does not make any difference in the result. Am I missing something?&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#7938280</link><pubDate>Fri, 29 Feb 2008 02:35:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7938280</guid><dc:creator>Kyralessa</dc:creator><description>&lt;p&gt;It's interesting to note that in VB .NET this isn't confusing at all. &amp;nbsp;The syntax makes it clear what happens. &amp;nbsp;If you explicitly call the base constructor, and you do it like this...&lt;/p&gt;
&lt;p&gt;Public Sub New()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;' some other code, and then:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;MyBase.New()&lt;/p&gt;
&lt;p&gt;End Sub&lt;/p&gt;
&lt;p&gt;...the compiler tells you the base constructor call has to be the first line. &amp;nbsp;Which makes it obvious, since it looks like a method call, that the base constructor's body gets executed first.&lt;/p&gt;
&lt;p&gt;The C# syntax, unfortunately, is a bit more obtuse to those not familiar with it.&lt;/p&gt;
</description></item><item><title>Community Convergence XLI</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8160905</link><pubDate>Wed, 12 Mar 2008 00:35:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8160905</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Welcome to the forty-first Community Convergence. The big news this week is that we have moved Future&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8173107</link><pubDate>Wed, 12 Mar 2008 22:37:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8173107</guid><dc:creator>Ben Voigt</dc:creator><description>&lt;p&gt;Wouldn't the following order make the most sense (since base initializers have no way of referring to derived variables, but the reverse isn't true)? &amp;nbsp;Or can you provide a counter-example?&lt;/p&gt;
&lt;p&gt;base initializer&lt;/p&gt;
&lt;p&gt;derived initializer&lt;/p&gt;
&lt;p&gt;base constructor&lt;/p&gt;
&lt;p&gt;derived constructor&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8336085</link><pubDate>Tue, 25 Mar 2008 19:57:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8336085</guid><dc:creator>Jim</dc:creator><description>&lt;p&gt;I think your point is: If the the initialization is not done first, then if the Derived class tried to reference the base.baseFoo.Bar() function in it's constructor baseFoo may be a null reference, correct?&lt;/p&gt;
&lt;p&gt;public Derived()&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;base.baseFoo.Bar();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Derived constructor&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8339667</link><pubDate>Thu, 27 Mar 2008 16:50:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8339667</guid><dc:creator>Cerror</dc:creator><description>&lt;p&gt;When we call Derived() constructor, The Base object should be created first, include all of its field. So of cause we can use the member of Base class in the Derived() constructor. But why should we initialize the Derived's readonly field before the Base field? I think it just let the compiler developing a little easy.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8344771</link><pubDate>Mon, 31 Mar 2008 02:17:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8344771</guid><dc:creator>arkain</dc:creator><description>&lt;p&gt;This shouldn't be as confusing as it seems. Consider what the compiler sees when I tries to construct A:&lt;/p&gt;
&lt;p&gt;public Class Base { ... }&lt;/p&gt;
&lt;p&gt;public Class Derived1 : Base { ... }&lt;/p&gt;
&lt;p&gt;public Class Derived2 : Derived1 { ... }&lt;/p&gt;
&lt;p&gt;Derived2 A;&lt;/p&gt;
&lt;p&gt;The compiler only knows that A is of type Derived2. So it has no choice but to depend on Derived2 to have all of the info required to build any ancestor classes. That's where initializers come in. The constructor for a class always uses its initializer to call the constructor of its base class before it does anything else. This should explain why the initializers are called first and in reverse order.&lt;/p&gt;
&lt;p&gt;When the last initializer calls the lowest base class constructor, there's no more base classes in the chain, so now it's time to execute all the constuctors. As each constructor is completed, the stack unwinds and each constructor is executed in turn from the base to the most derived. Hence the forward order of constructor execution. &lt;/p&gt;
&lt;p&gt;For VB fans, consider that your objects don't have initializers. This means that base class constructors have to be called manually. As such, the only way to ensure that the entire class is properly constructed before executing dangerous constructor content in the derived class is to make sure that the base constructor is called as the very first line of the derived class's constructor. This makes VB class construction function identically to that in C++ and C#.&lt;/p&gt;
&lt;p&gt;When you think about it, it's just not possible to call in this order:&lt;/p&gt;
&lt;p&gt;base initializer -&amp;gt; base constructor -&amp;gt; derived initializer -&amp;gt; derived constructor&lt;/p&gt;
&lt;p&gt;Since the initializer's job is to call the ancestor's constructor, there'd be nothing for the initializers to do.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8345698</link><pubDate>Mon, 31 Mar 2008 17:36:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8345698</guid><dc:creator>kshetgar</dc:creator><description>&lt;p&gt;I agree with Arkain. It makes sense that &amp;quot;Since the initializer's job is to call the ancestor's constructor, there'd be nothing for the initializers to do.&amp;quot;&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8350161</link><pubDate>Wed, 02 Apr 2008 10:21:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8350161</guid><dc:creator>Dogu Tumerdem</dc:creator><description>&lt;p&gt;readonly modifiers causes a kind of static initialization. that's why it will be initialized in reverse manner.&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8357154</link><pubDate>Fri, 04 Apr 2008 17:59:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8357154</guid><dc:creator>Don Dillard</dc:creator><description>&lt;p&gt;The output order becomes understandable when you look at the derived class &lt;/p&gt;
&lt;p&gt;like it actually works. &amp;nbsp;Note, I put the call to the base construcctor on &lt;/p&gt;
&lt;p&gt;the Derived constructor. &lt;/p&gt;
&lt;p&gt;class Derived : Base&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Foo derivedFoo = new Foo(&amp;quot;Derived initializer&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public Derived() : base() &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;------------- where the base is actually called&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;Console.WriteLine(&amp;quot;Derived constructor&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;} &lt;/p&gt;
&lt;p&gt;If I change the Base constructor to take a Foo &lt;/p&gt;
&lt;p&gt;parameter, why can't I pass Derived.derivedFoo? &amp;nbsp;It's clearly intialized!&lt;/p&gt;
&lt;p&gt;class Base&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Foo baseFoo = new Foo(&amp;quot;Base initializer&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public Base(Foo f) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;------------------------- &amp;nbsp;change&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Base constructor&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;baseFoo = f;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;} &lt;/p&gt;
&lt;p&gt;class Derived : Base&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Foo derivedFoo = new Foo(&amp;quot;Derived initializer&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public Derived() : base(derivedFoo) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;------------- &amp;nbsp;error&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;Console.WriteLine(&amp;quot;Derived constructor&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;} &lt;/p&gt;
&lt;p&gt;Note this works: &amp;nbsp;base(new Foo(&amp;quot;bubba&amp;quot;))&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8517924</link><pubDate>Sun, 18 May 2008 07:38:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8517924</guid><dc:creator>JFront</dc:creator><description>&lt;p&gt;C# Code behind is fabulous. Thanks. &lt;/p&gt;
&lt;p&gt;JFront&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8527947</link><pubDate>Wed, 21 May 2008 15:49:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8527947</guid><dc:creator>PK</dc:creator><description>&lt;p&gt;Process of creating an Instance of a Class goes in the following order: (static would be different)&lt;/p&gt;
&lt;p&gt;Step 1) Compiler executes code related to instance variable declarations; this is to identify the fields required to construct the instance.&lt;/p&gt;
&lt;p&gt;Step 2) Compiler executes the desired constructor - Constructor is the first method that is called on an instance.&lt;/p&gt;
&lt;p&gt;Trick is here: To construct a Derived class instance a base class instance needs to be constructed.&lt;/p&gt;
&lt;p&gt;Let’s get back to the code we are looking at:&lt;/p&gt;
&lt;p&gt;1) Line 'new Derived();' of the 'static void Main()' method creates an instance of 'Derived' class. So the compiler should run Step 1 on 'Derived' class.&lt;/p&gt;
&lt;p&gt;Result: 'Foo constructor: Derived initializer' is written to the console.&lt;/p&gt;
&lt;p&gt;2) Compiler should run Step 2 on 'Derived' class. To create an instance of the derived class it first needs to construct the base class instance. Hence,&lt;/p&gt;
&lt;p&gt;	i)	Step 1 is called on 'Base' class.&lt;/p&gt;
&lt;p&gt;		Result: 'Foo constructor: Base initializer' is written to the console.&lt;/p&gt;
&lt;p&gt;	ii) 	After Step 1, Step 2 is called on 'Base' class.&lt;/p&gt;
&lt;p&gt;		Result: 'Base constructor' is written to the console.&lt;/p&gt;
&lt;p&gt;Since the Base class instance is constructed, it finishes with constructing the Derived class by calling Step 2 on the 'Derived' class&lt;/p&gt;
&lt;p&gt;Result: 'Derived constructor' is written to the console.&lt;/p&gt;
&lt;p&gt;Guess this helps to understand the behavior.&lt;/p&gt;
</description></item><item><title>Order of Construction</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8569514</link><pubDate>Mon, 02 Jun 2008 18:07:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8569514</guid><dc:creator>Jason Crease</dc:creator><description>&lt;p&gt;For me, inheritance is often a headache. In particular, in what order is everything initialized? ...&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8572095</link><pubDate>Tue, 03 Jun 2008 23:26:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8572095</guid><dc:creator>latek</dc:creator><description>&lt;p&gt;As PK explained, &lt;/p&gt;
&lt;p&gt;Order execution is a recursive process:&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;p&gt;BASE CLASS A&lt;/p&gt;
&lt;p&gt;DERIVED CLASS B : A&lt;/p&gt;
&lt;p&gt;DERIVER CLASS C : B&lt;/p&gt;
&lt;p&gt;If we like to make instance of class C, it will go like this&lt;/p&gt;
&lt;p&gt;in this pleudocode will look like this:&lt;/p&gt;
&lt;p&gt;MAKEINSTANCE (C)&lt;/p&gt;
&lt;p&gt;VOID MAKEINSTANCE (type X)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; 1. identify and initialise the local fields required to construct the instance X&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; 2. if X is derived class &amp;nbsp;then &amp;nbsp;MAKEINSTANCE (base of X) &amp;nbsp;//recursive call&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; 3. call X constructor&lt;/p&gt;
&lt;p&gt;END VOID&lt;/p&gt;
&lt;p&gt;So the execution path will be:&lt;/p&gt;
&lt;p&gt;Fields Initialising C&lt;/p&gt;
&lt;p&gt; &amp;nbsp; //check, C is derived from B&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Fields Initialising B &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; //check B is derived from A&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; Fields Initialisiing A &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; Call constructor A&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Call constructor B&lt;/p&gt;
&lt;p&gt;Call constructor C&lt;/p&gt;
&lt;p&gt;Hope &amp;nbsp;this hepls someone...&lt;/p&gt;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx#8574473</link><pubDate>Thu, 05 Jun 2008 06:02:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8574473</guid><dc:creator>J</dc:creator><description>&lt;p&gt;Base initializer or Base constructor prior to ObjectConstructor might be helpful to do better for objectconstructor. &lt;/p&gt;
</description></item><item><title>C# 小测试（一）：类成员实例化与构造函数执行的顺序</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/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;
</description></item><item><title>Mutating Readonly Structs </title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: Why Do Initializers Run In The Opposite Order As Constructors? Part One</title><link>http://blogs.msdn.com/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;
</description></item></channel></rss>