您是否应该通过Property访问同一类中的变量?

时间:2022-06-28 23:44:29

If you have a Property that gets and sets to an instance variable then normally you always use the Property from outside that class to access it.

如果您有一个获取并设置为实例变量的Property,那么通常您总是使用该类外部的Property来访问它。

My question is should you also always do so within the class? I've always used the Property if there is one, even within the class, but would like to hear some arguments for and against as to which is the most correct and why.

我的问题是你是否也应该在课堂上这样做?我总是使用该属性,如果有一个,即使在课堂上,但是想听到一些赞成和反对的论据,哪个是最正确的,为什么。

Or is it just a matter of coding standards being used on the project?

或者只是在项目中使用的编码标准问题?

6 个解决方案

#1


22  

One of the stronger argument for accessing local (class scope) variables through properties is that you add a level of abstraction in your class. If you change any logic concerning how that field is stored then the rest of your code will be left unaffected.

通过属性访问本地(类范围)变量的一个更强大的参数是在类中添加抽象级别。如果您更改了有关该字段存储方式的任何逻辑,那么其余代码将不受影响。

For example you might change that from a local variable to a property of a child object, to a database call, to a webservice call, to a static property on a class and so on. When making the change it gives you a single point of change, the property, and you do not have to update the rest of your class since they all use the property.

例如,您可以将其从局部变量更改为子对象的属性,更改为数据库调用,更改为Web服务调用,更改为类上的静态属性等。在进行更改时,它会为您提供单点更改,属性,并且您不必更新类的其余部分,因为它们都使用该属性。

Also using the property enables you to apply business rules on the value of the property instead of having to enforce the same rule at each location where you'd directly access the field. Again, encapsulation

此外,使用该属性还可以对属性值应用业务规则,而不必在您直接访问该字段的每个位置强制执行相同的规则。再次,封装

With the introduction of automatic properties there's even less reason to explicitly have a local variable, unless you need to apply business rules on the get/set

随着自动属性的引入,显式拥有局部变量的理由更少,除非您需要在get / set上应用业务规则

#2


9  

It depends on whether you want to apply any logic implemented within the property setter, and so you really have to decide on a case by case basis.

这取决于您是否要应用属性设置器中实现的任何逻辑,因此您必须根据具体情况决定。

When you go directly to the private field, you know that the field is being set to exactly what you say.

当您直接进入私人领域时,您知道该字段正在设置为您所说的内容。

When you go through the Property, the value gets set according to the setter logic, so you get any business rules or validation you want over values assigned to that field.

当您浏览属性时,将根据setter逻辑设置该值,因此您可以获得所需的任何业务规则或验证,而不是分配给该字段的值。

Pretty hard to come up with a rule about when doing either is 'correct', about the only one I'd say I follow is that in constructor initialisation I'd pretty much never use the Property.

很难想出一个关于什么时候做“正确”的规则,关于我所说的唯一一个规则是在构造函数初始化中我几乎从不使用该属性。

#3


0  

I think it's purely preference.

我认为这纯粹是偏好。

Though, I find myself using the properties a lot more in C# 3.0 with the auto-property support:

虽然,我发现自己在C#3.0中使用自动属性支持更多的属性:

class Foo {
    public string Value { get; set; }

    public void Write() {
        Console.Write(Value);
    }
}

#4


0  

Generally depending on the project coding standards I use a "_" or "m" preceding the name for my private class attributes. (Like below)

通常,根据项目编码标准,我在私有类属性的名称前面使用“_”或“m”。 (如下)

private int mVariable;
private int _Variable;

With those in front of the variable I recognize right away that I'm dealing with an internal variable for the class. Then when it comes to debugging later myself or someone else can immediately recognize that the code is dealing with an internal private variable and make an adjustment. So it comes down to readability for me.

对于变量前面的那些我立即认识到我正在处理类的内部变量。然后,当涉及到我自己或其他人的调试时,可以立即认识到代码正在处理内部私有变量并进行调整。所以它归结为对我的可读性。

#5


0  

Yes I think you should use properties internally in your classes whenever possible. Properties are more flexible and allows you to add logic for validating it's value at a central place.

是的我认为你应该尽可能在课堂内部使用属性。属性更灵活,允许您添加逻辑以在中心位置验证其值。

You can also delay the initialization of the the field to whenever the property is used instead of being forced to do it in the constructor (or everywhere the field is used). Example:

您还可以将字段的初始化延迟到使用属性时,而不是在构造函数中强制执行它(或者在使用字段的任何位置)。例:

class Test {
   private int _checksum = -1;
   private int Checksum {
      get {
         if (_checksum == -1)
            _checksum = calculateChecksum();
         return checksum;
      }
   }
}

#6


0  

Always Use Properties, Here are some of the reasons

