13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 22 Delegates22. Delegates[Note: Delegates enable scenarios that other languages—such as C++, Pascal, and Modula—have addressed withfunction pointers. Unlike C++ function pointers, however, delegates are fully object oriented, and unlikeC++ pointers to member functions, delegates encapsulate both an object instance and a method. end note]A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instanceencapsulates one or more methods, each of which is referred to as a callable entity. For instance methods, acallable entity consists of an instance and a method on that instance. For static methods, a callable entity consistsof just a method. Given a delegate instance and an appropriate set of arguments, one can invoke all of thatdelegate instance’s methods with that set of arguments.[Note: An interesting and useful property of a delegate instance is that it does not know or care about the classesof the methods it encapsulates; all that matters is that those methods be compatible (§22.1) with the delegate’stype. This makes delegates perfectly suited for “anonymous” invocation. end note]22.1 Delegate declarationsA delegate-declaration is a type-declaration (§16.5) that declares a new delegate type.delegate-declaration:attributes opt delegate-modifiers opt delegate return-type identifier ( formal-parameter-list opt ) ;delegate-modifiers:delegate-modifierdelegate-modifiers delegate-modifierdelegate-modifier:newpublicprotectedinternalprivateIt is a compile-time error for the same modifier to appear multiple times in a delegate declaration.The new modifier is only permitted on delegates declared within another type, in which case it specifies that sucha delegate hides an inherited member by the same name, as described in §17.2.2.The public, protected, internal, and private modifiers control the accessibility of the delegate type.Depending on the context in which the delegate declaration occurs, some of these modifiers may not be permitted(§10.5.1).The delegate’s type name is identifier.The optional formal-parameter-list specifies the parameters of the delegate, and return-type indicates the returntype of the delegate. A method and a delegate type are compatible if both of the following are true:• They have the same number or parameters, with the same types, in the same order, with the same parametermodifiers.• Their return-types are the same.Delegate types in <strong>C#</strong> are name equivalent, not structurally equivalent. [Note: However, instances of two distinctbut structurally equivalent delegate types may compare as equal (§14.9.8). end note] Specifically, two differentdelegate types that have the same parameter lists and return type are considered different delegate types.[Example: For example:297

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

Saved successfully!

Ooh no, something went wrong!