my question is simple, is using the get set properties of C# considered good, better even than writing getter and setter methods? When you use these properties, don’t you have to declare your class data members as public ? I ask this because my professor stated that data members should never be declared as public, as it is considered bad practice.
This….
class GetSetExample
{
public int someInt { get; set; }
}
vs This…
class NonGetSetExample
{
private int someInt;
}
Edit:
Thanks to all of you! All of your answers helped me out, and I appropriately up-voted your answers.
This:
is really the same as this:
The
get; set;syntax is just a convenient shorthand for this that you can use when the getter and setter don’t do anything special.Thus, you are not exposing a public member, you are defining a private member and providing get/set methods to access it.