How can I access the backup variable for the auto-update property?

In the past, we have declared the following properties:

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 a private variable that is created automatically using this notation?

I prefer access to a private variable, rather than to the public accessory "Age". Is there a default notation for accessing a private variable, or is it just not possible?

+78
c # properties
Sep 14 '08 at 17:19
source share
6 answers

The aim of the new automatic properties is to reduce the amount of template code that you need to write when you simply have a simple property that does not need any special logic in get or set.

If you want to access a private member that uses these properties, which usually happens for several reasons:

  • You need more than just get / set - in this case, you should simply not use the automatic properties for this element.
  • You want to avoid a performance hit going through get or set, and just use the member directly - in this case I would be surprised if there really was a performance hit. The simple get / set elements are very easy to install, and in my (admittedly limited) test, I did not find the difference between using automatic properties and accessing the element directly.
  • You just want to have open access for reading (that is, just β€œget”), and write the class directly to the user - 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 of the reasons you want to go directly to the support field used by automatic properties.

+92
Sep 14 '08 at 17:47
source share

Your use of automatic properties means that you do not need the get / set logic for the property, so no private backup variable is needed.

Do not use automatic properties if you have complex logic in your class. Just go private int _age and normal getters / setters as usual.

IMO, automatic properties are more suitable for quick implementation of discarded objects or temporary data capsules, such as:

 public class TempMessage { public int FromID { get; set; } public int ToID { get; set; } public string Message { get; set; } } 

If you don’t need a lot of logic.

+23
Sep 14 '08 at 17:35
source share

This syntax is usually called syntactic sugar, which means that the compiler uses this syntax and translates it into something else. In your example, the compiler will generate code that looks something like this:

 [CompilerGenerated] private int <Age>k_BackingField; public int Age { [CompilerGenerated] get { return this.<Age>k_BackingField; } [CompilerGenerated] set { this.<Age>k_BackingField = value; } 

Even knowing all this, you can possibly access the base field directly, but this view defeats the goal of using automatic properties. I probably say here, because then you depend on the implementation details, which may change at any time in a future version of the C # compiler.

+12
Sep 14 '08 at 17:34
source share

Behind the scenes, what happens when you enter a private member variable, with the prefix <> k__AutomaticallyGeneratedPropertyField #

From Detailed Descriptions of C # 3.0 Properties

Although it may be possible to use this private member directly, it is very hacked and not needed.

+10
Sep 14 '08 at 17:27
source share

You should not do this, and this is very unlikely for you. If you need to access a property, just use a public property (like this.Age). There is nothing special about the fact that the private field supports public property, using it in the preference of property, it’s just superstition.

+7
Sep 14 '08 at 20:39
source share

You cannot, this is a language function, not an IDE function. Honestly, I would prefer the IDE to add a private variable for you. I agree that it’s rather strange for a class to use a public entry point to access its variables. Therefore, I do not use this new feature so much.

+2
Sep 14 '08 at 17:22
source share



All Articles