Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the backing variable of an auto-implemented property?

Tags:

c#

properties

In the past we declared properties like this:

public class MyClass {     private int _age;      public int Age     {           get{ return _age;  }           set{ _age = value; }     } } 

Now we can do:

public class MyClass {     public int Age {get; set;}  } 

My question is, how can I access the private variable that is created automatically using this notation?

I would rather access the private variable and not the public accessor 'Age'. Is there a default notation to access the private variable, or it is just not possible?

like image 815
public static Avatar asked Sep 14 '08 17:09

public static


People also ask

What is an auto implemented property and what does it automatically create?

Auto-implemented properties declare a private instance backing field, and interfaces may not declare instance fields. Declaring a property in an interface without defining a body declares a property with accessors that must be implemented by each type that implements that interface.

What is the difference between a property and an auto implemented property?

Read-Only Properties: When property contains only get method. Write Only Properties: When property contains only set method. Auto Implemented Properties: When there is no additional logic in the property accessors and it introduce in C# 3.0.

What is an auto implemented property?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

What is backing field in C#?

A private field that stores the data exposed by a public property is called a backing store or backing field. Fields typically store the data that must be accessible to more than one type method and must be stored for longer than the lifetime of any single method.


1 Answers

The aim of the new automatic properties is to reduce the amount of boilerplate code you need to write when you just have a simple property that doesn't need any special logic in the get or the set.

If you want to access the private member that these properties use, that's usually for a few reasons:

  • You need to more than just a simple get/set - in this case, you should just avoid using automatic properties for this member.
  • You want to avoid the performance hit of going through the get or set and just use the member directly - in this case, I'd be surprised if there really was a performance hit. The simple get/set members are very very easy to inline, and in my (admittedly limited) testing I haven't found a difference between using the automatic properties and accessing the member directly.
  • You only want to have public read access (i.e. just a 'get') and the class write to the member directly - in this case, you can use a private set in your automatic property. i.e.

    public class MyClass {     public int Age {get; private set;}  }

This usually covers most the reasons for wanting to directly get to the backing field used by the automatic properties.

like image 70
Wilka Avatar answered Nov 29 '22 15:11

Wilka