Re: Best practice and is there a difference?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Jim H" <jimh@xxxxxxxxxxxxx> wrote in
news:#O9C#ujpJHA.996@xxxxxxxxxxxxxxxxxxxx:

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

.



Relevant Pages

  • no default constructor -- bad form?
    ... question (which a C++ question, not a windows question), but the class ... and to make an sense the class must know what the member is. ... But this puts me in the position of having no default constructor, ... I have to initialize x_ to something meaningful when ...
    (comp.lang.cpp)
  • Member Initialization Gotcha!
    ... This only took a few hours to figure out after I finally went back to MSDN and saw this warning regarding member initialization: ... So you would think that two protected const DWORD members shown here would be constructed in its declared order: ... Well, never mind the above uses D1 to initialize D2, the above is drastically reduce code for illustration. ... It showed up when the original constructor: ...
    (microsoft.public.vc.language)
  • Re: public static final - compilation error
    ... > is it possible to initialize a public static final member of a class in ... > the class' constructor? ... Prev by Date: ...
    (comp.lang.java.help)
  • initializing non-static consts
    ... I'm learning consts in C++ and the book says that u have to initialize ... non-static consts inside the constructor initializer list, ... "const string* stack" isn't initialized in the constructor ...
    (comp.lang.cpp)
  • Re: Shared classes or modules?
    ... initialization code that is executed the very first time you use any member ... I could have the Sub New above open the log file the first time I went ... Other posts say that a module have no default constructor but it can ... >> Also you should make the constructor private, to prevent instantiating ...
    (microsoft.public.dotnet.languages.vb)