C # application Scopes

Source: Internet
Author: User

Some examples of creating variables inside a method are shown earlier. A variable exists from the statement that defines it, and subsequent statements within the same method can use that variable. In other words, a variable can only be used after it has been created. Once the method has been executed, the variables will disappear completely.

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

Defining local scopes

Defining the starting and closing braces for a method body a scope is established. Any variable declared in the method body has the scope of that method, and as soon as the method ends, they disappear and can only be accessed by code that executes inside that method. These variables are called 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 fail to compile because the Anothermethod method attempts to use an out-of-bounds myvar variable. The variable can only be used by the statements in the FirstMethod method.

Defining class Scopes

Defining the starting and closing braces for a class principal also establishes a scope. Any variable declared in the body of a class (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;
}

The variable myfield is defined inside the class and externally to the FirstMethod and Anothermethod methods. Therefore, MyField has the scope of the class, which can be used by all methods in the class.

There is another point to note 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 take care of 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, an overloaded identifier belongs to a program bug that is caught at compile time and an error is encountered. For example, suppose that you declare two local variables of the same name in the same method, you get a compile-time error. Similarly, if you declare two fields of 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 appears to be trivial because everything is reported as a compile-time error. However, you can really overload identifiers in one way, and this overload is not only useful, but also important.

Take the WriteLine method in the console class as an example, previously using 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 displayed, listing 19 different versions! Each version of the WriteLine method gets a different set of parameters. One implementation does not get any arguments, just outputs a blank line, another implementation acquires a bool parameter and outputs its value as a string (true or false), and an implementation gets a small value and outputs it as a string; When the program compiles, the compiler checks the type of the arguments passed, and then calls 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 very useful technique if you need to perform the same action 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 with a different number of parameters or different parameter types. With this feature, you can provide a comma-separated list of arguments when invoking a method, and the compiler will select one of the overloaded versions of the argument 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 only two methods with the same name that differ from the return type (the compiler is smarter, but not as smart as that level).

C # Application scope

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.