views:

672

answers:

4

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 ?

+1  A: 

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

John Saunders
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". ;-)
peSHIr
+10  A: 

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

Lasse V. Karlsen
I Want to use the value of the property in a method which do some mathematical calculations
Shyju
So why not just use the property itself?
Lasse V. Karlsen
The only thing get does is return that specific value ;).
Dykam
Yes, and then what?
Lasse V. Karlsen
Sorry, forgot to say I commented on Shyju.
Dykam
+4  A: 

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.

Achim
A: 

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.

Saajid Ismail
@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.
Saajid Ismail
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,
Saajid Ismail