Visual C # 2005 QuickStart Application scope

Source: Internet
Author: User
Tags variables string variable versions
visual| Quick Start has shown some examples of creating variables inside methods. The variable begins to exist from the statement that defines it, and subsequent statements within the same method can use the variable. In other words, a variable can be used only after it has been created. After the method has been executed, the variable disappears completely.

If a variable can be used at a particular point in the program, it means that the variable has the scope of that location. That is, a variable's scope (scope) refers to the area of the program that can use the variable. Scopes are used both as methods and as variables. The scope of an identifier, whether it represents a variable or a method, starts at the point where the identifier is declared.

defining local Scopes

The beginning and closing braces of the method body are defined to establish a scope. Any variable declared in a method body has the scope of that method, and once the method is finished, it disappears and can only be accessed by code that executes inside the method. These variables are called locals (local variable) because they are limited to the method that declares them and cannot be used in the scope of any other method. In other words, you cannot use local variables to share information between different methods. For example:

Class Example
{
void FirstMethod ()
{
int MyVar;
...
}
void Anothermethod ()
{
MyVar = 42; ERROR-Variable out of bounds
...
}
}
The code above will compile unsuccessfully because the Anothermethod method attempts to use a MyVar variable that is out of bounds. The variable can only be used by a statement in the FirstMethod method.

Defining a class scope

Defining the starting and closing braces of a class body also establishes a scope. Any variable declared in the class body (but not in a method) has the scope of that class. In C # terminology, a developer uses the term field to describe a variable defined by a class.    Unlike local variables, you can use fields to share information between different methods. For example:

Class Example
{
void FirstMethod ()
{
MyField = 42; Ok
...
}
void Anothermethod ()
{
MyField = 42; Ok
...
}
int MyField = 0;
}
Variable myfield are defined inside the class and outside of the FirstMethod and Anothermethod methods. Therefore, MyField has a scope for classes that can be used by all methods in the class.

Another point has to be noted in this example. In a method, you must declare it before you use a variable. But with a slightly different field, a method can use that field before the statement that defines a field--in which case the compiler will do everything for you!

Overloaded Methods

If two identifiers have the same name and are declared in the same scope, they can be said to be overloaded (overloaded). Typically, overloaded identifiers are part of a program bug that is caught at compile time and an error occurs. For example, suppose you declare two local variables with the same name in the same method, and you get a compile-time error. Similarly, if you declare two fields with the same name in the same class, or if you declare two identical methods in the same class, you get a compile-time error. This fact seems to be insignificant on the surface, because everything is reported as a compile-time error. However, you do have the ability to overload identifiers in one way, and this overload is not only useful but also important.

As an example of the WriteLine method in the Console class, you have previously used this method to output a string to the screen. However, when you enter WriteLine in the Code and Text Editor window, a "IntelliSense" list is automatically popped up, listing 19 different versions! Each version of the WriteLine method obtains a different set of parameters. An implementation takes no arguments, but outputs a blank row; the other implementation takes a bool parameter and outputs its value in the form of a string (true or false); Another implementation gets a decimal value and prints it as a string; When the program compiles, the compiler checks the type of the passed argument and then invokes a method version that matches the parameter set. Here is an example:

static void Main ()
{
Console.WriteLine ("The answer is");
Console.WriteLine (42);
}
Overloading is a useful technique if you need to perform the same operation for different data types. If different implementations have different sets of parameters, you can consider overloading a method. In other words, each version has the same method name, but has a different number of parameters or different parameter types. With this feature, when you call a method, you can provide a comma-delimited list of arguments, and the compiler will select one of the matching overloaded versions based on the number and type of these arguments. Note, however, that although you can overload the parameters of a method, you cannot overload the return type of the method. That is, you cannot declare a two method with the same name that is different from the return type (the compiler is smarter but not too smart to that extent).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.