Auto Implemented Properties - C#

Hi,

I have a class called Person. And it is like below. Can you please let me know in which internal  variable Name value will be stored ? I thought as like in VB.NET it is going to store in _Name variable. and i can access that variable in Print() method. But it did not in C# !!

public class Person
{
        public string Name { get; set; }

        public void Print()
        {
            Console.WriteLine(_Name);    // Error here
        }
}
milani_lucieAsked:
Who is Participating?
 
HohlovDimaCommented:
Hi,
In C# you can't access it b/c it is defended by compiler and have coded name. Look at screenshot. I use reflector to see what was generated
 c#in VB we have another situation look at screen:
 vb
0
 
Asim NazirCommented:
Hi,

User Name instead of _Name.
0
 
Asim NazirCommented:
Use Name instead of _Name.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
milani_lucieAuthor Commented:
But what is the internal variable name it is storing ?
0
 
Abhijit MaratheCommented:
Hi,

The compiler will automatically generate the backing field at compile time if it finds empty get or set blocks saving you the work.  You can still add get and set blocks that have additional filtering logic in them.

If you open it using ILDASM command line tool. You will get following line for your automatic property name Name.
.field private string '<Name>k__BackingField'

If you still have some query then please let me know.
0
 
Abhijit MaratheCommented:
Continue with my previous post

I think you cannot access it through code.
0
 
milani_lucieAuthor Commented:
I cannot do this:

public class Class1
{
        public string Name { get; set; }

        public void Print()
        {
            Console.WriteLine(<Name>k__BackingField);    // Error here
        }
}

I think so you are correct. As like in VB.NET, we cannot access internal variable. What do you think ?

Thanks
0
 
HohlovDimaCommented:
Yes we can't get access to it.
0
 
mrjoltcolaCommented:
You can access it via Reflection, but not sure why you want to.

Auto-generated backing fields will have the CompilerGeneratedAttribute (as shown by HohlovDima's picture).
http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.compilergeneratedattribute.aspx

You can detect this with Reflection as well, in FieldInfo.
0
 
Asim NazirCommented:
I cannot see why you want to access internal variable?
why don't you just set and get he property itself?
Is there any specific reason?
0
 
Todd GerbertIT ConsultantCommented:
You don't need to know the name of the backing variable, you can simply refer to the property by it's name, or by using the "this" keyword in C# or "Me" in VB (I prefer the "this.PropName" usage just because it makes what the code is doing a little more obvious).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
	static void Main(string[] args)
	{
		Class1 c1 = new Class1();
		c1.Name = "Bubba";
		c1.PrintMethodOne();
		c1.PrintMethodTwo();
		Console.ReadKey();
	}
}
public class Class1
{
	public string Name { get; set; }

	public void PrintMethodOne()
	{
		Console.WriteLine(this.Name);
	}

	public void PrintMethodTwo()
	{
		Console.WriteLine(Name);
	}
}

Open in new window

0
 
ulfurCommented:
There are two versions you can use:
public class Person
{
	// this is the instance variable that will store the name
	private string _Name;
	// define the accessors if you need extra functionality, e.g. fire an event or update a control
	public string Name
	{
		get { return _Name; }
		set { _Name = value; }
	}
	public void Print()
	{
		// print the name to the console; the following two statements yield the same result
		Console.WriteLine(_Name); // direct access to the value
		Console.WriteLine(Name); // retrieve the value by calling the get accessor
	}
	}

Open in new window


The simple version is this:

public class Person
{
	// Name becomes the instance variable that stores the name
	public string Name;

	public void Print()
	{
		// print the name to the console
		Console.WriteLine(Name);
	}
	}

Open in new window


Hope this helps
0
 
Asim NazirCommented:
Ok. So if you really want to use variables, leave auto implemented properties and go for standars properties.
0
 
ulfurCommented:
That's right. You can find the details here Auto-Implemented Properties (C# Programming Guide)
"When you declare a property ... the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors."
0
 
Todd GerbertIT ConsultantCommented:
Probalby just a typo on ulfur's part, but since you accepted http:#a35451260, it's probably worth pointing out that the second code snippet in that comment doesn't have a property, it has a member variable.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.