Have looked around but I could not find an answer to this question.
Is there a difference between these two field initialisations? Or does it really not matter and is it just a matter of style.

class aClass
    {
        private int aField;

        public aClass() //default constructor
        {
            aField = 42;
        }
    }

or

class aClass
    {
        private int aField = 42;

        public aClass() //default constructor
        {
        }
    }

Well there is a little bit of difference.

First:

private void button1_Click(object sender, EventArgs e)
    {
      System.Diagnostics.Debugger.Break(); //Hit "step into"
      Ctors ct = new Ctors();
      Console.WriteLine(
        "Instance: " + ct.aMemberField.ToString());
    }
  }

  internal class Ctors
  {
    private int _aMemberField = 41; //set a breakpoint
    public int aMemberField { get { return _aMemberField; } }

    public Ctors() //member ctor
    {
      _aMemberField = 42;
    }
  }

Hit the "Step Into" with the debugger on the .Break() line. You will see that your debugger steps in to each field being initialized outside of the ctor. This makes it a pain to step over all of the field initializing! If it was in the ctor you could put a break point after the constructor.

Next initializing the field values outside of the ctor may make it harder to tell which is the final value being set at a glance. This really boils down to someone not being careful when reviewing the code -- but sometimes it is better to help yourself be careful :)

internal class Ctors
  {
    private int _aMemberField = 41; //set a breakpoint
    public int aMemberField { get { return _aMemberField; } }

    public Ctors() //member ctor
    {
      _aMemberField = 42;
    }

    public Ctors(string someParam)
      : this()
    {
      _aMemberField = 43;
    }
  }

Obviously the 43 value wins out.

Lastly the constructor is called last in this case. All of the fields are initialized before the stack enters the ctor method. So it is different in terms of call order but there is no way to hook an event in between field initialization and the ctor being called so it doesn't make a difference.

All of this holds true for static constructors as well:

internal class Ctors
  {
    private int _aMemberField = 41; //set a breakpoint
    public int aMemberField { get { return _aMemberField; } }

    public Ctors() //member ctor
    {
      _aMemberField = 42;
    }

    private static int _aStaticField = 41;
    public static int aStaticField { get { return _aStaticField; } }
    static Ctors() //static ctor
    {
      _aStaticField = 42;
    }
  }

calling it:

private void button1_Click(object sender, EventArgs e)
    {
      System.Diagnostics.Debugger.Break(); //Hit "step into"
      Ctors ct = new Ctors();
      Console.WriteLine(
        "Instance: " + ct.aMemberField.ToString() + Environment.NewLine +
        "Static: " + Ctors.aStaticField.ToString());
    }

The real thing that bugs the ___ out of me with field initializations is not being able to step over all of the initializations in the debugger since you're not in a method. You could set the debugger to the first line of the ctor and hit run but that requires more clicks. I guess it boils down to preference :)

commented: Great! +9
commented: Great! I appreciate your knowledge. +11

Wow :-O thanks Scott!
You cleared some things up for me here.
Only change I made was Console.WriteLine to MessageBox.Show.
Works better here;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.