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