Danh mục tài liệu

Using Variables

Số trang: 2      Loại file: pdf      Dung lượng: 18.52 KB      Lượt xem: 3      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Sử dụng biến biến là một vị trí lưu trữ chứa một giá trị. Bạn có thể nghĩ của một biến như là một hộp giữ thông tin tạm thời. Bạn phải cung cấp cho mỗi biến trong một chương trình tên duy nhất. Bạn sử dụng tên của một biến để chỉ giá trị nó giữ.
Nội dung trích xuất từ tài liệu:
Using Variables Using VariablesA variable is a storage location that holds a value. You can think of a variable as a boxholding temporary information. You must give each variable in a program a uniquename. You use a variables name to refer to the value it holds. For example, if you wantto store the value of the cost of an item in a store, you might create a variable simplycalled cost, and store the items cost in this variable. Later on, if you refer to the costvariable, the value retrieved will be the items cost that you put there earlier.Naming VariablesYou should adopt a naming convention for variables that help you avoid confusionconcerning the variables you have defined. The following list contains some generalrecommendations: • Dont use underscores. • Dont create identifiers that differ only by case. For example, do not create one variable named myVariable and another named MyVariable for use at the same time, because it is too easy to get them confused. NOTE Using identifiers that differ only by case can limit the ability to reuse classes in applications developed using other languages that are not case sensitive, such as Visual Basic. • Start the name with a lowercase letter. • In a multiword identifier, start the second and each subsequent word with an uppercase letter. (This is called camelCase notation.) • Dont use Hungarian notation. (Microsoft Visual C++ developers reading this book are probably familiar with Hungarian notation. If you dont know what Hungarian notation is, dont worry about it!) IMPORTANT You should treat the first two recommendations as compulsory because they relate to Common Language Specification (CLS) compliance. If you want to write programs that can interoperate with other languages, such as Microsoft Visual Basic .NET, you need to comply with these recommendations.For example, score, footballTeam, _score, and FootballTeam are all valid variable names,but only the first two are recommended.Declaring VariablesRemember that variables are like boxes in memory that can hold a value. C# has manydifferent types of values that it can store and process—integers, floating-point numbers,and strings of characters, to name three. When you declare a variable, you must specifywhat type of data it will hold.NOTEMicrosoft Visual Basic programmers should note that C# does not allow implicitdeclarations. You must explicitly declare all variables before you can use them if youwant your code to compile.You declare the type and name of a variable in a declaration statement. For example, thefollowing statement declares that the variable named age holds int (integer) values. Asalways, the statement must be terminated with a semi-colon.int age;The variable type int is the name of one of the primitive C# types—integer which is awhole number. (Youll learn about several primitive data types later in this chapter.) Afteryouve declared your variable, you can assign it a value. The following statement assignsage the value 42. Again, youll see that the semicolon is required.age = 42;The equal sign (=) is the assignment operator, which assigns the value on its right to thevariable on its left. After this assignment, the age variable can be used in your code torefer to the value it holds. The next statement writes the value of the age variable, 42, tothe console:Console.WriteLine(age);TIPIf you leave the mouse pointer over a variable in the Visual Studio 2005 Code and TextEditor window, a ToolTip appears telling you the type of the variable.

Tài liệu được xem nhiều:

Tài liệu có liên quan: