Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use a private variable in a property accessor?

Tags:

Sorry If I am being noob, I have this doubt, why do we use private variables and set them using properties ?

Why can't we just use properites alone ?

I am talking about situations like this

private string _testVariable;  public string MyProperty {     get { return _testVariable;}     set {_testVariable = value;} } 

I am thinking of simply using

public string MyProperty { get; set; }  

Why redundant private variable? are these two strategies different ? can anyone please throw some light on this.

Thanks

like image 393
Mahesh Velaga Avatar asked Dec 25 '09 17:12

Mahesh Velaga


People also ask

What is the use of private variables even though they are accessible via getters and setters from some other class?

In classes, variables are often made private for encapsulation, and to limit the variables to a certain scope allow better error control and fewer bugs. This makes sense, as the fewer places a variable can be accessed the fewer places a bug can occur with that variable.

Why is it better to declare class attributes member variables private rather than public?

private data members are generally considered good because they provide encapsulation. Providing getters and setters for them breaks that encapsulation, but it's still better than public data members because there's only once access point to that data.

Why we use properties instead of public variables?

Property always a better choice instead of public variables. Property is safe while public variables are unsafe. And you can not debug with public variables but you can do that with property. Public variables are useful.

What is a private variable in C#?

Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members. Create a private variable −


2 Answers

Your examples are semantically the same. The concise property declaration syntax (just having { get; set; }) is a shortcut available in C# 3.0. The compiler actually creates a private backing variable and a simple getter and setter as in your first example.

If all you're doing is creating a getter and setter (and nothing actually happens when either occurs), then the concise syntax is a good option. If you have to perform any other actions (redraw a control, for example) when you set the value, then the full syntax is required.

like image 88
Adam Robinson Avatar answered Oct 02 '22 18:10

Adam Robinson


Why redundant private variable? are these two strategies different ? can anyone please throw some light on this.

If all your doing is reading/writing a variable, then no. Otherwise, there's two reasons why you'd want a private variable:

Data validation

// Data validation public class IntWrapper {     private int _value;     public int Value     {         get { return _value; }         set         {             if (value < 0) { throw new Exception("Value must be >= 0"); }             _value = value;         }     } } 

Getter/setter wraps up an underlying data store

public class StringBuffer {     List<char> chars = new List<char>();      // Wraps up an underlying data store     public string Value     {         get { return new String(chars.ToArray()); }         set { chars = new List<char>(value.ToCharArray()); }     }      public void Write(string s) { Write(chars.Count, s); }      public void Write(int index, string s)     {         if (index > chars.Count) { throw new Exception("Out of Range"); }         foreach(char c in s)         {             if (index < chars.Count) { chars[index] = c; }             else { chars.Add(c); }             index++;         }     } } 
like image 43
Juliet Avatar answered Oct 02 '22 19:10

Juliet