日期:2011-12-01  浏览次数:20487 次

本节课将介绍C# 语言的表达式,类型和变量。本节课要达到如下几个目的:
1.了解什么是"变量"

2.学习C#的简单类型

3.对C#表达式有个初步的了解

4.了解什么是String类型

5.学习如何使用数组

"变量"仅仅是数据的存储位置。你可以把数据存放到其中,或者从中取出来作为C#表达式的一部分。变量中所存放的数据的含义是通过类型来控制的。

C#是个强类型(???)的语言。这样,一切对变量的操作都是针对该变量的类型而进行的。为了保证变量中所存放数据的合法性和一致性,对不同类型的变量进行操作有相应的规则。

C#语言的简单类型包含布尔类型和三种数值类型:整型,浮点型和小数。

1.清单1-1 显示布尔值:Boolean.cs

using System;
class Booleans {
public static void Main() {
bool content = true;
bool noContent = false;
Console.WriteLine("It is {0} that C# Station provides C# programming language content.", content);
Console.WriteLine("The statement above is not {0}.", noContent);
}
}

说明

1.在清单1-1中,布尔值作为句子的一部分输出到控制台中。"bool"类型的取值要么为真,要么为假。程序运行结果如下:

>It is True that C# Station provides C# programming language content.
>The statement above is not False.

2.下列表格显示了各种整数类型,所占字节大小和所能表示的数的范围。

类型 位 范围
sbyte 8 -128 to 127
byte 8 0 to 255
short 16 -32768 to 32767
ushort 16 0 to 65535
int 32 -2147483648 to 2147483647
uint 32 0 to 4294967295
long 64 -9223372036854775808 to 9223372036854775807
ulong 64 0 to 18446744073709551615
char 16 0 to 65535

在对整数进行计算时,除了字符类型之外,上述这些类型都是适合的。字符类型代表一个Unicode字符。正如在上表中可以看到的,你可以从中选择适合你需要的类型。

3.下列表格显示了单精度类型,双精度类型和小数类型的数据,它们所占的字节,精度和所能表示的数的范围。

类型 位 精度 范围
float 32 7 digits 1.5 x 10-45 to 3.4 x 1038
double 64 15-16 digits 5.0 x 10-324 to 1.7 x 10308
decimal 128 28-29 decimal places 1.0 x 10-28 to 7.9 x 1028

当你需要进行涉及到分数的操作时,就需要用到实型,然而,对于金融财经方面的数据的计算,小数类型也许是你最好的选择。

4.表达式计算之后可以得出结果。这些表达式把变量和运算符一同放到语句中。下表列出了C#允许的运算符,优先级和结合性。

分类 运算符 结合性
初级 (x) x.y f(x) a[x] x++ x-- new typeof sizeof checked unchecked 左
单目 + - ! ~ ++x --x (T)x 左
乘法等 * / % 左
加法等 + - 左
移位 << >> 左
关系 < > <= >= is 左
相等 == != 右
逻辑与 & 左
逻辑异或 ^ 左
逻辑或 | 左
条件与 && 左
条件或 || 左
条件 ?: 右
赋值等 = *= /= %= += -= <<= >>= &= ^= |= 右

左结合意味着运算符是从左到右进行运算的。右结合意味着所有的运算是从右到左进行的,如赋值运算符,要等到其右边的计算出来之后,才把结果放到左边的变量中。

2.清单 1-2. 单目运算符: Unary.cs

using System;
class Unary {
public static void Main() {
int unary = 0;
int preIncrement;
int preDecrement;
int postIncrement;
int postDecrement;
int positive;
int negative;
sbyte bitNot;
bool logNot;
preIncrement = ++unary;
Console.WriteLine("Pre-Increment: {0}", preIncrement);
preDecrement = --unary;
Console.WriteLine("Pre-Decrement: {0}", preDecrement);
postDecrement = unary--;
Console.WriteLine("Post-Decrement: {0}", postDecrement);
postIncrement = unary++;
Console.WriteLine("Post-Increment: {0}", postIncrement);
Console.WriteLine("Final Value of Unary: {0}", unary);
positive = -postIncrement;
Console.WriteLine("Positive: {0}", positive);
negative = +postIncrement;
Console.WriteLine("Negative: {0}", negative);
bitNot = 0;
bitNot = (sbyte)(~bitNot);
Console.WriteLine("Bitwise Not: {0}", bitNot);
logNot = false;
logNot = !logNot;
Console.WriteLine("Logical Not: {0}", logNot);
}
}

说明

1.当计算表达式的时候,在后置增一和后置减一运算符进行运算时,先返回其值,再进行增一或者减一运算。当使用前置加号和减号运算符进行运算时,是先进行增一或者减一的运算,然后再返回其结果值。

2.在清单1-2中, 变量unary初始化为0,进行++x 运算时,"unary"的值加1,再把其值1赋给"preIncrement"变量。在进行--x运算时,先把"unary"的值减到0, 再把值0赋给"preDecrement"变量。

3.进行x-运算时,先把"unary"的值0赋给"postDecrement" 变量,之后再把"unary"减到-1。进行x++运算时,先把"unary"的值-1赋给"postIncrement"变量,之后再对"unary"加1,使得"unary"变量现在的值为0。

4.变量"bitNot"初始值为0,进行按位取反运算,本例中,数0表示为二进制"00000000",按位取反之后变为-