Can you tell me what is the exact usage of properties in C# i mean practical explanation
in our project we are using properties like
///
Here's a common pattern:
class Foo {
private Bar _bar;
//here, Foo has a Bar object. If that object has already been instantiated, return that value. Otherwise, get it from the database.
public Bar bar {
set { _bar = value;}
get {
if (_bar == null) {
_bar = Bar.find_by_foo_name(this._name);
}
return _bar;
}
}
}
In short, this allows us to access the Bar object on our instance of Foo. This encapsulation means we don't have to worry about how Bar is retrieved, or if foo.bar has already been instantiated. We can just use the object and let the internals of the Foo class take care of it.