C# 3.0 :Automatic Properties - what would be the name of private variable created by compiler
Asked Answered
F

4

11

I was checking the new features of .NET 3.5 and found that in C# 3.0, we can use

public class Person 
{    
 public string FirstName  { get; set; }
 public string LastName  { get; set; }
}

instead of

private string name;

public string Name
{
  get { return name; }
  set { name = value; }
}

If i use the Automatic Properties,what will be the private variable name for Name ? the tutorials on internet says that compiler will automatically create a private variable.So how can i use /access the private variable,if i want to use it in a method in this class ?

Fayalite answered 14/8, 2009 at 9:57 Comment(2)
Note that this is a feature of C# 3.0, not .NET 3.5 - you can use it even if you're targeting .NET 2.0 from a C# 3.0 compiler.Catalan
possible duplicate of Is the implementation of Auto Properties in the spec?Narcose
P
21

Rewritten to be more clear

The field is generated alright, but it is not visible to your code as a regular field.

Here's your typical automatic property:

public string FirstName  { get; set; }

which, if we look at the compiled assembly, produces this backing store field:

[CompilerGenerated]
private string <FirstName>k__BackingField;

Note the < and > in there, which are not characters you can use in your own field names. Nor can you access that field, because it doesn't "exist" as far as the compiler cares, when it looks at your code.

The real question here, from me, is why you would want to access that field. In other words, why do you need access to the field, and what does it do for your code that accessing the property don't do?

If you want to prevent outside write-access to the field, that's easily doable by making the setter method private, like this:

public string FirstName  { get; private set; }

Note that since the field is actually present in the assembly, it means that this is not JITter magic, but compiler magic, and thus you could use reflection to find, and access, that field.

But again, why would you want to?

Now, let's assume that there really is a legitimate reason for you wanting to use the field, instead of the property. I can think of one, though I would probably do it differently, and that would be that you want to pass the field name to a method as an out or ref parameter, like this:

public void AdjustName(ref String name)
{
    name = Capitalize(name);
}

You can't pass properties as out/ref-parameters, so this code won't work:

AdjustName(ref string FirstName);

In this case, you need to fall back to the "old" way of defining properties, adding the backing store field manually, like this:

private string firstName;
public string FirstName
{
    get { return firstName; }
    set { firstName = value; }
}

With this in place, you can call that method:

AdjustName(ref string firstName); // note the field, not the property

However, I would probably change that method in order to return the new value, instead of directly adjusting a referenced variable.

Pejsach answered 14/8, 2009 at 10:0 Comment(4)
I Want to use the value of the property in a method which do some mathematical calculationsFayalite
So why not just use the property itself?Pejsach
The only thing get does is return that specific value ;).Iambus
Sorry, forgot to say I commented on Shyju.Iambus
H
5

As already said: You cannot access the automatically generated variable (without using bad tricks). But I assume you are asking that question because you want to have only a getter, but still want to use automatic properties ... right? In that case you can use this one:

public string FirstName  { get; private set; }

Now you have a private setter and a public getter.

Humoresque answered 14/8, 2009 at 10:9 Comment(0)
V
1

If you use automatic properties, then you cannot access the private field. It is not only private, it is also anonymous.

Vermouth answered 14/8, 2009 at 9:59 Comment(1)
Right. If you for whatever reason think you need access to the private member (other then wanting to know what it is called "out of curiosity") then don't implement the property like this, but define your own private member. Like "in the old days". ;-)Josefina
I
0

So how can i use /access the private variable,if i want to use it in a method in this class ?

In your source code you only need to, and are only allowed to work with the Property Accessor person.LastName or LastName if you are writing code inside the person class.

The compiler will automatically create a private member variable, or backing field, but this exists in the CIL code. It does not exist anywhere in your source code.

Indisposed answered 14/8, 2009 at 10:12 Comment(2)
@Shyju: I think you might be making the wrong assumption that Visual Studio will auto-generate a private field for you to use. The auto-generation is done by the compiler, and exists only in the CIL code.Indisposed
If you really REALLY must get hold of the private field, then you can use reflection. But I'm pretty sure that this is not what you want,Indisposed

© 2022 - 2024 — McMap. All rights reserved.