I'm not strong on c# interfaces, so this is me misunderstanding something. I have this interface (PMQIdent is just an identifier at heart):
public interface IisNamedItem2 {
// note: is virtual
public virtual PMQIdent name {
get => name;
private set => name = value;
}
}
used like this:
public class TVDeclarationStatement2 : IisNamedItem2 {
// ctor
public TVDeclarationStatement2(PMQIdent nameIn) =>
name = nameIn;
}
But it complains that "the name 'name' does not exist in the current context"
As I marked the relevant part is virtual, I'd expect that to be carried into the class (edit: meaning effectively copied into the using class's definition, so it would just be there instead of me having to add it each time).
If I rewrite the interface as
public interface IisNamedItem999 {
private PMQIdent _name;
public virtual PMQIdent getName() => _name;
public virtual PMQIdent sestName(PMQIdent val) =>
_name = val;
}
It - quite reasonably - complains “Interfaces cannot contain instance fields”
What’s the right way to do this?
More importantly, what is the conceptual thing I’m missing that is making me misunderstand this?
Very helpful answers and comments all round. I've accepted Stefan's answer as it explains why I my thinking was wrong. Thanks all, and I've got some good links to read.