Console.ReadLine();

   return;

  }

  static private void Method1() {

   Console.WriteLine('num in Method1() is {0}', num); //---7---

  }

 }

}

Because the num variable is declared in the class, it is visible (that is, global) to all the methods declared within the class, and you see the following output:

num in Main() is 7

num in Method1() is 7

However, if you declare another variable with the same name (num) within Main() and Method1(), like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace HelloWorld {

 class Program {

  static int num = 7;

  static void Main(string[] args) {

   int num = 5;

   Console.WriteLine('num in Main() is {0}', num); //---5---

   HelloWorld.Program.Method1();

   Console.ReadLine();

   return;

  }

  static private void Method1() {

   int num = 10;

   Console.WriteLine('num in Method1() is {0}', num); //---10---

  }

 }

}

You get a very different output:

num in Main() is 5

num in Method1() is 10

That's because the num variables in Main() and Method1() have effectively hidden the num variable in the Program class. In this case, the num in the Program class is known as the global variable while the num variables in Main and Method1 are known as local variables. The num variable in Main() is only visible within Main(). Likewise, this also applies to the num variable in Method1().

What if you need to access the num declared in the Program class? In that case, you just need to specify its full name:

Console.WriteLine('num in Program is {0}', HelloWorld.Program.num); //---7---

While a local variable can hide the scope of a global variable, you cannot have two variables with the same scope and identical names. The following makes it clear:

static void Main(string[] args) {

 int num = 5;

 Console.WriteLine('num in Main() is {0}', num); //---5---

 int num = 6; //---error: num is already declared---

 return;

}

However, two identically named variables in different scope would be legal, as the following shows:

static void Main(string[] args) {

 for (int i = 0; i < 5; i++) {

  //--- i is visible within this loop only---

  Console.WriteLine(i);

 } //--- i goes out of scope here---

 for (int i = 0; i < 3; i++) {

  //--- i is visible within this loop only---

  Console.WriteLine(i);

 } //--- i goes out of scope here---

 Console.ReadLine();

 return;

}

Here, the variable i appears in two for loops (looping is covered later in this chapter). The scope for each i is restricted to within the loop, so there is no conflict in the scope and this is allowed.

Declaring another variable named i outside the loop or inside it will cause a compilation error as the following example shows:

static void Main(string[] args) {

 int i = 4; //---error---

 for (int i = 0; i < 5; i++) {

  int i = 6; //---error---

  Console.WriteLine(i);

 }

 for (int i = 0; i < 3; i++) {

  Console.WriteLine(i);

 }

 Console.ReadLine();

 return;

}

This code results in an error: 'A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'parent or current' scope to denote something else.'

Constants

To declare a constant in C#, you use the const keyword, like this:

//---declared the PI constant---

const float PI=3.14f;

You cannot change the value of a constant (during runtime) once it has been declared and assigned a value.

As a good programming practice, you should always use constants whenever you use values that do not

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату