Marcus25
Marcus25

Reputation: 873

Using properties in C#, outside class can still access private variables ? Need some elaboration on this

I have this question about get set accessors in C#.

{
   private string _mvalue
   public string MyValue 
   {
       get
       {
           return mvalue;
       }
       set
       {
           _mvalue = value;
       }
}

Here, _mvalue is private. We made it private so that it should not be accessible outside the class. But then again, we created a property 'MyValue' over this private variable. Using property we can access the private variable. So don't you think we are compromising over the privateness of the variable. I mean, the variable is meant to be private. But, with the help of proprties, outsiders can still access this variable

Upvotes: 0

Views: 2912

Answers (4)

Sarath Subramanian
Sarath Subramanian

Reputation: 21271

First thing is the private variable "_mvalue" cannot be accessed from outside world as it is Private. Secondly, if you want to really protect "MyValue", ie, outsiders can access only "Get" property, then use private keyword with "set".

private string _mvalue;    
public string MyValue     
{
        get
       {
           return _mvalue;
       }
       private set
       {
           _mvalue = value;
       }   

}

Upvotes: 0

Kumareshan
Kumareshan

Reputation: 1341

Usually, we go for private variables in property when we want to do some additionally operations during set or get of the value. For example I always want to multiple the assigned value with 5, in that case we can use some thing like this

public int A
{
   get
   {
      return a;
   }
   set 
  {
     a=value*5;
  }
} 

But if you just want to store a value that should be accessed by outside the class in that case you can write a property with out private variable as below

 public int A
 {
   get;
   set;
 } 

if you want only to expose the value, but no one should set the value out side the class then you can even make set as private.

Always property is to expose the values outside the class, but it depends on your logic how you want to write, you can use private variables within that or you needed not, usually private variable is used with the property when it is Bind the to Wpf controls(MVVM) to implement INotifyPropertyChanged.

If your using private variable in a property without any custom logic in get or set, then it is bad programming style, it is like exposing a private member out side the class

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

This is perfectly fine. No one is making you add a public property to your class. If you really want _mvalue to really stay private, just don't make the public property that uses it.

Upvotes: 3

NoChance
NoChance

Reputation: 5752

_mvalue is not created to make the value of MyValue private. _mvalue is required to maitain the state of the public propery MyValue. No user of this object can access _mvalue as long as it is private. Also, MyValue may contain a different value than _mvalue. For example, you could return _mvalue+"/". Add to this the fact that not every private field like _mvalue need to have a public property of course.

There are several ways to prevent access to a data variable such as:

  • Use private members only

  • use a private property

The confusion may be reduced by using Automatic Properties, refer to the accepted answer in: Automatic Property.

Upvotes: 3

Related Questions