Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 17 Classes

1generic method-declaration for an explicit interface member implementation inherits any constraints from

2the constraints on the interface method. Similarly, a method declaration with the override modifier shall

3not have any type-parameter-constraints-clauses and the constraints of the method’s type parameters are

4inherited from the virtual method being overridden. Generic methods are fully specified in §26.6.

5The optional formal-parameter-list specifies the parameters of the method (§17.5.1).

6The return-type and each of the types referenced in the formal-parameter-list of a method shall be at least as

7accessible as the method itself (§10.5.4).

8For abstract and extern methods, the method-body consists simply of a semicolon. For all other

9methods, the method-body consists of a block, which specifies the statements to execute when the method is

10invoked.

11The name, the number of type parameters, and the formal parameter list of a method define the signature

12(§10.6) of the method. Specifically, the signature of a method consists of its name, the number of its type

13parameters, and the number, parameter-modifiers, and types of its formal parameters. The return type is not

14part of a method’s signature, nor are the names of the formal parameters, the names of the type parameters,

15or the constraints. When a formal parameter type references a type parameter of the method, the ordinal

16position of the type parameter (not the name of the type parameter) is used for type equivalence.

17The name of a method shall differ from the names of all other non-methods declared in the same class. In

18addition, the signature of a method shall differ from the signatures of all other methods declared in the same

19class, and two methods declared in the same class shall not have signatures that differ solely by ref and

20out.

2117.5.1 Method parameters

22The parameters of a method, if any, are declared by the method’s formal-parameter-list.

23formal-parameter-list:

24

fixed-parameters

25

fixed-parameters , parameter-array

26

parameter-array

27

fixed-parameters:

28

fixed-parameter

29

fixed-parameters , fixed-parameter

30

fixed-parameter:

31

attributesopt parameter-modifieropt type identifier

32

parameter-modifier:

33

ref

34out

35parameter-array:

36

attributesopt params array-type identifier

37The formal parameter list consists of one or more comma-separated parameters of which only the last can be

38a parameter-array.

39A fixed-parameter consists of an optional set of attributes (§24), an optional ref or out modifier, a type,

40and an identifier. Each fixed-parameter declares a parameter of the given type with the given name.

41A parameter-array consists of an optional set of attributes (§24), a params modifier, an array-type, and an

42identifier. A parameter array declares a single parameter of the given array type with the given name. The

43array-type of a parameter array shall be a single-dimensional array type (§19.1). In a method invocation, a

44parameter array permits either a single argument of the given array type to be specified, or it permits zero or

45more arguments of the array element type to be specified. Parameter arrays are described further in

46§17.5.1.4.

279

C# LANGUAGE SPECIFICATION

1A method declaration creates a separate declaration space (§10.3) for type parameters, formal parameters,

2local variables, and local constants. Names are introduced into this declaration space by the type parameter

3list and formal parameter list of the method and by local variable declarations and by local constant

4declarations in the block of the method. All names in the declaration space of a method shall be unique;

5otherwise, a compile-time error results.

6A method invocation (§14.5.5.1) creates a copy, specific to that invocation, of the formal parameters and

7local variables of the method, and the argument list of the invocation assigns values or variable references to

8the newly created formal parameters. Within the block of a method, formal parameters can be referenced by

9their identifiers in simple-name expressions (§14.5.2).

10There are four kinds of formal parameters:

11Value parameters, which are declared without any modifiers.

12Reference parameters, which are declared with the ref modifier.

13Output parameters, which are declared with the out modifier.

14Parameter arrays, which are declared with the params modifier.

15[Note: As described in §10.6, the ref and out modifiers are part of a method’s signature, but the params

16modifier is not. end note]

1717.5.1.1 Value parameters

18A parameter declared with no modifiers is a value parameter. A value parameter corresponds to a local

19variable that gets its initial value from the corresponding argument supplied in the method invocation.

20When a formal parameter is a value parameter, the corresponding argument in a method invocation shall be

21an expression of a type that is implicitly convertible (§13.1) to the formal parameter type.

22A method is permitted to assign new values to a value parameter. Such assignments only affect the local

23storage location represented by the value parameter—they have no effect on the actual argument given in the

24method invocation.

2517.5.1.2 Reference parameters

26A parameter declared with a ref modifier is a reference parameter. Unlike a value parameter, a reference

27parameter does not create a new storage location. Instead, a reference parameter represents the same storage

28location as the variable given as the argument in the method invocation.

29When a formal parameter is a reference parameter, the corresponding argument in a method invocation shall

30consist of the keyword ref followed by a variable-reference (§12.3.3.27) of the same type as the formal

31parameter. A variable shall be definitely assigned before it can be passed as a reference parameter.

32Within a method, a reference parameter is always considered definitely assigned.

33[Example: The example

34using System;

35class Test

36{

37

static

void Swap(ref int x, ref int y) {

38

int

temp = x;

39

x =

y;

40

y = temp;

41

}

 

42

static void Main() {

43

int i = 1, j = 2;

44

Swap(ref i, ref j);

45

Console.WriteLine("i = {0}, j = {1}", i, j);

46}

47}

280

Chapter 17 Classes

1produces the output

2i = 2, j = 1

3For the invocation of Swap in Main, x represents i and y represents j. Thus, the invocation has the effect of

4swapping the values of i and j. end example]

5In a method that takes reference parameters, it is possible for multiple names to represent the same storage

6location. [Example: In the following code

7class A

8{

9

string s;

10

void F(ref string a, ref string b) {

11

s = "One";

12

a = "Two";

13

b = "Three";

14

}

15

void G() {

16

F(ref s, ref s);

17}

18}

19the invocation of F in G passes a reference to s for both a and b. Thus, for that invocation, the names s, a,

20and b all refer to the same storage location, and the three assignments all modify the instance field s. end

21example]

