Declaring Getter and Setter in .NET.

I was wondering what are the differences between these getter and setter declarations and if there is a preferred method (and why). The first one can be generated automatically using Visual Studio. What about others? Thanks

the first

string _myProperty { get; set; } 

second

 string _myProperty; public string myProperty { get { return _myProperty; } set { _myProperty = value; } } 

third

 string _myProperty; public getMyProperty() { return this._myProperty; } public setMyProperty(string value) { this._myProperty = value; } 
+80
c # visual-studio-2010
Jul 26 '13 at 12:17
source share
8 answers

Properties are used to encapsulate some data. You can use a simple field:

 public string MyField 

But this field is available to all external users of your class. People can insert illegal values โ€‹โ€‹or change the value in ways you did not expect.

Using a property, you can encapsulate the way you access your data. C # has good syntax for turning a field into a property:

 string MyProperty { get; set; } 

This is called an automatically implemented property . When such a need arises, you can expand your property to:

 string _myProperty; public string MyProperty { get { return _myProperty; } set { _myProperty = value; } } 

Now you can add code that checks the value in your setter :

 set { if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException(); _myProperty = value; } 

Properties can also have different accessors for getter and setter:

 public string MyProperty { get; private set; } 

This way you create a property that can be read by everyone, but can only be changed by the class itself.

You can also add a fully custom implementation for getter :

 public string MyProperty { get { return DateTime.Now.Second.ToString(); } } 

When C # compiles your automatically implemented property, it generates an intermediate language (IL). In your IL, you will see the get_MyProperty and set_MyProperty . It also creates a support field called <MyProperty>k_BackingField (usually it would be an illegal name in C #, but it is valid in IL. This way you will not get any conflicts between the generated types and your own code). However, you must use the official property syntax in C #. This creates a more enjoyable C # experience (e.g. with IntelliSense).

By convention, you should not use properties for operations that take a lot of time.

+119
Jul 26 '13 at 12:30
source share

Well, the first and second generate something like the third at the end. However, do not use the third one if you have property syntax.

Finally, if you don't have work in get or set , use the first one.

After all, the first and second are just a form of syntactic sugar, but why the code is more than necessary.

 // more code == more bugs 

And just to have some fun, think about it:

 public string A { get; private set; } 

Now that is a lot more straightforward, isn't it? The public modifier is implied for both get and set , but can be overridden. Of course, this will be the same rule for any modifier used to determine the property itself.

+10
Jul 26 '13 at 12:19
source share

the first

 string _myProperty { get; set; } 

This is called an Auto Property in the .NET World. This is just syntactic sugar for No. 2.

second

 string _myProperty; public string myProperty { get { return _myProperty; } set { _myProperty = value; } } 

This is the usual way to do this, which is required if you need to do some verification or additional code in the property. For example, in WPF, if you need to fire an Event with a changed property. If you do not, just use the auto property, this is pretty standard.

3

 string _myProperty; public string getMyProperty() { return this._myProperty; } public string setMyProperty(string value) { this._myProperty = value; } 

The keyword this is redundant here. No need at all. These are just methods that get and set, unlike properties, such as the way Java is executed.

+7
Jul 26 '13 at 12:25
source share

To clarify, in your third example, _myProperty is not really a property. This is a field with the get and set methods (and, as already mentioned, the get and set methods must indicate the types of returned data).

In C #, in most situations, the 3rd method should be avoided. You would really use it only if the type you wanted to return was an array, or if the get method did a lot of work and not just return a value. The latter is not really necessary, but for the sake of clarity, the method of obtaining a property that does a great job is misleading.

+2
Jul 26 '13 at 13:14
source share

With this, you can execute some code in the get or set scope.

 private string _myProperty; public string myProperty { get { return _myProperty; } set { _myProperty = value; } } 

You can also use automatic properties:

 public string myProperty { get; set; } 

And the .NET Framework will handle you. It was created because it is a good appraisal and relief.

You can also control the visibility of these areas for the sample:

 public string myProperty { get; private set; } public string myProperty2 { get; protected set; } public string myProperty3 { get; } 

Update

Now in C # you can initialize the property value. For sample:

 public int Property { get; set; } = 1; 

If you can also define it and make it read-only, without typing.

 public int Property { get; } = 1; 

And finally, you can define the function of the arrow.

 public int Property { get; } => GetValue(); 
+1
Jul 26 '13 at 12:23
source share

1st by default when there is nothing special to return or write. 2nd and 3rd are basically the same where 3rd is a bit more advanced version of 2nd

0
Jul 26 '13 at 12:22
source share

Let's start with 3. This will not work. public getMyProperty() has no return.

And the numbers 1 and 2 are actually the same. 2 is what after compilation becomes number 1.

So 1 and 2 are the same. with two you can have some validation or caching in your model.

except that they become the same.

0
Jul 26 '13 at 12:23
source share

The first is the "short" form - you use it when you do not want to do something different with your getters and setters. It is not possible to execute a method or something similar in this form.

The second and third forms are almost identical, although the second is compressed to one line. This form is discouraged by the style, because it looks a little strange and does not match C 'Stylguides.

I would use the third form if I expected to use my getters / setters for something special, for example. use a lazy construct or so.

0
Jul 26 '13 at 12:24
source share



All Articles