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 PatelRank #292 · 8.6K reputationPosted Dec 12, 2009, 9:25 AM EST
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 :)
Baimey RajeshRank #1,338 · 1.3K reputationPosted Dec 12, 2009, 9:26 AM EST
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
Mahesh ChandRank #1 · 393.4K reputationPosted Dec 12, 2009, 2:52 PM EST
Nonetheless, this is the syntax for implementing properties in C#.
Hiren SoniRank #694 · 3.5K reputationPosted Dec 12, 2009, 7:40 PM EST