Emmett
Emmett

Reputation: 14327

What is the conventional way to assign default values to member variables in C#?

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;
  }
}

Upvotes: -2

Views: 599

Answers (9)

zproxy
zproxy

Reputation: 3549

I am using the default keyword.

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

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

Update: added the link to msdn

Upvotes: 0

Roubachof
Roubachof

Reputation: 3401

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.

Upvotes: 0

Mike J
Mike J

Reputation: 3149

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.

Upvotes: 0

Juri
Juri

Reputation: 32920

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;
      }
    }

 }

Upvotes: 0

Senthil Kumar
Senthil Kumar

Reputation: 479

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.

Upvotes: 2

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27374

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.

Upvotes: 0

Louis Haußknecht
Louis Haußknecht

Reputation: 924

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

For complex types I prefer the intitialization inside the constuctor.

Upvotes: 0

xanadont
xanadont

Reputation: 7604

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.

Upvotes: 2

Naveen
Naveen

Reputation: 73443

I like the second way. It looks more natural.

Upvotes: 0

Related Questions