Hi
I recently came to the C# world. I have seen many get/set method variations. Which one do you use and I should use?
I listed them below:
private string name; public string Name {get { return name; }set { name = value; }}
public string name; // Why would the name be public if it has a set/get thingy. Or is it a mistake? public string Name
{ get { return name; } set { name = value; }}
public string name { get; set;}public string name { get; private set;}
|
What are the differences between these. I like the last ones for their compactness.
Felipe RamosPosted Sep 23, 2010, 10:26 AM
Moose PickardPosted Sep 23, 2010, 10:02 AM
Does that mean that setting the variable can only be done in the same class?
If I make the set/get like in the first example I can call it also from a higher object. Say for example there is an instance of a Name object in the Person object:
Name M = new Name
M.Name = "John"
How would I do it with just the last get/set notation example (public string name { get; private set;})?
Also I am curios about the variable notation You, Felipe, use. The m_ in front, what does it stand for?
Felipe RamosPosted Sep 23, 2010, 8:57 AM
Manikavelu VelayuthamPosted Sep 23, 2010, 8:34 AM
Private means it will be like a read only.
public means you can get and set values to any property