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.

Chapter 17 ClassesSystem.Int32 System.String System.DoubleSystem.Object[]System.Object[]System.Int32 System.String System.DoubleIn the first and last invocations of F, the normal form of F is applicable because an implicit conversion existsfrom the argument type to the parameter type (both are of type object[]). Thus, overload resolution selects thenormal form of F, and the argument is passed as a regular value parameter. In the second and third invocations,the normal form of F is not applicable because no implicit conversion exists from the argument type to theparameter type (type object cannot be implicitly converted to type object[]). However, the expanded form ofF is applicable, so it is selected by overload resolution. As a result, a one-element object[] is created by theinvocation, and the single element of the array is initialized with the given argument value (which itself is areference to an object[]). end example]17.5.2 Static and instance methodsWhen a method declaration includes a static modifier, that method is said to be a static method. When nostatic modifier is present, the method is said to be an instance method.A static method does not operate on a specific instance, and it is a compile-time error to refer to this in a staticmethod.An instance method operates on a given instance of a class, and that instance can be accessed as this (§14.5.7).When a method is referenced in a member-access (§14.5.4) of the form E.M, if M is a static method, E mustdenote a type that has a method M, and if M is an instance method, E must denote an instance of a type that has amethod M.The differences between static and instance members are discussed further in §17.2.5.17.5.3 Virtual methodsWhen an instance method declaration includes a virtual modifier, that method is said to be a virtual method.When no virtual modifier is present, the method is said to be a non-virtual method.The implementation of a non-virtual method is invariant: The implementation is the same whether the method isinvoked on an instance of the class in which it is declared or an instance of a derived class. In contrast, theimplementation of a virtual method can be superseded by derived classes. The process of superseding theimplementation of an inherited virtual method is known as overriding that method (§17.5.4).In a virtual method invocation, the run-time type of the instance for which that invocation takes place determinesthe actual method implementation to invoke. In a non-virtual method invocation, the compile-time type of theinstance is the determining factor. In precise terms, when a method named N is invoked with an argument list Aon an instance with a compile-time type C and a run-time type R (where R is either C or a class derived from C),the invocation is processed as follows:• First, overload resolution is applied to C, N, and A, to select a specific method M from the set of methodsdeclared in and inherited by C. This is described in §14.5.5.1.• Then, if M is a non-virtual method, M is invoked.• Otherwise, M is a virtual method, and the most derived implementation of M with respect to R is invoked.For every virtual method declared in or inherited by a class, there exists a most derived implementation of themethod with respect to that class. The most derived implementation of a virtual method M with respect to a class Ris determined as follows:• If R contains the introducing virtual declaration of M, then this is the most derived implementation of M.• Otherwise, if R contains an override of M, then this is the most derived implementation of M.• Otherwise, the most derived implementation of M is the same as that of the direct base class of R.[Example: The following example illustrates the differences between virtual and non-virtual methods:233

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

Saved successfully!

Ooh no, something went wrong!