« Home « Kết quả tìm kiếm

Document Class for Applying Scope


Tóm tắt Xem thử

- You have seen in some of the examples that you can create a variable inside a method..
- subsequent statements in the same method can then use the variable.
- To put it another way, the scope of a variable is simply the region of the program in which that variable is usable.
- The scope of an identifier (of a variable or method) is linked to the location of the declaration that introduces the identifier into the program, as you'll now learn..
- Unlike local variables, you can use fields to share information between methods.
- The variable myField is defined within the class, but outside of the methods firstMethod and anotherMethod.
- In a method, you must declare a variable before you can use it.
- A method can use a field before the statement that defines the field—the compiler sorts out the details for you!.
- If two identifiers have the same name and are declared in the same scope, they are said to be overloaded.
- 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.
- 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.
- 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.
- At compile time, the compiler looks at the types of the arguments you are passing in and then calls the version of the method that has a matching set of parameters.
- Overloading is primarily useful when you need to perform the same operation on.
- You can overload a method when the different implementations have different sets of parameters.
- that is, when they have the same name but a different.
- number of parameters, or when the types of the parameters differ.
- This capability is allowed so that, when you call a method, you can supply a comma-separated list of arguments, and the number and type of the arguments is used by the compiler to select one of the overloaded methods.
- However, note that although you can overload the parameters of a method, you can't overload the return type of a method.
- In other words, you can't declare two methods with the same name that differ only in their return type.

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt