J
Jon Poploskie
Hello,
I am looking for opinions on property vs. (instance) variable use within a
class. Say I have the following class:
public class Dog
{
private string _name;
private string _breed;
public string Name
{
get { return _name; }
set { _name = (value == null ? "spot" : value; ) }
}
public string Breed
{
get { return _breed; }
set { _breed = value; }
}
public Dog(string name, string breed)
{
// how to set??
}
}
My question is, what is the best practice for using properties or variable
INSIDE the class in which they're defined? For example, in the constructor
above, should it look like:
{
_name = name; (or _name = (name == null ? "spot" : name; )
_breed = breed;
}
or
{
Name = name; // use property
Breed = breed; // use property
}
Is there a general rule, or does it depend on the semantics of where it's
being set (should _name automatically be set to "spot" in the constructor?),
or???
Thank you all for your input.
Jon
I am looking for opinions on property vs. (instance) variable use within a
class. Say I have the following class:
public class Dog
{
private string _name;
private string _breed;
public string Name
{
get { return _name; }
set { _name = (value == null ? "spot" : value; ) }
}
public string Breed
{
get { return _breed; }
set { _breed = value; }
}
public Dog(string name, string breed)
{
// how to set??
}
}
My question is, what is the best practice for using properties or variable
INSIDE the class in which they're defined? For example, in the constructor
above, should it look like:
{
_name = name; (or _name = (name == null ? "spot" : name; )
_breed = breed;
}
or
{
Name = name; // use property
Breed = breed; // use property
}
Is there a general rule, or does it depend on the semantics of where it's
being set (should _name automatically be set to "spot" in the constructor?),
or???
Thank you all for your input.
Jon