15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Classes ❘ 71<br />

out Parameters<br />

In C-style languages, it is common for functions to be able to output more than one value from a single<br />

routine. This is accomplished using output parameters, by assigning the output values to variables that<br />

have been passed to the method by reference. Often, the starting values of the variables that are passed by<br />

reference are unimportant. Those values will be overwritten by the function, which may never even look at<br />

any previous value.<br />

It would be convenient if you could use the same convention in <strong>C#</strong>. However, <strong>C#</strong> requires that variables<br />

be initialized with a starting value before they are referenced. Although you could initialize your input<br />

variables with meaningless values before passing them into a function that will fill them with real,<br />

meaningful ones, this practice seems at best needless <strong>and</strong> at worst confusing. However, there is a way to<br />

short-circuit the <strong>C#</strong> compiler’s insistence on initial values for input arguments.<br />

You do this with the out keyword. When a method’s input argument is prefixed with out, that method can<br />

be passed a variable that has not been initialized. The variable is passed by reference, so any changes that<br />

the method makes to the variable will persist when control returns from the called method. Again, you also<br />

need to use the out keyword when you call the method, as well as when you define it:<br />

static void SomeFunction(out int i)<br />

{<br />

i = 100;<br />

}<br />

public static int Main()<br />

{<br />

int i; // note how i is declared but not initialized.<br />

SomeFunction(out i);<br />

Console.WriteLine(i);<br />

return 0;<br />

}<br />

Named Arguments<br />

Typically, parameters need to be passed into a method in the same order that they are defined. Named<br />

arguments allow you to pass in parameters in any order. So for the following method:<br />

string FullName(string firstName, string lastName)<br />

{<br />

return firstName + " " + lastName;<br />

}<br />

The following method calls will return the same full name:<br />

FullName("John", "Doe");<br />

FullName(lastName: "Doe", firstName: "John");<br />

If the method has several parameters, you can mix positional <strong>and</strong> named arguments in the same call.<br />

Optional Arguments<br />

Parameters can also be optional. You must supply a default value for parameters that are optional. The<br />

optional parameter(s) must be the last ones defined as well. So the following method declaration would<br />

be incorrect:<br />

void TestMethod(int optionalNumber = 10, int notOptionalNumber)<br />

{<br />

System.Console.Write(optionalNumber + notOptionalNumber);<br />

}<br />

For this method to work, the optionalNumber parameter would have to be defined last.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!