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 SPECIFICATIONThe new delegate instance is initialized with a reference to the method that was determined at compile-timeand a reference to the target object computed above.• If E is a value of a delegate-type:E is evaluated. If this evaluation causes an exception, no further steps are executed.If the value of E is null, a System.NullReferenceException is thrown and no further steps areexecuted.A new instance of the delegate type D is allocated. If there is not enough memory available to allocate thenew instance, a System.OutOfMemoryException is thrown and no further steps are executed.The new delegate instance is initialized with references to the same invocation list as the delegate instancegiven by E.The method and object to which a delegate refers are determined when the delegate is instantiated and thenremain constant for the entire lifetime of the delegate. In other words, it is not possible to change the targetmethod or object of a delegate once it has been created. [Note: Remember, when two delegates arecombined or one is removed from another, a new delegate results; no existing delegate has its contentchanged. end note]It is not possible to create a delegate that refers to a property, indexer, user-defined operator, instanceconstructor, destructor, or static constructor.[Example: As described above, when a delegate is created from a method group, the formal parameter listand return type of the delegate determine which of the overloaded methods to select. In the exampledelegate double DoubleFunc(double x);class A{DoubleFunc f = new DoubleFunc(Square);static float Square(float x) {return x * x;}static double Square(double x) {return x * x;}}the A.f field is initialized with a delegate that refers to the second Square method because that methodexactly matches the formal parameter list and return type of DoubleFunc. Had the second Square methodnot been present, a compile-time error would have occurred. end example]14.5.11 The typeof operatorThe typeof operator is used to obtain the System.Type object for a type.typeof-expression:typeof ( type )typeof ( void )The first form of typeof-expression consists of a typeof keyword followed by a parenthesized type. Theresult of an expression of this form is the System.Type object for the indicated type. There is only oneSystem.Type object for any given type. [Note: This means that for type T, typeof(T) == typeof(T)is always true. end note]The second form of typeof-expression consists of a typeof keyword followed by a parenthesized voidkeyword. The result of an expression of this form is the System.Type object that represents the absence ofa type. The type object returned by typeof(void) is distinct from the type object returned for any type.[Note: This special type object is useful in class libraries that allow reflection onto methods in the language,where those methods wish to have a way to represent the return type of any method, including void methods,with an instance of System.Type. end note]150

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

Saved successfully!

Ooh no, something went wrong!