4
Answers

Different Get/Set methods, which to use?

Moose Pickard

Moose Pickard

14y
1.8k
1
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.

Answers (4)