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 SPECIFICATIONIn a method that takes reference parameters, it is possible for multiple names to represent the same storagelocation. [Example: In the exampleclass A{string s;void F(ref string a, ref string b) {s = "One";a = "Two";b = "Three";}void G() {F(ref s, ref s);}}the invocation of F in G passes a reference to s for both a and b. Thus, for that invocation, the names s, a, and ball refer to the same storage location, and the three assignments all modify the instance field s. end example]17.5.1.3 Output parametersA parameter declared with an out modifier is an output parameter. Similar to a reference parameter, an outputparameter does not create a new storage location. Instead, an output parameter represents the same storagelocation as the variable given as the argument in the method invocation.When a formal parameter is an output parameter, the corresponding argument in a method invocation mustconsist of the keyword out followed by a variable-reference (§12.3.3) of the same type as the formal parameter.A variable need not be definitely assigned before it can be passed as an output parameter, but following aninvocation where a variable was passed as an output parameter, the variable is considered definitely assigned.Within a method, just like a local variable, an output parameter is initially considered unassigned and must bedefinitely assigned before its value is used.Every output parameter of a method must be definitely assigned before the method returns.Output parameters are typically used in methods that produce multiple return values. [Example: For example:using System;class Test{static void SplitPath(string path, out string dir, out string name) {int i = path.Length;while (i > 0) {char ch = path[i – 1];if (ch == '\\' || ch == '/' || ch == ':') break;i--;}dir = path.Substring(0, i);name = path.Substring(i);}static void Main() {string dir, name;SplitPath("c:\\Windows\\System\\hello.txt", out dir, out name);Console.WriteLine(dir);Console.WriteLine(name);}}The example produces the output:c:\Windows\System\hello.txtNote that the dir and name variables can be unassigned before they are passed to SplitPath, and that they areconsidered definitely assigned following the call. end example]230

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

Saved successfully!

Ooh no, something went wrong!