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
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.
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.
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.
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 −
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.
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++; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With