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.

30 ❘ ChaPTer 2 cOre c#<br />

Here’s another example:<br />

public static int Main()<br />

{<br />

int j = 20;<br />

for (int i = 0; i < 10; i++)<br />

{<br />

int j = 30; // Can't do this—j is still in scope<br />

Console.WriteLine(j + i);<br />

}<br />

return 0;<br />

}<br />

code snippet ScopeBad.cs<br />

If you try to compile this, you’ll get an error like the following:<br />

ScopeTest.cs(12,15): error CS0136: A local variable named 'j' cannot be declared in<br />

this scope because it would give a different meaning to 'j', which is already used<br />

in a 'parent or current' scope to denote something else.<br />

This occurs because the variable j, which is defined before the start of the for loop, is still in scope within the<br />

for loop, <strong>and</strong> won’t go out of scope until the Main() method has finished executing. Although the second j<br />

(the illegal one) is in the loop’s scope, that scope is nested within the Main() method’s scope. The compiler has<br />

no way to distinguish between these two variables, so it won’t allow the second one to be declared.<br />

scope Clashes for fields <strong>and</strong> local Variables<br />

In certain circumstances, however, you can distinguish between two identifiers with the same name<br />

(although not the same fully qualified name) <strong>and</strong> the same scope, <strong>and</strong> in this case the compiler allows you to<br />

declare the second variable. The reason is that <strong>C#</strong> makes a fundamental distinction between variables that<br />

are declared at the type level (fields) <strong>and</strong> variables that are declared within methods (local variables).<br />

Consider the following code snippet:<br />

using System;<br />

namespace Wrox<br />

{<br />

class ScopeTest2<br />

{<br />

static int j = 20;<br />

public static void Main()<br />

{<br />

int j = 30;<br />

Console.WriteLine(j);<br />

return;<br />

}<br />

}<br />

}<br />

code snippet ScopeTest2.cs<br />

This code will compile, even though you have two variables named j in scope within the Main() method:<br />

the j that was defined at the class level, <strong>and</strong> doesn’t go out of scope until the class is destroyed (when the<br />

Main() method terminates, <strong>and</strong> the program ends); <strong>and</strong> the j defined in Main(). In this case, the new<br />

variable named j that you declare in the Main() method hides the class-level variable with the same name,<br />

so when you run this code, the number 30 will be displayed.<br />

However, what if you want to refer to the class-level variable You can actually refer to fields of a class<br />

or struct from outside the object, using the syntax object.fieldname. In the previous example, you are<br />

accessing a static field (you look at what this means in the next section) from a static method, so you can’t<br />

use an instance of the class; you just use the name of the class itself:<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!