X hits on this document

1201 views

0 shares

0 downloads

0 comments

33 / 396

C# LANGUAGE SPECIFICATION

vars["x"] = 1.5; vars["y"] = 9; Console.WriteLine(e.Evaluate(vars));// Outputs "16.5" } }

1.6.5.5 Method overloading

Method overloading permits multiple methods in the same class to have the same name as long as they have unique signatures. When compiling an invocation of an overloaded method, the compiler uses overload resolution to determine the specific method to invoke. Overload resolution finds the one method that best matches the arguments or reports an error if no single best match can be found. The following example shows overload resolution in effect. The comment for each invocation in the Main method shows which method is actually invoked.

class Test { static void F() {...} Console.WriteLine("F()"); }

static void F(object x) { Console.WriteLine("F(object)"); }

static void F(int x) { Console.WriteLine("F(int)"); }

static void F(double x) { Console.WriteLine("F(double)"); }

static void F(double x, double y) { Console.WriteLine("F(double, double)"); }

static void Main() { F();// Invokes F() F(1);// Invokes F(int) F(1.0);// Invokes F(double) F("abc");// Invokes F(object) F((double)1);// Invokes F(double) F((object)1);// Invokes F(object) F(1, 1);// Invokes F(double, double) } }

As shown by the example, a particular method can always be selected by explicitly casting the arguments to the exact parameter types.

1.6.6 Other function members

Members that contain executable code are collectively known as the function members of a class. The preceding section describes methods, which are the primary kind of function members. This section describes the other kinds of function members supported by C#: constructors, properties, indexers, events, operators, and destructors.

The following table shows a class called List, which implements a growable list of objects. The class contains several examples of the most common kinds of function members.

20Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1201
Page views1201
Page last viewedWed Jan 18 22:34:52 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments