Translate

Wednesday, August 29, 2012

Why we use property in c#? (get, set)


Properties are a way to control access to your private members. It simply (set) copies the given value to the private member and (get) returns the private member without modifying it. Often, the set part is used to check the given value in order to avoid later errors, or to simplify code

private string myString = "";

public string MyString
{
set
{ if ( value == null )
{value = "";}
myString = value;
}
}

No comments:

Post a Comment