0

Does auto implemented properties have the same backed field in base and derived classes in C# ? I have the following code:

class Employee
    {
        public virtual string Infos { get; set; }
    }

    class Engineer : Employee
    {
        public override string Infos
        {
            get
            {
                //Employee and Engineer share the same backed field.
                return base.Infos + " *";
            }
        }
    }

And in the Main class, i have the following code :

class Program
    {
        static void Main(string[] args)
        {
            Employee employee = new Engineer { Infos = "Name : Boulkriat Brahim" };
            Console.WriteLine ( employee.Infos );

        }
    }

Compiling and Running this code will print "Boulkriat Brahim *". So base.Info is equal to "Boulkriat Brahim".This mean that this.Info and Base.Info have the same value despite i create an object of type Engineer. Does this mean that they had the same backed field?

svick
  • 236,525
  • 50
  • 385
  • 514
Brahim Boulkriat
  • 984
  • 2
  • 9
  • 21
  • What do you meant by "properties have the same backed field in base and derived classes in C#"? Post some code of what you're talking about – Sriram Sakthivel Nov 01 '13 at 06:36
  • I updated my post with some code :) – Brahim Boulkriat Nov 01 '13 at 07:00
  • 1
    Well, Of course yes. Note you're not using backing field directly you're using the base class property only which uses the backing field. – Sriram Sakthivel Nov 01 '13 at 07:12
  • So is backed field shared between base an derived classes properties? Did they access the same backed field? – Brahim Boulkriat Nov 01 '13 at 07:16
  • When you call `base.Infos` you're not using the backing field directly. you're calling the base class property only. but.. base class property accesses the backing field and gets the value back. There is no sharing happens here. It should be understood like you're just calling base class method to get the value(under the hood properties are methods) – Sriram Sakthivel Nov 01 '13 at 07:35

2 Answers2

3

Yes, exactly as if you declared the properties manually. There's only one field, and all subclasses inherit it.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • Are backed fields private? If so, how does derived classes access them if there is only one shared backed field between classes? – Brahim Boulkriat Nov 01 '13 at 06:33
  • Yes, they are private (and compiler-generated to boot!), and without reflection, subclasses can't access them directly. You can, however, obviously access them via the get/set methods, within the base class or any derived classes. – Kirk Woll Nov 01 '13 at 06:34
1

In your code, there is only one backing field, because there is one auto-implemented property: Employee.Infos. Engineer.Infos is a normal property, so it doesn't have any backing field.

If you instead wrote your code like this:

class Employee
{
    public virtual string Infos { get; set; }
}

class Engineer : Employee
{
    public override string Infos { get; set; }

    public void M()
    {
        this.Infos = "Name : Boulkriat Brahim";
        Console.WriteLine(base.Infos);
    }
}

Then calling new Enginner().M() would pass null to WriteLine(), because Engineer.Infos has a different backing field than Employee.Infos.

svick
  • 236,525
  • 50
  • 385
  • 514