15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Classes ❘ 69<br />

}<br />

}<br />

}<br />

public static int GetSquareOf(int x)<br />

{<br />

return x*x;<br />

}<br />

public static double GetPi()<br />

{<br />

return 3.14159;<br />

}<br />

code snippet MathTest.cs<br />

Running the MathTest example produces these results:<br />

Pi is 3.14159<br />

Square of 5 is 25<br />

Value field of math variable contains 30<br />

Square of 30 is 900<br />

As you can see from the code, the MathTest class contains a field that contains a number, as well as a<br />

method to find the square of this number. It also contains two static methods, one to return the value of pi<br />

<strong>and</strong> one to find the square of the number passed in as a parameter.<br />

Some features of this class are not really good examples of <strong>C#</strong> program design. For example, GetPi()<br />

would usually be implemented as a const field, but following good design here would mean using some<br />

concepts that we have not yet introduced.<br />

Passing Parameters to Methods<br />

In general, parameters can be passed into methods by reference or by value. When a variable is passed<br />

by reference, the called method gets the actual variable — so any changes made to the variable inside the<br />

method persist when the method exits. But, when a variable is passed by value, the called method gets<br />

an identical copy of the variable — which means any changes made are lost when the method exits. For<br />

complex data types, passing by reference is more efficient because of the large amount of data that must be<br />

copied when passing by value.<br />

In <strong>C#</strong>, all parameters are passed by value unless you specifically say otherwise. However, you need to be<br />

careful in underst<strong>and</strong>ing the implications of this for reference types. Because reference type variables hold<br />

only a reference to an object, it is this reference that will be copied, not the object itself. Hence, changes<br />

made to the underlying object will persist. Value type variables, in contrast, hold the actual data, so a copy<br />

of the data itself will be passed into the method. An int, for instance, is passed by value to a method, <strong>and</strong><br />

any changes that the method makes to the value of that int do not change the value of the original int<br />

object. Conversely, if an array or any other reference type, such as a class, is passed into a method,<br />

<strong>and</strong> the method uses the reference to change a value in that array, the new value is reflected in the original<br />

array object.<br />

Here is an example, ParameterTest.cs, which demonstrates the following:<br />

using System;<br />

namespace Wrox<br />

{<br />

class ParameterTest<br />

{<br />

static void SomeFunction(int[] ints, int i)<br />

{<br />

ints[0] = 100;<br />

i = 100;<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!