Should I refer to a Member property or variable inside the class?

Possible duplicate:
Should a variable be accessed within the same class using a property?

I came across this lately and I was curious if there is any standard for which you need to reference within the class.

I mean, it really shouldn't matter if you access the member variable directly or go through the property (unless you need to shy away from some kind of custom setting code), but I wanted to be sure that there wasn’t best practice for him.

partial class MyClass { private string foo; internal string Foo { get { return foo; } private set { foo=value; // I do other stuff } } public void DoSomething() { //Option 1; Foo="some string"; //Option 2; foo="some string"; } } 
+4
source share
8 answers

This should not be the choice you really make. It is assumed that either the code in the setter should work, or in this case use the property, or not, in which case you are using a member variable. In most situations, everything is correct, but one is wrong. In the general case, none of them is right / wrong, and it is unusually "unimportant" for him.

For example, if the installer code fires a “modified” event, do you want external objects to be notified of the change or not? If you change it in response to a previous change, maybe not (infinite recursion?), If not, you probably want to make sure it is fired (so you don't change the value and notify about the changes).

If he simply checks the correctness of the set value, then you know that in this context the value is already checked and must be valid, and in this case there is no need to check again; set the property. If you have not confirmed what you are going to install, then you want the verification logic to run, so use the property.

+3
source

This question is discussed quite a lot, so there is no obvious answer to the question.

Personally, I prefer access through a property because you may have some validation or conversion code. Even if your getters and setters are trivial, they may change in the future.

+2
source

If you wrapped the foo field in the foo property, you probably did it for some reason (conversion, events, validation, etc.). So, generally speaking, the only place you should reference the foo field is in getters and setters for the foo property. The rest of the code should reference the foo property.

I am sure that there is some kind of unclear situation where you need to get around the getters and seters properties, and this is certainly good, but such situations would be an exception to the rule.

+1
source

Option 1 is good practice. because if you use Option 2 , you will lose other things when setting the foo value.

0
source

I would go with Option 1. If you are setting a variable, you should use this property, and not directly access the variable. This is because the property has additional code that you specified with "// I am doing other things." You would not want to repeat this “other stuff” just because you did not set the property ... unless you want to do “other things” when you set it up this time.

Honestly, this is just a theoretical situation, and it would be much easier to answer if you would give a practical situation when you are faced with this problem.

0
source

When using the INotifyPropertyChanged interface, using a property is required if you want to update related objects.

0
source

If the installer does not have logic, it makes no sense to explicitly declare a private variable, and it is better to use automatically implemented properties:

  internal string Foo { get; private set; } public void DoSomething() { this.Foo = "some string"; } 

If the installer has logic, the private variable should only be used in the installer and should never be changed outside of the installation. In any case (and, in my opinion :)), a private variable should never appear anywhere else near the property installer.

0
source

Imagine a code like

 public partial class HybridPanel: Panel { [DefaultValue(BorderStyle.FixedSingle)] public virtual new BorderStyle BorderStyle { set { if(value!=borderStyle) { borderStyle=value; base.PerformLayout(); } } get { try { return borderStyle; } finally { if(borderStyle!=base.BorderStyle) base.PerformLayout(); } } } BorderStyle borderStyle=BorderStyle.FixedSingle; bool isCollapsed, isAutoSize; } 

In this connection, the property is used not only as a variable, but also other things. Access to properties in one class is NOT considered a bad assessment, in addition, the compiler would assume that:

For a method that is designed to access a field without passing arguments, consider the definition as a property.

By the way, you can correct the description of access to the directory of member variables to get access to the member variable directly (i.e. to access the fields).

0
source

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


All Articles