What is the difference...? [C # properties of GET / SET ways]

I know that in C # you can easily create accessors for a data type, for example by doing the following:

public class DCCProbeData { public float _linearActual { get; set; } public float _rotaryActual { get; set; } } 

However, my colleague advised me to do it like this:

 public class DCCProbeData { private float _linearActual = 0f; public float LinearActual { get { return _linearActual; } set { _linearActual = value; } } private float _rotaryActual = 0f; public float RotaryActual { get { return _rotaryActual; } set { _rotaryActual = value; } } } 

My way seems simpler and more concise. What are the differences and benefits of this anyway?

thanks

Edit Only a note, my colleague was able to create code for the “second path” using the “Refactor” option in the “Class Details” panel, which was most easily found in the diagram file. This makes it easy to add many properties without having to manually create accessors.

+4
source share
13 answers

Your path simply tells the compiler to create a second option. Unless you do something else in a getter or setter, they are functionally identical.

However, using "your path", I would recommend using the correct C # naming conventions. I will personally write this as:

 public class DccProbeData { public float LinearActual { get; set; } public float RotaryActual { get; set; } } 
+24
source

The only difference is that you named the fields.

(I would stick to your peers naming convention for public properties.)

+3
source

They do the same inside. The only difference is that you cannot directly access the support field variable using the Autorealized properties.

+1
source

They are technically the same ... get / set is a shorthand ( auto property ).

Many questions about SO about this:

+1
source

Your path does not allow you to initialize values, and your colleague follows a more standard naming convention.

+1
source

I would like to add something that I have not seen in the other answers, which makes choice # 2 better:

Using the first method, you cannot set a breakpoint on get and set .

Using the second method, you can set a breakpoint on get and set , which is very useful for debugging anything that accesses your private variable.

+1
source

Well, the names have been mentioned before. It is also worth noting that, just as with other conventional .NET conventions, the beginning of an open name with an underscore is not CLS-compatible (indeed, one reason for using it for private names is precisely because of this, it makes the difference more clear and should result in a warning using some code controllers if you accidentally have the wrong access level).

Names aside, one advantage to the latter form is that you can add more complex code. However, this is an ongoing change to move from the previous style to the last, so there is no reason for this before it is needed.

+1
source

The first way is the way when you need simple properties with get and set and a private store for you.

Use the second method if you need to do something special when you get or set a value.

In addition, I recommend that you adhere to naming conventions with FxCop or ReSharper.

0
source

I believe that at the level of IL, they both turn out to be the same. In the background, VS creates auto-tagged variables for you when using auto-streams and setters.

0
source

The only way this could be better is if you feel that you will later add logic to getters and setters.

Even then it seems a little pointless.

0
source

There is no difference , however before C # 3 you had to use a long way. At the end of the day, this is the C # function - syntactic sugar. They are both functionally identical.

0
source

They are the same in the sense that your sample code automatically generates support fields.

But two code LinearActual are different from each other because the property names do not match ( LinearActual vs LinearActual )

0
source

What you can do if you don't use automatically implemented properties:

  • initialize default value
  • access or annotation of the support field (attributes)
  • Read-only fields supported or immutable
  • set breakpoint on access
  • has native code around variable access
  • Use [System.ComponentModel.EditorBrowsableAttribute()] to enable custom logic for accessories that you avoid accidentally bypassing during encoding
    • hides support field from intellisense

Converting between the two methods is greatly simplified with ReSharper.

This does not mean that they do not use them, using them if you do not need any of the other functions listed.

0
source

Source: https://habr.com/ru/post/1345121/


All Articles