M
mp
trying to understand properties
normally i would create a private variable and return that in a get, assign
it in a set
class TestProperty
{
private List<string> _someList= new List<string>();
public List<string> SomeList { get{return _someList;} set{_someList
= value;} }
...}
i see in c# the type of property that doesn't use a variable (must create a
hidden var behind the scenes or something...)
class TestProperty
{
public string strProp { get; set; }
...}
I got to wondering if I was wasting a variable by using the first (more
verbose) style
so i tried it with a list but that didn't work because (naturally) the list
was null
class TestProperty
{
public List<string> SomeList { get; set; }
public TestProperty()
{
}
public void Test()
{
// A first chance exception of type
'System.NullReferenceException'
SomeList.Add ("Test1");
}
}
yet with a string it does work...although a string is also an object so not
sure why i didn't get the same null reference exception
class TestProperty
{
public string strProp { get; set; }
public TestProperty()
{
}
public void Test()
{ //this works
strProp = "Test strprop";
}
}
appreciate any background or sites that explain why the difference...
i searched msdn and most examples have the more verbose style with private
variable returned from a property
there's probably a good explanation in there somewhere but i haven't
stumbled on to it yet.
and maybe the style that doesn't use a variable isn't any more efficient
than the one that does
i imagine if the class doesn't have to interact with the value during its
lifetime other than to receive a setting from client and return it when
asked, then the variable is of not much use...
but if the class in any method needs to manipulate the variable, then the
private var is useful(though it could probably access it's own property i
would guess...so i'm not sure about that either)
thanks for any input
mark
normally i would create a private variable and return that in a get, assign
it in a set
class TestProperty
{
private List<string> _someList= new List<string>();
public List<string> SomeList { get{return _someList;} set{_someList
= value;} }
...}
i see in c# the type of property that doesn't use a variable (must create a
hidden var behind the scenes or something...)
class TestProperty
{
public string strProp { get; set; }
...}
I got to wondering if I was wasting a variable by using the first (more
verbose) style
so i tried it with a list but that didn't work because (naturally) the list
was null
class TestProperty
{
public List<string> SomeList { get; set; }
public TestProperty()
{
}
public void Test()
{
// A first chance exception of type
'System.NullReferenceException'
SomeList.Add ("Test1");
}
}
yet with a string it does work...although a string is also an object so not
sure why i didn't get the same null reference exception
class TestProperty
{
public string strProp { get; set; }
public TestProperty()
{
}
public void Test()
{ //this works
strProp = "Test strprop";
}
}
appreciate any background or sites that explain why the difference...
i searched msdn and most examples have the more verbose style with private
variable returned from a property
there's probably a good explanation in there somewhere but i haven't
stumbled on to it yet.
and maybe the style that doesn't use a variable isn't any more efficient
than the one that does
i imagine if the class doesn't have to interact with the value during its
lifetime other than to receive a setting from client and return it when
asked, then the variable is of not much use...
but if the class in any method needs to manipulate the variable, then the
private var is useful(though it could probably access it's own property i
would guess...so i'm not sure about that either)
thanks for any input
mark