Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.58/5 (6 votes)
See more:
There are two ways to initialize members in a C# class, via constructor:
class MyClass{
  private int Member;
  public MyClass()
  {
    Member = 0;
  }
}

or through member initialization:
class MyClass
{
  private int Member = 0;
}

My question: Are there problems / benefits of one approach vs. another?

From what I can see, it's a wash if you only have a default constructor. If you have multiple constructors, member initialization is preferable for those members that are set to the same value by all constructors.
Posted
Updated 2-Jan-13 7:37am
v2

Gary,

If the class calls Finalize, then using member initialization is certainly the right way to go. The reason being, if there is an error in the constructor, and Finalize is called, then the state of the fields could come into play. Using member initialization in this case means that you know your fields are valid, so you won't have issues if finalize is called following failed object construction.

D.



Last modified: 12mins after originally posted --


 
Share this answer
 
I don't think it really matters, especially in the case of setting variables to their default values (which is recommended to not do). If you have multiple constructors, you can set the initial values in your simplest constructor and then use constructor chaining. In your first example, you actually end up with 3 additional lines of IL in the constructor to handle loading the arguement on to the evaluation stack (which it already does), then loading the 0 as an integer on to the evaluation stack, and then replacing the value stored in the "Member" field with the new value.

 
Share this answer
 
There's a subtle difference if you call virtual methods from your constructor, which is generally not advisable. When generating IL code for the constructor, the C# compiler puts code for all field initializers before code for the constructor's body This means that overridden will see initialized values for field initialized members, but will see default values for others.

<br /><br />class Base<br />{<br />   protected int x = 42;<br />   protected int y;<br /><br />   public Base()<br />   {<br />      SomeVirtualMethod();<br />      y = 42;<br />   }<br /><br />   protected virtual void SomeVirtualMethod() {}<br />}<br /><br />class Derived : Base<br />{<br />   protected override void SomeVirtualMethod()<br />   {<br />      Console.WriteLine(x);<br />      Console.WriteLine(y);<br />   }<br />}<br /><br />


prints 42 and 0.

 
Share this answer
 
It will also affect debugging when you step into code so I often prefer to do initialization in the constructor as it make debugging somewhat easier.
 
Share this answer
 
Gary, in your case, this will do:
class MyClass<br />{<br />  private int Member;<br />}
Class level members are always initialized to default values. Strings are empty, numbers are 0, and bools are false.

 
Share this answer
 
Comments
David St. Hilaire 2-Jan-13 16:28pm    
I believe strings are initialized to null.
FXCop will trigger an "issue" if you do the 2nd thing, but FXCop is, after all, just a style guide and most of what it chokes/pukes on can safely be ignored. I tend to use method #2 in your examples.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900