When should we use private variables and when should we use properties.

Posted by Shantanu Gupta on Stack Overflow See other posts from Stack Overflow or by Shantanu Gupta
Published on 2010-05-26T12:30:30Z Indexed on 2010/05/26 12:31 UTC
Read the original article Hit count: 232

Filed under:
|
|
|
|

In most of the cases we usually creates a private variable and its corresponding public properties and uses them for performing our functionalities.

Everyone has different approach like some ppl uses properties every where and some uses private variables within a same class as they are private and opens it to be used by external environment by using properties.

Suppose I takes a scenario say insertion in a database. I creates some parameters that need to be initialized.

I creates 10 private variables and their corresp public properties which are given as

private string name;
public string Name
   {
     get{return name;}
      set{name=value;}
   }

and so on. In these cases what should be used internally variables or properties.

And in those cases like

public string Name
   {
     get{return name;}
     set{name=value>5?5:0;} //or any action can be done. this is just an eg.
   }

In such cases what should be done.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET