The return type of the members on an Interface Implementation must match exactly the interface definition?

前端 未结 6 1678
栀梦
栀梦 2020-12-05 11:58

According to CSharp Language Specification.

An interface defines a contract that can be implemented by classes and structs. An interface does not p

6条回答
  •  春和景丽
    2020-12-05 12:29

    You need 13.4.4 from the specification:

    For purposes of interface mapping, a class member A matches an interface member B when:

    A and B are properties, the name and type of A and B are identical, and A has the same accessors as B (A is permitted to have additional accessors if it is not an explicit interface member implementation).

    Additionally, your belief that List Integers { get; set; } satisfies the contract of IEnumerable Integers { get; set; } is false. Even if the specification were somehow relaxed to not require that the return types be identical, note that a property of type List with a public setter is not anywhere near the same as a property of type IEnumerable with a public setter because to the latter you can assign an instance of int[], but to the former you can not.

提交回复
热议问题