How do you initialize automatic list properties?
-
publc class Thing
{
Thing () {}public List<Foo> MyFoos {get; set;}
}How do you get the list to be defaulted to an empty list rather than null? Do I need to implement a private backing variable?
Either use a backing field and lazy initialization
public class Thing
{
private List m_List;Thing() { }
public List MyFoos
{
get
{
if(m_List == null)
m_List = new List();return m\_List; } set{ m\_List = value; }
}
or use the constructor
public class Thing
{
Thing()
{
MyFoos = new List();
}public List MyFoos {get; set;}
}
I know the language. I've read a book. - _Madmatt
-
publc class Thing
{
Thing () {}public List<Foo> MyFoos {get; set;}
}How do you get the list to be defaulted to an empty list rather than null? Do I need to implement a private backing variable?
-
Either use a backing field and lazy initialization
public class Thing
{
private List m_List;Thing() { }
public List MyFoos
{
get
{
if(m_List == null)
m_List = new List();return m\_List; } set{ m\_List = value; }
}
or use the constructor
public class Thing
{
Thing()
{
MyFoos = new List();
}public List MyFoos {get; set;}
}
I know the language. I've read a book. - _Madmatt
Excellent answer... (Because I do it this way, of course.)
I wasn't, now I am, then I won't be anymore.
-
Either use a backing field and lazy initialization
public class Thing
{
private List m_List;Thing() { }
public List MyFoos
{
get
{
if(m_List == null)
m_List = new List();return m\_List; } set{ m\_List = value; }
}
or use the constructor
public class Thing
{
Thing()
{
MyFoos = new List();
}public List MyFoos {get; set;}
}
I know the language. I've read a book. - _Madmatt
-
Do you have to do it in every constructor? I put it in the default constructor but it doesn't seem to work when I instatiate using a different constructor.
You need to append :basename() to the constructor definitions.
public class MySuperAwesomeClass
{
public int AwesomeOne { get; set; }
public int NotAsAwesome { get; set; }
public string UselessCrap { get; set; }public MySuperAwesomeClass() { AwesomeOne = 42; NotAsAwesome = -1; } public MySuperAwesomeClass( string someValue ) : base() { UselessCrap = someValue; }
}
I wasn't, now I am, then I won't be anymore.
-
You need to append :basename() to the constructor definitions.
public class MySuperAwesomeClass
{
public int AwesomeOne { get; set; }
public int NotAsAwesome { get; set; }
public string UselessCrap { get; set; }public MySuperAwesomeClass() { AwesomeOne = 42; NotAsAwesome = -1; } public MySuperAwesomeClass( string someValue ) : base() { UselessCrap = someValue; }
}
I wasn't, now I am, then I won't be anymore.
-
Appending :base() doesn't run the default constructor for that class. The debugger never enters the default constructor and all my list properties are still null if I instantiate using a different constructor.
Rather than append
:base()
, you should use:this()
to call the default constructor in the current class. The use ofbase
implies a call to a base class, whereasthis
refers to the current class.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
You need to append :basename() to the constructor definitions.
public class MySuperAwesomeClass
{
public int AwesomeOne { get; set; }
public int NotAsAwesome { get; set; }
public string UselessCrap { get; set; }public MySuperAwesomeClass() { AwesomeOne = 42; NotAsAwesome = -1; } public MySuperAwesomeClass( string someValue ) : base() { UselessCrap = someValue; }
}
I wasn't, now I am, then I won't be anymore.
Not quite. I suspect you're thinking of
:this()
instead.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads