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 17 ClassesA method invocation (§14.5.5.1) creates a copy, specific to that invocation, of the formal parameters and localvariables of the method, and the argument list of the invocation assigns values or variable references to the newlycreated formal parameters. Within the block of a method, formal parameters can be referenced by their identifiersin simple-name expressions (§14.5.2).There are four kinds of formal parameters:• Value parameters, which are declared without any modifiers.• Reference parameters, which are declared with the ref modifier.• Output parameters, which are declared with the out modifier.• Parameter arrays, which are declared with the params modifier.[Note: As described in §10.6, the ref and out modifiers are part of a method’s signature, but the paramsmodifier is not. end note]17.5.1.1 Value parametersA parameter declared with no modifiers is a value parameter. A value parameter corresponds to a local variablethat gets its initial value from the corresponding argument supplied in the method invocation.When a formal parameter is a value parameter, the corresponding argument in a method invocation must be anexpression of a type that is implicitly convertible (§13.1) to the formal parameter type.A method is permitted to assign new values to a value parameter. Such assignments only affect the local storagelocation represented by the value parameter—they have no effect on the actual argument given in the methodinvocation.17.5.1.2 Reference parametersA parameter declared with a ref modifier is a reference parameter. Unlike a value parameter, a referenceparameter does not create a new storage location. Instead, a reference parameter represents the same storagelocation as the variable given as the argument in the method invocation.When a formal parameter is a reference parameter, the corresponding argument in a method invocation mustconsist of the keyword ref followed by a variable-reference (§12.3.3) of the same type as the formal parameter.A variable must be definitely assigned before it can be passed as a reference parameter.Within a method, a reference parameter is always considered definitely assigned.[Example: The exampleusing System;class Test{static void Swap(ref int x, ref int y) {int temp = x;x = y;y = temp;}static void Main() {int i = 1, j = 2;Swap(ref i, ref j);Console.WriteLine("i = {0}, j = {1}", i, j);}}produces the outputi = 2, j = 1For the invocation of Swap in Main, x represents i and y represents j. Thus, the invocation has the effect ofswapping the values of i and j. end example]229

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

Saved successfully!

Ooh no, something went wrong!