According to an answer for Why are we not allowed to specify a constructor in an interface?,
Because an interface describes behaviour. Constructors aren't behaviour. How an object is built is an implementation detail.
If interface
describes behavior, why does interface
allow declaring state?
public interface IStateBag
{
object State { get; }
}
Well - its not really state. If interfaces allowed you to declare fields then that would be state. Since a property is just syntax sugar for get and set methods it is allowed.
Here is an example:
interface IFoo
{
Object Foo { get; set; }
}
The previous interface gets compiled to the following IL:
.class private interface abstract auto ansi IFoo
{
.property instance object Foo
{
.get instance object IFoo::get_Foo()
.set instance void IFoo::set_Foo(object)
}
}
As you can see, even the interface sees the property as methods.
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