13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATION20.2.3 Interface eventsInterface events are declared using interface-event-declarations:interface-event-declaration:attributes opt new opt event type identifier ;The attributes, type, and identifier of an interface event declaration have the same meaning as those of an eventdeclaration in a class (§17.7).20.2.4 Interface indexersInterface indexers are declared using interface-indexer-declarations:interface-indexer-declaration:attributes opt new opt type this [ formal-parameter-list ] { interface-accessors }The attributes, type, and formal-parameter-list of an interface indexer declaration have the same meaning asthose of an indexer declaration in a class (§17.8).The accessors of an interface indexer declaration correspond to the accessors of a class indexer declaration(§17.8), except that the accessor body must always be a semicolon. Thus, the accessors simply indicate whetherthe indexer is read-write, read-only, or write-only.20.2.5 Interface member accessInterface members are accessed through member access (§14.5.4) and indexer access (§14.5.6.2) expressions ofthe form I.M and I[A], where I is an expression having an interface type, M is a method, property, or event ofthat interface type, and A is an indexer argument list.For interfaces that are strictly single-inheritance (each interface in the inheritance chain has exactly zero or onedirect base interface), the effects of the member lookup (§14.3), method invocation (§14.5.5.1), and indexeraccess (§14.5.6.2) rules are exactly the same as for classes and structs: More derived members hide less derivedmembers with the same name or signature. However, for multiple-inheritance interfaces, ambiguities can occurwhen two or more unrelated base interfaces declare members with the same name or signature. This sectionshows several examples of such situations. In all cases, explicit casts can be used to resolve the ambiguities.[Example: In the exampleinterface IList{int Count { get; set; }}interface ICounter{void Count(int i);}interface IListCounter: IList, ICounter {}class C{void Test(IListCounter x) {x.Count(1);// Errorx.Count = 1;// Error((IList)x).Count = 1; // Ok, invokes IList.Count.set((ICounter)x).Count(1); // Ok, invokes ICounter.Count}}the first two statements cause compile-time errors because the member lookup (§14.3) of Count inIListCounter is ambiguous. As illustrated by the example, the ambiguity is resolved by casting x to theappropriate base interface type. Such casts have no run-time costs—they merely consist of viewing the instanceas a less derived type at compile-time. end example][Example: In the example282

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!