03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

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.

Parameter Declarations ❘ 85<br />

Because the DoubleIt method doesn’t require that its parameter be initialized, the DoubleTest<br />

method does not initialize its value variable. The DoubleIt method sets the parameter’s value and<br />

displays it. Because the parameter is declared out, the value returns to the calling code, so in the<br />

DoubleTest method value becomes 50.<br />

The following text shows the result.<br />

DoubleIt: 50<br />

DoubleTest: 50<br />

Unusual Circumstances and Exceptions<br />

Even if you know how to pass arguments by value, by reference, and for output, there are situations<br />

that can be confusing. Recall that some variables are value types and others are reference types.<br />

When you pass a value type variable by value, the method receives a copy of the value and all is as<br />

you would expect.<br />

If you pass a reference variable by value, the method receives a copy of the reference, not a copy of<br />

the reference’s data. That means the method cannot change the reference (because it was passed by<br />

value) but it can change the data associated with the reference.<br />

For example, consider the following method.<br />

private void SetCity(Person person)<br />

{<br />

person.City = "Bugsville";<br />

person.Zip = 12345;<br />

}<br />

This method takes a Person parameter passed by value, changes the object’s City and Zip values,<br />

and ends.<br />

If Person is a class, it is a reference type so the method receives a copy of the reference. That means<br />

the parameter refers to the same object as the argument in the calling code, so any changes to the<br />

object’s properties are also changes to the original object.<br />

In the calling code, the Person object hasn’t changed but its City and Zip values have.<br />

Now consider the following version of the SetCity method.<br />

private void SetCity(Person person)<br />

{<br />

person = new Person() { City = "Programmeria", Zip = 54321 };<br />

}<br />

This version sets the person parameter to a new Person object. Because the parameter was passed<br />

by reference, however, the calling code’s Person object isn’t changed. You can make the calling code<br />

receive the new value by declaring the parameter ref or out.<br />

A similar phenomenon occurs when you pass arrays into a method. If the array is passed by value,<br />

the method can change its values, but if it sets the parameter equal to a new array, the change does<br />

not return to the calling code. If the array parameter is declared ref or out, the method can set the<br />

parameter equal to a new array, and the change returns to the calling code.<br />

Chapter 6 has more to say about methods.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!