Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
26
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

56

Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

Applying Scope

In some of the examples, you can see that you can create variables inside a method. These variables come into existence at the point where they are defined, and subsequent statements in the same method can then use these variables; a variable can be used only after it has been created. When the method has finished, these variables disappear.

If a variable can be used at a particular location in a program, the variable is said to be in scope at that location. To put it another way, the scope of a variable is simply the region of

the program in which that variable is usable. Scope applies to methods as well as variables. The scope of an identifier (of a variable or method) is linked to the location of the declaration that introduces the identifier in the program, as you’ll now learn.

Defining Local Scope

The opening and closing braces that form the body of a method define a scope. Any variables you declare inside the body of a method are scoped to that method; they disappear

when the method ends and can be accessed only by code running in that method. These variables are called local variables because they are local to the method in which they are de-

clared; they are not in scope in any other method. This arrangement means that you cannot use local variables to share information between methods. Consider this example:

class Example

{

void firstMethod()

{

int myVar;

...

}

void anotherMethod()

{

myVar = 42; // error – variable not in scope

...

}

}

This code would fail to compile because anotherMethod is trying to use the variable myVar, which is not in scope. The variable myVar is available only to statements in firstMethod and that occur after the line of code that declares myVar.

Defining Class Scope

The opening and closing braces that form the body of a class also create a scope. Any variables you declare inside the body of a class (but not inside a method) are scoped to that

Chapter 3 Writing Methods and Applying Scope

57

class. The proper C# name for the variables defined by a class is a field. In contrast with local variables, you can use fields to share information between methods. Here is an example:

class Example

{

void firstMethod()

{

myField = 42; // ok

...

}

void anotherMethod()

{

myField++; // ok

...

}

int myField = 0;

}

The variable myField is defined in the class but outside the methods firstMethod and anotherMethod. Therefore, myField has class scope and is available for use by all methods

in the class.

There is one other point to notice about this example. In a method, you must declare a variable before you can use it. Fields are a little different. A method can use a field before the statement that defines the field—the compiler sorts out the details for you!

Overloading Methods

If two identifiers have the same name and are declared in the same scope, they are said to be overloaded. Often an overloaded identifier is a bug that gets trapped as a compile-time

error. For example, if you declare two local variables with the same name in the same method, you get a compile-time error. Similarly, if you declare two fields with the same name in the same class or two identical methods in the same class, you also get a compile-time error. This fact may seem hardly worth mentioning, given that everything so far has turned out to be a compile-time error. However, there is a way that you can overload an identifier, and that way is both useful and important.

Consider the WriteLine method of the Console class. You have already used this method for outputting a string to the screen. However, when you type WriteLine in the Code and Text Editor window when writing C# code, you will notice that IntelliSense gives you 19 different options! Each version of the WriteLine method takes a different set of parameters;

one version takes no parameters and simply outputs a blank line, another version takes a bool parameter and outputs a string representation of its value (true or false), yet another implementation takes a decimal parameter and outputs it as a string, and so on. At compile

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]