简体   繁体   中英

Auto properties with different accessors in an Interface

is there any diffrence between this 3 auto properties ?

interface MyInterface {
    public int p1 { get; set; }
    public int p2 { get; }
    public int p3 { set; }
}

also why we can write this code in an interface but not in a class ?

public int p { get; }

For the same reason you can write this in an interface:

interface IFace {
    void Test();
}

Also, your interface is invalid, as public isn't valid in an interface. The point being, different things are legal in interfaces and classes.

When you do public int P1 { get; set; } public int P1 { get; set; } public int P1 { get; set; } in a class, that turns into a auto property. However, you can't do public int P1 { get; } public int P1 { get; } , because what would you want that to mean? Should it always return 0? There is no way to set it. So if you want a read only property you have to define the getter yourself like this:

int _p1;

public int P1 {
    get { return _p1; }
}

Also. Another way to achieve more or less the same is this:

public int P1 { get; private set; }

There are differences between those properties. Firstly, you should remove the public modifier from your declaration. Secondly, by putting get or set within the block you define what properties in derived classes should look like. For example, public int p1 { get; set; } public int p1 { get; set; } public int p1 { get; set; } requires getter and setter in a derived class, public int p2 { get; } public int p2 { get; } only getter, and public int p3 { set; } public int p3 { set; } requires only setter to be implemented.

You can't use access modifiers inside interfaces because interfaces are guidelines for other developers that force them to go in a certain direction when developing the implementing classes.

Look at this post for more information about that.

Keep in mind interface does NOT contain any implementation data. When you add property in an interface, it merely says that a class implementing this interface needs to have said property with get, set or both methods, depending on what you wrote. So any class implementing your interface has to implement (or have auto-generated) p1 property with get and set method, p2 with get method, and p3 with set method. Interface doesn't care whether these will be auto-generated or your own custom implementations, they just have to be in an implementing class.

Therefore, you can write

int p { get; }

in an interface as all it does is telling that any class implementing this interface has to have property p with getter, again, not caring about its actual implementation - you could write a getter that does some computations, returns some constant, etc. OTOH in a class writing the same would mean that you want a property with auto-generated backing field, except since it would have no setter, you couldn't actually change its value, so it would always have its default value 0.

And as noted, you cannot write access modifiers in an interface, as all interface members are implicitly public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM