We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Auto Implemented Properties - C#

milani_lucie
milani_lucie asked
on
Medium Priority
524 Views
Last Modified: 2013-12-16
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
        }
}
Comment
Watch Question

Asim NazirProject Manager

Commented:
Hi,

User Name instead of _Name.
Asim NazirProject Manager

Commented:
Use Name instead of _Name.

Author

Commented:
But what is the internal variable name it is storing ?
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Continue with my previous post

I think you cannot access it through code.
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Top Expert 2009

Commented:
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.
Asim NazirProject Manager

Commented:
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?
Todd GerbertSenior Engineer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Asim NazirProject Manager

Commented:
Ok. So if you really want to use variables, leave auto implemented properties and go for standars properties.
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Todd GerbertSenior Engineer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.