3

what is the different between writing the getter and setter directly like this:

public string Name {get; set;}

and like this:

private string _name;

public string Name
{
    get
    {
       return this._name;
    }
    set
    {
        this._name = value;
    }
}

I saw that in lots of codes. why they use a private member than a public getter and setter.

is it for performance or privacy or what is the point? thank you.

Samy Sammour
  • 2,298
  • 2
  • 31
  • 66
  • 2
    They're basically the same thing. The first code only became an option in C# 3. – Jon Skeet Apr 04 '17 at 12:36
  • Once upon a time, [auto-implemented properties](https://msdn.microsoft.com/en-us/library/bb384054.aspx) didn't even exist. – spender Apr 04 '17 at 12:36
  • The first is just a convince that actually compiles into the second. It was added later in the C# life, so it could be force of habit or just old code. The main difference is that with the second you can directly manipulate the private field from inside the class. – juharr Apr 04 '17 at 12:36
  • There's none. The first is syntactic sugar for the second. See [here](https://tryroslyn.azurewebsites.net/#b:master/f:r/K4Zwlgdg5gBAygTxAFwKYFsDcBYAUAB2ACMAbMAYxnJIEMQQYBhGAbzxg5kNIphQCdIsAHI10qVlFTJMfaZgC+eBUA==) – Ofir Winegarten Apr 04 '17 at 12:38
  • The second way is used when you want something else to happen when you set or get. Sometimes people use the long way, because they want the class to access the field directly instead of using its own interface. That way you can add stuff to the setter or getter without breaking the class implementation. One of the great things about C# is that you can write it the short way and change it later without breaking any clients. – Matt Timmermans Apr 04 '17 at 12:42
  • @MattTimmermans Cool! thank you. I guess it is for the purpose of future change. – Samy Sammour Apr 04 '17 at 12:44

6 Answers6

3

what is the different between writing the getter and setter directly like this

public string Name {get; set;}

They're essentially the same.

The code below you're basically creating the private field and providing both getters and setters for it, which of course does the intended idea. However, the language implementors decided they could make the life of a programmer easier by providing a shorter syntax where you can create a private field and at the same time provide a getter or setter or both.

private string _name;

public string Name
{
    get
    {
       return this._name;
    }
    set
    {
        this._name = value;
    }
}

So, in C# 3 they(language implementors) came up with the idea of making the syntax shorter by enabling a programmer to simultaneously create a private field and at the same time provide a getter or setter or both.

Behind the scenes, all that happens for the code below is the compiler creates a private field and also provides a getter and setter for it. So, basically, it's shorter more concise syntax to achieve the same task as the example above.

auto-implemented property

public string Name {get; set;}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
2

There is none.

The thing is: auto-implemented properties weren't available until C# 3 (if you look at the documentation referenced: it goes back to VS 2008 which was released with C# 3), and not all code was written in the C# 3 era. Also, not all developers are aware of this feature. If I would stumble across this kind of code, I would rewrite it to use auto-implemented properties.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

An property is just a short hand and will create at the background an public get method and a public set method and a private field to store the value.

Example Code

 // example property
 public string Name { get; set; }

// at run time it is the same as:
private string Name;

public string GetName(){
  return this.Name;
}

public string SetName(string name){
  this.Name = name;
}

See Image :

The sample class only has an property in code Name. If you use Reflection to get all the members off the Sample class you will see that at run time set_name() and get_name() methods are generated. These methods are not specified in in code.

enter image description here

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Timon Post
  • 2,779
  • 1
  • 17
  • 32
1

Short answer, there isn't a difference. The compiler will convert the "auto" property to that style regardless, it's just saving you the writer a few keystrokes. It really only comes into play when you start working with DataBinding or having to do something else in the Set portion.

private string _name;

public string Name
{
    get
    {
       return this._name;
    }
    set
    {
        this._name = value;OnPropertyChange();
    }
}

In WPF/XAML/DataBinding, this would let anyone subscribed to this object know that a property with the name "Name" has changed and it should reflect so in the UI.

Kevin B Burns
  • 1,032
  • 9
  • 24
0

The first one is called an auto-implemented property. Second one is used when you want to add some custom code logic that validates the value in your setter.

-1

You can control what happens in the getter & setters, whereas if the member was public, the variable could be modified directly.

private string _name;

public string Name
{
    get
    {
       return this._name + " likes chocolate";
    }
    set
    {
        this._name = value;
    }
}

Here, your private _name always stays the same, but anyone accessing Name will get _name + " likes chocolate".

In the setter, you could do some validation.

pookie
  • 3,796
  • 6
  • 49
  • 105
  • 2
    In both cases shown, there's a private field and a public property. – Jon Skeet Apr 04 '17 at 12:37
  • I can understand this code when I have to write a certain getter and setter but why when I use a normal code like I did. I work in a big company and all the code are written like this – Samy Sammour Apr 04 '17 at 12:39
  • My apologies. I did not see the line `public string Name {get; set;}`. I was comparing: `private string _name;` and `public string Name`. The others are correct, they are the same... it is just easier to write 1 line :) – pookie Apr 04 '17 at 12:43
  • No one expect that a property will change the input value. The is code smell – Franck Apr 04 '17 at 12:43
  • @SamySammour, just out of curiosity are you new to programming or just C#? – Kevin B Burns Apr 04 '17 at 13:26
  • I am not new, I have 5 Certificate from Microsoft with 6 years of experience in .NET. but this question was in my brain always and I forgot to ask it. After you go in the direction of professional you ask always small questions really in the details. like should I write {get;set;} or {set;get;} It is always cool to share other's thoughts, so I can learn! @KevinBBurns :) – Samy Sammour Apr 04 '17 at 14:35
  • @SamySammour, I was just curious, when I made the switch from C++ professionally to C#, the get and set thing was just odd. That's why I was curious. – Kevin B Burns Apr 05 '17 at 15:36
  • No problem, I am curious too. with curiosity, we can good learning – Samy Sammour Apr 06 '17 at 13:06