Best practice and is there a difference?

J

Jim H

When creating member objects in a C# class what's the difference between
initializing the member where it's defined and using a constructor. Is
there a best practice for this. I come from a C/C++ background and like
constructors, but I don't know if there is a performance and/or readability
preference in C#. This is for basic classes and not classes where the
constructor takes arguments.


example:

public class A
{
private String string1;
private int number1;
private List<String> bigList;

public A()
{
string1 = String.Empty;
number1 = 16;
bigList = new List<String>();
}
}

or

public class A
{
private String string1 = String.Empty;
private int number1 = 16;
private List<String> bigList = new List<String>();

}



Side question: Do the new preoperty defs with anonymous members need to be
initialized manually or in the constructor, or does it happen automatically?

example:
public class A
{
public String FileName { get; set; }
}
 
J

Jeff Johnson

When creating member objects in a C# class what's the difference between
initializing the member where it's defined and using a constructor. Is
there a best practice for this. I come from a C/C++ background and like
constructors, but I don't know if there is a performance and/or
readability preference in C#. This is for basic classes and not classes
where the constructor takes arguments.

I go back and forth on this one myself. Most of the time I initialize in the
declaration, but sometimes I do it in the constructor. Most of the time that
I do it in the constructor is when I have lists or dictionaries that I'm
going to fill during construction. "One-off" variables (especially value
types) I almost never do in the constructor. But that's just my preference.
Side question: Do the new preoperty defs with anonymous members need to be
initialized manually or in the constructor, or does it happen
automatically?

example:
public class A
{
public String FileName { get; set; }
}

Every private member variable (including the hidden ones created for
automatic properties) is initialized to its default value automatically.
Numerics get 0, reference types get null, etc.
 
J

Jeroen Mostert

Jim said:
When creating member objects in a C# class what's the difference between
initializing the member where it's defined and using a constructor.

None, except for the order in which fields are initialized. If the order
matters, use a constructor and be explicit, because you're setting yourself
up for a maintenance problem otherwise. Fields are initialized in the order
they are declared, but people would expect to be able to move the
declarations around without this causing problems.
Is there a best practice for this. I come from a C/C++ background and
like constructors, but I don't know if there is a performance and/or
readability preference in C#.

There is no performance benefit either way, and readability isn't part of
the language, so use whatever you think is best.
Side question: Do the new preoperty defs with anonymous members need to be
initialized manually or in the constructor, or does it happen automatically?
Automatically. An automatic property is just a property with an implicit
backing field. Functionally it's no different from a regular property, and
unlike C++, there is no such thing as an uninitialized field in C#. In some
scenarios your initialization may not have run yet, but there's always
default initialization.
public class A
{
public String FileName { get; set; }
}
The (implicitly named) backing field of FileName will be initialized to null.
 
J

Joe Greer

When creating member objects in a C# class what's the difference
between initializing the member where it's defined and using a
constructor. Is there a best practice for this. I come from a C/C++
background and like constructors, but I don't know if there is a
performance and/or readability preference in C#. This is for basic
classes and not classes where the constructor takes arguments.


example:

public class A
{
private String string1;
private int number1;
private List<String> bigList;

public A()
{
string1 = String.Empty;
number1 = 16;
bigList = new List<String>();
}
}

or

public class A
{
private String string1 = String.Empty;
private int number1 = 16;
private List<String> bigList = new List<String>();

}



Side question: Do the new preoperty defs with anonymous members need
to be initialized manually or in the constructor, or does it happen
automatically?

example:
public class A
{
public String FileName { get; set; }
}

Here is my take on it, fwiw. Barring special needs (always have to have
the disclaimer...), If a members value doesn't depend upon parameters
passed to the constructor, I go ahead and initialize them where it is
defined. Since all objects in C++ have to be new-ed to exist, this
works out to be similar to the default constructor in C++. For members
which take on values based upon parameters, I initialize them in the
constructor. So, I might have...

public class A
{
private List<string> myStrings = new List<string>();
private ExternalState myExternalState;

A(ExternalState externalState)
{
myExternalState = externalState;
myStrings.Add("one");
}
}

In the above case, while I create the list at the member declaration, I
still have to add elements to it in the constructor (just as you would
in C++).

So, in short, I use member declaration initialization to take the place
of default construction and otherwise put things into the constructor.

joe
 
A

Arne Vajhøj

Jeroen said:
None, except for the order in which fields are initialized. If the order
matters, use a constructor and be explicit, because you're setting
yourself up for a maintenance problem otherwise. Fields are initialized
in the order they are declared, but people would expect to be able to
move the declarations around without this causing problems.

I very strongly agree with this.

Initialization in constructor gives a clear sequential flow
that should be obvious to any reader.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top