Which is recommended for initialization of class fields in C#:class Foo{    public X x = new X(); // or any default value...    public Foo(X _x)    {       x = _x;    }}Orclass Foo{    public X x;    public Foo(X _x)    {       x = _x;    }}Please note in the second version, it is probable that I introduce other constructors where x may not be initialized!
Should I initialize a member variable in declaration when it is initialized by a constructor parameter?
c#;object oriented;initialization
You should choose between init in declaration or constructor, not both.In a language that has a default constructor where you don't have to explicitly provide a constructor if the default is all you need, a declaration init may be fine. As soon as you need an explicit constructor with parameter, I tend to move everything to constructor(s), to ensure that init behaviour is in one place.Too be honest, I hardly ever use declaration init, but that may just be preference. My perceived advantage is that all init behaviour is always encapsulated in constructors: I never have to look in two places (or miss it up in the variable declaration).Re: it is probable that I introduce other constructors where x may not be initializedYou should always code constructors to re-use another constructor as appropriate. E.g. in your example, adding another constructor with additional param could be done like this:class Foo{    public X x;    public Y y;    public Foo(X _x)    {       x = _x ?? new X();    }    //WRONG!    public Foo(X _x, Y _y)    {       x = _x;       y = _y;    }    //Right    public Foo(X _x, Y _y)    {       this(_x);       y = _y ?? new Y();    }}Finally, when delegating to another constructor, always prefer this() to base() (super() in Java) since it adds to local cohesion. For more detail on this() vs base() in C#, see e.g. https://stackoverflow.com/questions/3797528/base-and-this-constructors-best-practices