简体   繁体   中英

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:

The name _currentTemp does not exist in the current context.

Using { get; set; } { get; set; } { get; set; } should automatically create this private variable ( _currentTemp ), right?

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...

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:

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; you may want to read into implementing INotifyPropertyChanged . Here's a link: https://msdn.microsoft.com/en-us/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. If you are not doing anything with the property then just use the default get/set like this:

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. This will only allow the property to be publically read.

public double CurrentTemp { get; private set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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