Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Please tell me whether the following code will complie or not?</p> </blockquote> <p>Yes, it will - but you could have found that out for yourself just by trying it.</p> <blockquote> <p>Why it is so?</p> </blockquote> <p>Quite simply, <code>Derived</code> implements the contract required by <code>MyInterface</code>. It happens to do that via <code>Base</code> which is unaware of the interface, but it still has all the relevant members.</p> <p>There's a danger here, though - in reality, interfaces are about more than just having appropriate members available for callers; they're about the <em>semantics</em> of those members. A class hierarchy like this only works when the semantics of <code>Base.Method()</code> exactly match the requirements of <code>MyInterface.Method()</code> <em>by coincidence</em> (as opposed to by explicit, declared design). Even if it's valid to start with, the maintainer of <code>Base</code> may decide to make a change to the behaviour of <code>Method</code> which is valid according to what <code>Base.Method</code> has guaranteed, but <em>isn't</em> valid according to what the interface guarantees.</p> <p>EDIT: In terms of the language specification, this is in section 13.4.4 of the C# 4 spec:</p> <blockquote> <p>A class or struct must provide implementations of all members of the interfaces that are listed in the base class list of the class or struct. The process of locating implementations of interface members in an implementing class or struct is known as interface mapping.</p> <p>Interface mapping for a class or struct C locates an implementation for each member of each interface specified in the base class list of C. The implementation of a particular interface member I.M, where I is the interface in which the member M is declared, is determined by examining each class or struct S, starting with C and repeating for each successive base class of C, until a match is located.</p> <p>[...]</p> <p>A compile-time error occurs if implementations cannot be located for all members of all interfaces specified in the base class list of C. </p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload