Saturday 15 March 2014

.net - Is using get set properties of C# considered good practice? -


The question is simple, as well as similar properties in C #, in addition to writing the gutter and setter methods, Is considered better? When you use these properties, do not you have to declare your class data members as public? I ask this because my professor has said that the data members should never be declared in public, as it is considered bad behavior because never should be declared public.

This ....

  class GetSetExample {public int someInt {get; Set; }}  

vs. it ...

  class NonGetSetExample {private int someInt; }  

Edit:

Thank you for all! All your answers helped me, and I picked up your answers properly.

It:

  class GetSetExample {public int someInt {get; Set; }}  

In fact follows:

  class GetSetExample {private int _someInt; Public int someInt {get {return_someInt; } Set {_someInt = value; }}}  

get ;

In this way, you are not exposing a public member, you are defining;

Provide a private member and get / set methods to achieve it.


No comments:

Post a Comment