简体   繁体   English

访问自动实现属性的私有字段

[英]Accessing the private field of an auto-implemented property

Below, when I attempt to use the _currentTemp variable, that was supposed to be auto-generated via the auto properties functionality, I get a variable not found message: 在下面,当我尝试使用_currentTemp变量(该变量应该通过自动属性功能自动生成)时,出现一条未找到变量消息:

The name _currentTemp does not exist in the current context. 名称_currentTemp在当前上下文中不存在。

Using { get; set; } 使用{ get; set; } { get; set; } { get; set; } should automatically create this private variable ( _currentTemp ), right? { get; set; }应该自动创建此私有变量( _currentTemp ),对吗?

public class DogTemperature
{
    public double CurrentTemp { get; set; }

    public DogTemperature(double dCurrentTemp)
    {
        _currentTemp = dCurrentTemp;  //***this line***
    }
}

Backing fields created by auto-properties are not available for you to interact with in your source code, as it is generated by the compiler. 由自动属性创建的后备字段在源代码中不可与您交互,因为它是由编译器生成的。

If you want to interact with the backing field, you'll need to create your properties the verbose way. 如果要与支持字段进行交互,则需要以冗长的方式创建属性。

Based on @Alex Gravely's answer... 基于@Alex Gravely的答案...

If I'm understanding your necessity for a full property: you can create full properties and backing fields like this: 如果我了解完整属性的必要性,则可以创建完整属性和支持字段,如下所示:

private double _currentTemp;

public double CurrentTemp
{
     get { return _currentTemp; }
     set { _currentTemp = value; }
}

Then in your constructor for DogTemperature , you just need to set the CurrentTemp to the double you passed in: 然后在DogTemperature的构造函数中,只需将CurrentTemp设置为传入的double即可:

public void DogTemperature(double temp)
{
    DogTemperature = temp;
}

Depending on what usage you want to get out of the CurrentTemp property - ie to display in a View and updating it; 取决于CurrentTemp属性中使用的用法-即在View中显示并更新它; you may want to read into implementing INotifyPropertyChanged . 您可能需要阅读实现INotifyPropertyChanged Here's a link: https://msdn.microsoft.com/en-us/library/ms229614(v=vs.100).aspx 这是链接: https : //msdn.microsoft.com/zh-cn/library/ms229614(v=vs.100).aspx

If it is just a plain old property, and not used for anything special (such as in a model, for example); 如果它只是一个普通的旧属性,并且没有用于任何特殊的东西(例如在模型中); then the 然后

public double DogTemperature { get; set; }

property will suffice; 财产就足够了; setting it in the constructor as above. 在上面的构造函数中进行设置。

Hope this helps! 希望这可以帮助!

In my opinion defining a property like this is complely pointless if all you want to do is store a value. 在我看来,如果您只想存储一个值,则定义这样的属性是毫无意义的。

double _currentTemp;
public double CurrentTemp
{
   get { return _currentTemp; }
   set { _currentTemp = value; }
}

All you're doing here is giving the private context two ways to set the same value. 您在这里所做的就是为私有上下文提供两种设置相同值的方法。 You can set the _currentTemp field directly or you can set the CurrentTemp property which sets the _currentTemp field. 您可以直接设置_currentTemp字段,也可以设置CurrentTemp属性来设置_currentTemp字段。 If you are not doing anything with the property then just use the default get/set like this: 如果您不对该属性做任何事情,则只需使用默认的get / set,如下所示:

public double CurrentTemp { get; set; }

If you need to do more complex work in the property then go ahead and define a field like this. 如果您需要在媒体资源中执行更复杂的工作,请继续定义这样的字段。 More complex work such as conditions, calculations or raising events: 更复杂的工作,例如条件,计算或引发事件:

double _currentTempFarenheit;
double _currentTempCelcius;

public double CurrentTemp
{
   get 
   { 
       if(UseFarenheit)
           return _currentTempFarenheit;
       else
           return _currentTempCelcius;
   }
   set 
   { 
       if(UseFarenheit)
           _currentTempFarenheit = value;
       else
            currentTempCelcius = value;
   }
}

Furthermore if you only want the value of your property to be set by the constructor of your DogTemperature class then you should make the setter private. 此外,如果仅希望由DogTemperature类的构造函数设置属性的值,则应将设置器设为私有。 This will only allow the property to be publically read. 这将仅允许该属性公开阅读。

public double CurrentTemp { get; private set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM