Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'c# bible 2002 phần 2', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Note Where you declare your variable is up to you but keep this in mind If you declare it in a function as shown in the AnotherVariable variable in the preceding example only the code in that function can work with the variable. If you declare it within the class as with the MyIntegerVariable variable also shown in the preceding example any code in that class can work with the variable. If you take the code in the example and add another function to the class the code in that new function can work with the MyIntegerVariable variable but cannot work with the AnotherVariable variable. If that new function tries to access the AnotherVariable variable declared in the Main function you get the following error message from the C compiler error CS0103 The name AnotherVariable does not exist in the class or namespace MyClass Using Default Values for Variables In other programming languages it is legal to work with a variable without first giving it a value. This loophole is a source of bugs as the following code demonstrates class MyClass static void Main int MyVariable What is the value of MyVariable here What is the value of MyVariable when Main executes Its value is unknown because the code does not assign a value to the variable. The designers of C were aware of the errors that can pop up as a result of using variables that have not been explicitly given a value. The C compiler looks for conditions like this and issues an error message. If the MyVariable variable shown in the preceding code is referenced in Main without a value assignment the C compiler presents the following error message error CS0165 Use of unassigned local variable MyVariable C makes a distinction between assigned and unassigned variables. Assigned variables are given a value at some point in the code and unassigned variables are not given a value in the code. Working with unassigned variables is forbidden in C because their values are not known and using the variables can lead to errors in your .