How do Auto-Implemented properties work in C# interfaces? - c#

If I declare Auto-Implemented properties in C# classes, then public string Property { get; set; } becomes:
private string _property;
public string get_Property() {
return _property;
}
public void set_Property(string value) {
_property = value;
}
which includes a private field string _property.
If I create an interface, I am allowed to use Auto-Implemented properties like so:
string Property { get; set; }
Why can I declare Auto-Implemented properties inside Interface, but I can't use the longer, more verbose syntax that declares private fields? I am aware of the definition that says:
An interface contains only the signatures of methods, properties, events or indexers
Is the private field in interfaces not generated the same as in classes? Is it generated at all?

Why can I declare Auto-Implemented properties inside Interface
They aren't auto-implemented, it's just that the syntax of a property declaration is identical to the syntax for an auto-implemented property definition.
This:
interface IFoo
{
String Bar { get; set; }
}
Means: "IFoo has a public[1] String property named Bar which has a getter and a setter."
This:
class Foo
{
String Bar { get; set; }
}
Means: "Foo has a private[2] String property named Bar which has a getter and a setter, and the getter and setter are both auto-generated by the compiler and operate on a hidden instance field.
Note that the syntax used in the interface is unrelated to the syntax used by implementation in the class or struct. So given the same IFoo as above...
interface IFoo
{
String Bar { get; set; }
}
...we can have:
// Using auto-implemented property:
class Foo2 : IFoo
{
public String Bar { get; set; }
}
// Using explicit backing field:
class Foo3 : IFoo
{
private String bar;
public String Bar
{
get { return this.bar; }
set { this.bar = value; }
}
}
// Using expression-body syntax with a backing field:
class Foo4 : IFoo
{
private String bar;
public String Bar
{
get => this.bar;
set => this.bar = value;
}
}
// Using explicit interface implementation with a backing field:
class Foo5 : IFoo
{
private String bar;
String IFoo.Bar
{
get { return this.bar; }
set { this.bar = value; }
}
}
// You can also use explicit interface implementation with an auto-implemented property:
class Foo6 : IFoo
{
String IFoo.Bar { get; set; }
}
// However, if it's a getter-only property you won't be able to set a property value in the constructor - but you can initialize it inline:
interface IReadOnlyFoo
{
String Bar { get; }
}
class Foo7 : IReadOnlyFoo
{
String IReadOnlyFoo.Bar { get; } = "foo"; // ok
}
class Foo8 : IReadOnlyFoo
{
public Foo8()
{
this.Bar = "foo"; // <-- Error. `Bar` is not a member of `this`.
// You also can't cast `(IReadOnlyFoo)this` because `IReadOnlyFoo` does not contain a setter.
}
String IReadOnlyFoo.Bar { get; }
}
Is the private field in interfaces not generated the same as in classes
Interfaces don't have fields, interfaces only have virtual methods (or rather: an interface can be thought-of as a single vtable). Note that internally: properties and events are also fundamentally virtual methods (also note that while internally they're virtual calls, implemented interface methods are not automatically virtual (in the C# sense) in that a subclass of an interface implementation cannot arbitrarily override any interface member[3].
Also not to be confused with "Default interface implementations" in C# 8.0 which a more akin to extension methods than treating interfaces as classes, because interfaces still cannot have fields.
You should also familarise yourself with C#'s expression-bodied member syntax (though it isn't used much for property-setters):
class Foo2
{
String bar; // this is a private instance field
String Bar // this is a private instance property
{
get => this.bar;
set => this.bar = value;
}
}
[1] Disregarding Explicit Interface Implementation, of course.
[2] Class members are private by default if they don't have an explicit access-modifier.
[3] Subclasses can reimplement an interface which will have the effect of overriding any virtual-calls to that interface member, but only if the member is accessed through an interface reference rather than through an object reference to the class's supertype.

Related

Is it possible to use an auto property with an explicit interface implementation? [duplicate]

Is there any way to implement an interface explicitly using an automatic property? For instance, consider this code:
namespace AutoProperties
{
interface IMyInterface
{
bool MyBoolOnlyGet { get; }
}
class MyClass : IMyInterface
{
static void Main(){}
public bool MyBoolOnlyGet { get; private set; } // line 1
//bool IMyInterface.MyBoolOnlyGet { get; private set; } // line 2
}
}
This code compiles. However, if you replace line 1 with line 2 it does not compile.
(It's not that I need to get line 2 working - I'm just curious.)
Indeed, that particular arrangement (explicit implementation of a get-only interface property by an automatically implemented property) isn't supported by the language. So either do it manually (with a field), or write a private auto-implemented prop, and proxy to it. But to be honest, by the time you've done that you might as well have used a field...
private bool MyBool { get;set;}
bool IMyInterface.MyBoolOnlyGet { get {return MyBool;} }
or:
private bool myBool;
bool IMyInterface.MyBoolOnlyGet { get {return myBool;} }
The problem is that the interface has only getter and you try to explicitly implement it with getter and setter.
When you explicitly implement an interface the explicit implementation will be called only when your reference if of the interface type, so... if the interface only has getter there is no way to use the setter so it makes no sense to have a setter there.
This, for example will compile:
namespace AutoProperties
{
interface IMyInterface
{
bool MyBoolOnlyGet { get; set; }
}
class MyClass : IMyInterface
{
static void Main() { }
bool IMyInterface.MyBoolOnlyGet { get; set; }
}
}

Read-only interface without requiring body in abstract class?

I'd like to be able to create a read-only property in an interface that is writable in the instantiated class, but I'm having problems if the inheritance goes through an abstract class:
interface IFoo {
string Foo { get; }
}
abstract class Bar : IFoo {
}
class Baz : Bar {
public string Foo { get; private set; }
}
This gives me an error Bar does not implement interface member IFoo.Foo. Is there any way to circumvent this? I want the ultimately-instantiated class to determine the visibility of the setter.
You need to implement it abstract:
abstract class Bar : IFoo
{
public abstract string Foo { get; }
}
However, it doesn't look like when you do this that you can choose to override the visibility in the Baz class.
The best you can do is to use a backing field in the derived class:
class Baz : Bar
{
private string _foo;
public override string Foo
{
get { return _foo; }
}
}
This answer on a duplicate question goes into more detail on the why. The problem stems from the fact that property getters and setters are abstract, rather than the property itself.

Implementing interfaces in C# .NET

Consider the following interface
public interface ICustomData
{
String CustomData { set; get; }
}
According to MSDN documentation, interfaces members are automatically public.
Lets say I now want to implement my interface:
public class CustomDataHandler : ICustomData
{
}
This would break at compile time, telling me that I have not implemented "CustomData"
This on the otherhand would work:
public class CustomDataHandler : ICustomData
{
public String CustomData { set; get; }
}
My question is: If the member of the interface is automatically public, why MUST I declare the implementation in the class? If the interface declaration is syntactically identical to the class declaration, why can the compiler not infer this automatically from my interface?
EDIT:
My reason for asking. Imagine a scenario where you are building data models, entities etc. I might code some interfaces to these models like so:
public interface IUserAccount
{
Guid Identity { set; get; }
String FirstName { set; get; }
String LastName { set; get; }
String EmailAddress { set; get; }
String Password { set; get; }
}
public interface IUserDataEntry
{
Guid DataIdentity { set; get; }
String DataName { set; get; }
String Data { set; get; }
}
It would be far simpler to construct the models like so:
public class UserAccount : IUserAccount
{
}
public class UserDataEntry : IUserDataEntry
{
}
public class Combined : IUserAccount, IUserDataEntry
{
}
An interface is not there to provide an implementation, it is there to define a contract. This then allows for different implementations to be built which implement it.
They may be syntactically identical, but they mean different things (i.e. they are not semantically identical).
In the interface, the syntax means that an implementing class must expose such a property, with get and set accessors implemented as it sees fit (either explicitly or implicitly). An interface merely defines the outward behaviour that a class must provide; it does not provide any implementation of that behaviour.
In the class, the syntax is an "auto-property", an implementation of the property defined by the interface, and the get and set accessors are implicitly converted into full implementations with a backing field. It looks something like this when it's compiled:
public class CustomDataHandler : ICustomData
{
private string customData;
public string CustomData
{
get
{
return customData;
}
set
{
customData = value;
}
}
}
You are implicitly implementing the interface. In this instance the method signatures of the class must match those of the interface (including accessibility). Ensuring that the methods are marked as public ensures that there are no surprises when looking at the class, for instance:
public class CustomDataHandler : ICustomData
{
String CustomData {get; set}
String PrivateCustomData {get;set;}
}
Even though both properties are declared the same, the CustomData property would be public by virtue of it being declared on the interface even though the declaration looks identical to that of PrivateCustomData. This would be inconsistent and lead to harder to maintain code.
If you do not wish to set the access modifier, you could explicitly implement the interface:
public class CustomDataHandler : ICustomData
{
String ICustomData.CustomData { set; get; }
}
The interface declaration is only specifying the behaviour which the interface defines. In your case, this is a property called CustomData which has a get and set (it is a read/write property) which is of type string.
The class which implements the interface needs to do exactly that - to specify the implementation.
Now in your case, you are using auto implemented properties { get; set; } which looks the same as the interface declaration, however you could also have a backing field and behaviour in your get or set methods.
Here's an example where the CustomData property is private in a derived class:
public class CustomDataHandler : ICustomData
{
private string CustomData { set; get; }
string ICustomData.CustomData { get; set; }
}
But this code compiles, because there is also an explicit implementation of the property.
So, the public modifier is not redundant in this case.
You must explicitly implement it because... You are not limited to implementing it that way. You could use a field, or do something else in your method. An interface is only a method contract that guaranty that this method exist.
public class CustomDataHandler : ICustomData
{
public String CustomData
{
get { return "None of your business!"; }
set { } // Decide to do nothing
}
}
The interface only guaranty this method will exist. Not what you're gonna do with it.
EDIT: As for your edit of your question, you probably seek to have a parent class instead of an interface if you want to implement the method only once for a collection of classes. However, while you can combine interface, you cannot combine parent classes. But, you can add interface at different point of a classes hierarchy.

How to automatically copy a property declaration to base class

I have a property A in all subclasses of base class Base.
How can I generate an abstract property definition of property A into base class Base?
I know ReSharper's refactoring Pull Members Up, but that moves the property to base class.
I need an abstract property in base class and a overriding properties in all sub classes. Is there a refactoring in Visual Studio or in ReSharper that can do it automatically for me?
There is a checkbox "Make abstract" for that in ReSharper Pull Members Up dialog :
I'm not sure Resharper can move up and create an abstraction as you want automatically, but you can atleast define it manually like this
In abstract class:
public abstract double A
{
get;
}
In Sub class:
public override double A
{
get
{
return 3.141;
}
}
It might be a clearner design to define a new Interface (or use an existing one) and define the property in the interface. That way, your existing subclasses won't have to use override.
public interface IInterface {
string MyProperty { get; }
}
public class Class : IInterface {
public string MyProperty { get; set; }
}
public abstract class AbstractClass {
public abstract string Value { get; }
}
public class ConcreteClass : AbstractClass {
private string m_Value;
public override string Value {
get { return m_Value; }
}
public void SetValue(string value) {
m_Value = value;
}
}
I hope this will be helpful to you.

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.
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.
No. Since it's abstract, the class implementer must implement the property. If the implementer declares it that way, then Yes, it's an automatic property with a hidden member to hold the actual value.
There's a difference between:
public abstract string Name { get; set; }
and
public string Name { get; set; }
The first property declaration doesn't create a backing field. It just creates an abstract property (kind of like an interface method declaration), which has to be implemented by any non-abstract inheriting class.
The second declaration is an auto-property, which DOES create a backing field. It's actually compiler syntactic sugar shorthand for:
private string _name;
public string Name { get { return _name; } set { _name = value; } }

Categories