Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an abstract property create a private backing field?

Simple question: does an abstract property create a private backing field? Example:

public abstract Name { get; set; }

Will this create a private backing field? I want to force any class that derives this property to use their own backing field, not one that's created by the compiler.

like image 679
Daniel T. Avatar asked Dec 09 '09 21:12

Daniel T.


People also ask

Do abstract classes have properties?

An abstract class not only contains abstract methods and assessors but also contains non-abstract methods, properties, and indexers. It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings.

Can abstract class have private variables in C#?

An abstract method cannot be private as in the following, abstract class Demo() { private abstract void Call();

What is backing fields in C#?

A private field that stores the data exposed by a public property is called a backing store or backing field. Fields typically store the data that must be accessible to more than one type method and must be stored for longer than the lifetime of any single method.

What are abstract properties?

An abstract property declaration does not provide an implementation of the property accessors -- it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.


1 Answers

No it doesn't. I just tested with the following class:

public abstract class Class1
{
    public abstract string TestStringAbstract { get; set; }

    public string TestString { get; set; }
}

and decompiled it in Reflector. This was the generated code:

public abstract class Class1
{
    // Fields
    [CompilerGenerated]
    private string <TestString>k__BackingField;

    // Methods
    protected Class1()
    {
    }

    // Properties
    public string TestString
    {
        [CompilerGenerated]
        get
        {
            return this.<TestString>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            this.<TestString>k__BackingField = value;
        }
    }

    public abstract string TestStringAbstract { get; set; }
}

As you can see only a single backing field was generated for the concrete property. The abstract one was left as a definition.

This makes logical sense since the property must be overridden by any child class there is no point in creating a backing field that there would be no way of ever accessing (since you can't ever access the abstract property).

On the other hand a virtual property will create a backing field and any class that overrides the property with an auto-implemented replacement will create its own backing field at that class's level.

like image 181
Martin Harris Avatar answered Sep 25 '22 21:09

Martin Harris