2217.5.1.3 Output parameters

23A parameter declared with an out modifier is an output parameter. Similar to a reference parameter, an

24output parameter does not create a new storage location. Instead, an output parameter represents the same

25storage location as the variable given as the argument in the method invocation.

26When a formal parameter is an output parameter, the corresponding argument in a method invocation shall

27consist of the keyword out followed by a variable-reference (§12.3.3.27) of the same type as the formal

28parameter. A variable need not be definitely assigned before it can be passed as an output parameter, but

29following an invocation where a variable was passed as an output parameter, the variable is considered

30definitely assigned.

31Within a method, just like a local variable, an output parameter is initially considered unassigned and shall

32be definitely assigned before its value is used.

33Every output parameter of a method shall be definitely assigned before the method returns.

34Output parameters are typically used in methods that produce multiple return values. [Example:

35using System;

36class Test

37{

38

static void SplitPath(string path, out string dir, out string name) {

39

int i = path.Length;

40

while (i > 0) {

41

char ch = path[i – 1];

42

if (ch == '\\' || ch == '/' || ch == ':') break;

43

i--;

44

}

45

dir = path.Substring(0, i);

46

name = path.Substring(i);

47

}

48

static void Main() {

49

string dir, name;

50

SplitPath(@"c:\Windows\System\hello.txt", out dir, out name);

51

Console.WriteLine(dir);

52

Console.WriteLine(name);

53}

54}

281

C# LANGUAGE SPECIFICATION

1The example produces the output:

2c:\Windows\System\

3hello.txt

4Note that the dir and name variables can be unassigned before they are passed to SplitPath, and that they

5are considered definitely assigned following the call. end example]

617.5.1.4 Parameter arrays

7A parameter declared with a params modifier is a parameter array. If a formal parameter list includes a

8parameter array, it shall be the last parameter in the list and it shall be of a single-dimensional array type.

9[Example: The types string[] and string[][,] can be used as the type of a parameter array, but the

10type string[,] can not. end example] It is not possible to combine the params modifier with the

11modifiers ref and out.

12A parameter array permits arguments to be specified in one of two ways in a method invocation:

13The argument given for a parameter array can be a single expression that is implicitly convertible

14(§13.1) to the parameter array type. In this case, the parameter array acts precisely like a value

15parameter.

16Alternatively, the invocation can specify zero or more arguments for the parameter array, where each

17argument is an expression that is implicitly convertible (§13.1) to the element type of the parameter

18array. In this case, the invocation creates an instance of the parameter array type with a length

19corresponding to the number of arguments, initializes the elements of the array instance with the given

20argument values, and uses the newly created array instance as the actual argument.

21Except for allowing a variable number of arguments in an invocation, a parameter array is precisely

22equivalent to a value parameter (§17.5.1.1) of the same type.

23[Example: The example

24using System;

25class Test

26{

27

static void F(params int[] args) {

28

Console.Write("Array contains {0} elements:", args.Length);

29

foreach (int i in args)

30

Console.Write(" {0}", i);

31

Console.WriteLine();

32

}

33

static void Main() {

34

int[] arr = {1, 2, 3};

35

F(arr);

36

F(10, 20, 30, 40);

37

F();

38}

39}

40produces the output

41Array contains 3 elements: 1 2 3

42Array contains 4 elements: 10 20 30 40

43Array contains 0 elements:

44The first invocation of F simply passes the array arr as a value parameter. The second invocation of F

45automatically creates a four-element int[] with the given element values and passes that array instance as a

46value parameter. Likewise, the third invocation of F creates a zero-element int[] and passes that instance

47as a value parameter. The second and third invocations are precisely equivalent to writing:

48F(new int[] {10, 20, 30, 40});

49F(new int[] {});

50end example]

282

Chapter 17 Classes

1When performing overload resolution, a method with a parameter array might be applicable, either in its

2normal form or in its expanded form (§14.4.2.1). The expanded form of a method is available only if the

3normal form of the method is not applicable and only if a method with the same signature as the expanded

4form is not already declared in the same type.

5[Example: The example

6using System;

7class Test

8{

9

static void F(params object[] a) {

10

Console.WriteLine("F(object[])");

11

}

 

 

12

static void F() {

13

Console.WriteLine("F()");

14

}

 

 

15

static void F(object a0, object a1) {

16

Console.WriteLine("F(object,object)");

17

}

 

 

18

static void Main() {

19

F();

 

 

20

F(1);

 

 

21

F(1, 2);

 

22

F(1,

2,

3);

23

F(1, 2, 3, 4);

24}

25}

26produces the output

27F();

28F(object[]);

29F(object,object);

30F(object[]);

31F(object[]);

32In the example, two of the possible expanded forms of the method with a parameter array are already

33included in the class as regular methods. These expanded forms are therefore not considered when

34performing overload resolution, and the first and third method invocations thus select the regular methods.

35When a class declares a method with a parameter array, it is not uncommon to also include some of the

36expanded forms as regular methods. By doing so it is possible to avoid the allocation of an array instance

37that occurs when an expanded form of a method with a parameter array is invoked. end example]

38When the type of a parameter array is object[], a potential ambiguity arises between the normal form of

39the method and the expended form for a single object parameter. The reason for the ambiguity is that an

40object[] is itself implicitly convertible to type object. The ambiguity presents no problem, however,

41since it can be resolved by inserting a cast if needed.

42[Example: The example

43using System;

44class Test

45{

46

static void F(params object[] args) {

47

foreach (object o in args) {

48

Console.Write(o.GetType().FullName);

49

Console.Write(" ");

50

}

51

Console.WriteLine();

52

}

283

Соседние файлы в предмете Электротехника