始终使用属性,以下是一些原因

  1. Easy to Use. In visual Studio you can use "Prop Tab Tab". You will get the property snippet
  2. 使用方便。在Visual Studio中,您可以使用“Prop Tab Tab”。您将获得属性片段

  3. Properties are language elements that are accessed as though they are data members
  4. 属性是被访问的语言元素,就像它们是数据成员一样

  5. .Net framework classes uses it, the data binding code classes in the .NET Framework support properties,
  6. .Net框架类使用它,.NET Framework支持属性中的数据绑定代码类,

  7. Properties have all the language features of methods. Properties can be virtual
  8. 属性具有方法的所有语言功能。属性可以是虚拟的

#1


22  

One of the stronger argument for accessing local (class scope) variables through properties is that you add a level of abstraction in your class. If you change any logic concerning how that field is stored then the rest of your code will be left unaffected.

通过属性访问本地(类范围)变量的一个更强大的参数是在类中添加抽象级别。如果您更改了有关该字段存储方式的任何逻辑,那么其余代码将不受影响。

For example you might change that from a local variable to a property of a child object, to a database call, to a webservice call, to a static property on a class and so on. When making the change it gives you a single point of change, the property, and you do not have to update the rest of your class since they all use the property.

例如,您可以将其从局部变量更改为子对象的属性,更改为数据库调用,更改为Web服务调用,更改为类上的静态属性等。在进行更改时,它会为您提供单点更改,属性,并且您不必更新类的其余部分,因为它们都使用该属性。

Also using the property enables you to apply business rules on the value of the property instead of having to enforce the same rule at each location where you'd directly access the field. Again, encapsulation

此外,使用该属性还可以对属性值应用业务规则,而不必在您直接访问该字段的每个位置强制执行相同的规则。再次,封装

With the introduction of automatic properties there's even less reason to explicitly have a local variable, unless you need to apply business rules on the get/set

随着自动属性的引入,显式拥有局部变量的理由更少,除非您需要在get / set上应用业务规则

#2


9  

It depends on whether you want to apply any logic implemented within the property setter, and so you really have to decide on a case by case basis.

这取决于您是否要应用属性设置器中实现的任何逻辑,因此您必须根据具体情况决定。

When you go directly to the private field, you know that the field is being set to exactly what you say.

当您直接进入私人领域时,您知道该字段正在设置为您所说的内容。

When you go through the Property, the value gets set according to the setter logic, so you get any business rules or validation you want over values assigned to that field.

当您浏览属性时,将根据setter逻辑设置该值,因此您可以获得所需的任何业务规则或验证,而不是分配给该字段的值。

Pretty hard to come up with a rule about when doing either is 'correct', about the only one I'd say I follow is that in constructor initialisation I'd pretty much never use the Property.

很难想出一个关于什么时候做“正确”的规则,关于我所说的唯一一个规则是在构造函数初始化中我几乎从不使用该属性。

#3


0  

I think it's purely preference.

我认为这纯粹是偏好。

Though, I find myself using the properties a lot more in C# 3.0 with the auto-property support:

虽然,我发现自己在C#3.0中使用自动属性支持更多的属性:

class Foo {
    public string Value { get; set; }

    public void Write() {
        Console.Write(Value);
    }
}

#4


0  

Generally depending on the project coding standards I use a "_" or "m" preceding the name for my private class attributes. (Like below)

通常,根据项目编码标准,我在私有类属性的名称前面使用“_”或“m”。 (如下)

private int mVariable;
private int _Variable;

With those in front of the variable I recognize right away that I'm dealing with an internal variable for the class. Then when it comes to debugging later myself or someone else can immediately recognize that the code is dealing with an internal private variable and make an adjustment. So it comes down to readability for me.

对于变量前面的那些我立即认识到我正在处理类的内部变量。然后,当涉及到我自己或其他人的调试时,可以立即认识到代码正在处理内部私有变量并进行调整。所以它归结为对我的可读性。

#5


0  

Yes I think you should use properties internally in your classes whenever possible. Properties are more flexible and allows you to add logic for validating it's value at a central place.

是的我认为你应该尽可能在课堂内部使用属性。属性更灵活,允许您添加逻辑以在中心位置验证其值。

You can also delay the initialization of the the field to whenever the property is used instead of being forced to do it in the constructor (or everywhere the field is used). Example:

您还可以将字段的初始化延迟到使用属性时,而不是在构造函数中强制执行它(或者在使用字段的任何位置)。例:

class Test {
   private int _checksum = -1;
   private int Checksum {
      get {
         if (_checksum == -1)
            _checksum = calculateChecksum();
         return checksum;
      }
   }
}

#6


0  

Always Use Properties, Here are some of the reasons

始终使用属性,以下是一些原因

  1. Easy to Use. In visual Studio you can use "Prop Tab Tab". You will get the property snippet
  2. 使用方便。在Visual Studio中,您可以使用“Prop Tab Tab”。您将获得属性片段

  3. Properties are language elements that are accessed as though they are data members
  4. 属性是被访问的语言元素,就像它们是数据成员一样

  5. .Net framework classes uses it, the data binding code classes in the .NET Framework support properties,
  6. .Net框架类使用它,.NET Framework支持属性中的数据绑定代码类,

  7. Properties have all the language features of methods. Properties can be virtual
  8. 属性具有方法的所有语言功能。属性可以是虚拟的