hello buddies...
plz help me out understanding folowing stuff... i hav searchd for it in many books but didnt understand...
why in evry class, for evry member we use get and set method...
for ex.
private string firstName;
public string FirstName
{
get { return firstName;}
set {firstName = value; }
}
i have seen such stuff in many languages but dont knw y we use thm??
i hav made programs without evn using such things...
plz help me out guys... plz help me understand the significance of these coding methods... thanks in advance...
Loading
Kirtan PatelPosted Dec 12, 2009, 2:25 PM
Get and Set is used to Define Property for the class .
Property is Used to Modify Private Variables in class .Private variables are normally modifiable only by the functions inside the class .so for modifying it from out side of class we need to define property .
in your given example firstName is Private Member so it can only be modified by any class method but what if we want to modify this private variable of class from method out side of class ? . so without Defining the Property you can not modify private Variable of the class from out side method .
in property Get Will return the Current value of private variable ans Set Will assing new value to the Private variable
friend, if my answer helped you to understand the concept simply then check "Do you like this answer" check box please :)
Hiren SoniPosted Dec 13, 2009, 12:40 AM
Mahesh ChandPosted Dec 12, 2009, 7:52 PM
Nonetheless, this is the syntax for implementing properties in C#.
Baimey RajeshPosted Dec 12, 2009, 2:26 PM
Please be clear about C# concept.It is object oriented.
In C# we dontgive direct exposure for private fields, again as the best practice fields are made private. We wrap it in a property and expose it to the other classes be declaring it public.
It is a art of encapsulation and abstraction.
Please let me know if it is not clear still :)
Please mark if this helps.....
Thanks
Paru