views:

262

answers:

9

Which is more conventional in C#?

class Foo
{
  private string _first;
  private string _second;

  public Foo(string first)
  {
    _first = first;
    _second = string.Empty;
  }
}

or

class Foo
{
  private string _first;
  private string _second = string.Empty;

  public Foo(string first)
  {
    _first = first;
  }
}
A: 

I like the second way. It looks more natural.

Naveen
+2  A: 

Don't know about convention, but the safer way is initializing members as in your second example. You may have multiple constructors and forget to do the init in one of them.

xanadont
A: 

I would choose the second way for primitive types (String,int, etc.).

For complex types I prefer the intitialization inside the constuctor.

Louis Haußknecht
A: 

The second way has few advantages: first of all it's easier to read, second it's more DRY, when you have more than one constructor you don't have to manually chain them or duplicate the initialization, which brings us to the third advantage - it's less error prone.

Krzysztof Koźmic
+2  A: 

Technically, there is a small variation in behavior between your snippets when calling virtual functions from the base class's constructor, but making virtual function calls during construction is bad practice anyway, so let's ignore that.

I'd prefer the second. If you have overloaded constructors, it saves some copy/pasting, and no one has to remember to write the assignment statement - the compiler takes care of all that.

If calculation of the assigned value is too complex or is cannot be assigned to a field initializer, I'd have a protected (or private) default constructor

class Foo
{
    private Foo() { _second = SomeComplexCalculation(); }
    public Foo(string first) : this()
    {
       _first = first;
    }
}

If the order of assignment matters, then I'll have a private function do the initialization.

Senthil Kumar
A: 

I usually do the initialization of variables with values passed as constructor parameters in the constructor (of course) and default initializations by directly assigning the corresponding value at the point of the definition of the member variable (as you did in your 2nd solution)

private string _second = string.Empty;

Together with properties I have a construct like the following:

 public class Foo
 {

    public Foo(...)
    {
      ...
    }

    private string _Second = string.Emtpy;
    public string Second
    {
      get
      {
        return _Second;
      }
      set
      {
        _Second = value;
      }
    }

 }
Juri
A: 

The second example you gave I would say is generally the preferred way because it saves time looking through any constructors and all the fields are initialized in a single place (from a developer's point of view).

But, when using this second style, the C# compiler generates the same initialization values of each field in every constructor present within the class.

Mike J
A: 

I am using the default keyword.

// local variable
var x = default(Action);

// member variable
this.FieldX = default(Action);

Update: added the link to msdn

zproxy
There's a 'default' keyword in .NET? wa???
Pure.Krome
@Pure.Krome it's a very important aspect of generics. a la: var myvar = default(T);
xanadont
A: 

Microsoft best practices is to initialize members while you declare them. So:

class Foo
{
  private string _first;
  private string _second = string.Empty;

  public Foo(string first)
  {
    _first = first;
  }
}

Would be the answer here.

It tells you also not to initialize members with default values.

Roubachof