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.
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.
An abstract method cannot be private as in the following, abstract class Demo() { private abstract void Call();
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With