If I have a need for simple, private fields, is there any reason I shouldn't just make it a convention to use private, auto-implemented properties instead?
For instance, I could do this:
private MyClass _foo;
or I could do this:
private MyClass Foo { get; set; }
It seems like the only time I need actual fields is when I'm doing something like lazy loading:
private MyClass Foo
{
get
{
if (_foo == null) _foo = new MyClass();
return _foo;
}
}
Best Answer
The two features of fields that I feel you might run into more commonly that you would lose by converting them to properties are the following:
You can't modify members of a property value if it's a value type.
You can't pass a property as an
out
orref
parameterIf neither of these is an issue for you, then you probably won't notice the difference. There are of course other differences if you're reflecting over your type, or using libraries that reflect over your type and expect things to be properties or fields, etc.