变量类型
资料介绍:
附件1:外文资料翻译译文
变量类型
对于编程而言,变量是基本要素。变量允许在内存中存储信息。存储信息后,就可以执行数学函数,计算新的日期,处理文本,计算文本的长度,以及执行其他各种函数。本书讨论在C#中使用变量的技巧。如果读者使用VB.NET或其他语言,语法就会不同,但理论是非常类似的。
变量是内存中的一个空间,程序员会给它指定一个名称和数据类型。内存中的这些空间可以用于存储要在程序中使用的信息。可以把变量看作盒子或信息仓库。不同的数据类型需要不同尺寸、不同形状的盒子-- 即内存量不同。在将信息放入变量之前,变量是空的(在代码运行时留出了内存空间)。将信息放入变量后,就可以查看变量中的信息、从中获取信息、或用新数据替换原信息。变量有四个部分:名称、内存空间、数据类型和包含的值。
C#是一种强类型化的语言,这意味着任何变量都必须有一个数据类型,如String、Integer、Date。这将告诉C#如何处理数据,例如,日期解释成相应的日期,而不是一个长的除式,如5/10/2003。
值类型变量的声明:
在编程时,使用变量前最好明确创建或声明它们。在C#中,最简单的值类型变量声明是在数据类型的后面加上变量名。在下面的代码中,int表示需要一个整数类型的变量。本章的后面将详细介绍不同的数据类型。 [版权所有:http://DOC163.com]
int NumberOfStates;
这个语句执行了三个任务。第一,建立变量的名称。第二,指定数据类型。第三,在内存中分配一块空间。在变量赋值之前,内存中什么也没有包含(请记住,0是一个值,因此变量被赋值之前内存中也不包含0,甚至连空格也不包含)。使用IsNothing(myVariable==null)条件,可以检查字符串变量是否包含值,如果变量为空,该条件就返回Ture,如果变量有值,它就返回False。
在给变量命名时,注意必须遵循下述三个原则:
● 所有的变量名都必须以字母开头(不是数字或符号)。
附件2:外文原文(复印件)
Type Variables
Variables are fundamental to programming - they let you store information in memory. Once the information is stored, you can perform mathematical functions, calculate new dates, manipulate text, count the length of sentences, and perform many such functions. This book discusses the techniques of using variables in C#. The syntax would be different if you work in VB.NET or another language, but the theory is very similar.
A variable is a space in memory that is allocated a name and given a datatype by the programmer. These spaces in memory can be used to store pieces of information that will be used in the program. Think of variables as you might think of boxes or repositories for information. Different datatypes requiredifferent sizes and shapes of boxes - with different amounts of memory. Any variable is empty until you put information into it (although the memory space is reserved while the code runs). You can then view the information inside the variable, get the information out, or replace the information with new data. Variables have four parts: a name, a space in memory, a datatype, and the value that they hold. [资料来源:www.doc163.com]
C# is a strongly typed language, which means that every variable has a datatype associated with it, suchas string, integer, or date. Typing tells C# how to deal with the data so that, for example, dates can be seen as proper dates and not a long 'division' operation such as 5/10/2003.
Declaring Value Type Variables
Good programming practice requires that you explicitly create or declare variables before you use them.In C#, the simplest type of variable declaration for value types is made with the datatype followed by the name of the variable. In the following case int implies that we want a variable of the datatype integer. We will talk at length about the different datatypes later in this chapter.