c#教程第二课:表达式,类型和变量
文章来源:互联网
本节课将介绍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); } } |
说明 |
>It is True that C# Station provides C# programming language content. >The statement above is not False. |
类型 | 位 | 范围 |
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 |
类型 | 位 | 精度 | 范围 |
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 |
分类 | 运算符 | 结合性 |
初级 | (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); } } |
说明 |
>Pre-Increment: 1 >Pre-Decrement 0 >Post-Decrement: 0 >Post-Increment -1 >Final Value of Unary: 0 >Positive: 1 >Netative: -1 >Bitwise Not: -1 >Logical Not: True |
3.清单 1-3. 二元运算符 Binary.cs |
using System; class Binary { public static void Main() { int x, y, result; float floatResult; x = 7; y = 5; result = x+y; Console.WriteLine("x+y: {0}", result); result = x-y; Console.WriteLine("x-y: {0}", result); result = x*y; Console.WriteLine("x*y: {0}", result); result = x/y; Console.WriteLine("x/y: {0}", result); floatResult = (float)x/(float)y; Console.WriteLine("x/y: {0}", floatResult); result = x%y; Console.WriteLine("x%y: {0}", result); result += x; Console.WriteLine("result+=x: {0}", result); } } |
说明 |
4.清单 1-4. Array Operations: Array.cs |
using System; class Array { public static void Main() { int[] myInts = { 5, 10, 15 }; bool[][] myBools = new bool[2][]; myBools[0] = new bool[2]; myBools[1] = new bool[1]; double[,] myDoubles = new double[2, 2]; string[] myStrings = new string[3]; Console.WriteLine("myInts[0]: {0}, myInts[1]: {1}, myInts[2]: {2}", myInts[0], myInts[1], myInts[2]); myBools[0][0] = true; myBools[0][1] = false; myBools[1][0] = true; Console.WriteLine("myBools[0][0]: {0}, myBools[1][0]: {1}", myBools[0][0], myBools[1][0]); myDoubles[0, 0] = 3.147; myDoubles[0, 1] = 7.157; myDoubles[1, 1] = 2.117; myDoubles[1, 0] = 56.00138917; Console.WriteLine("myDoubles[0, 0]: {0}, myDoubles[1, 0]: {1}", myDoubles[0, 0], myDoubles[1, 0]); myStrings[0] = "Joe"; myStrings[1] = "Matt"; myStrings[2] = "Robert"; Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]); } } |
说明 |
文章评论(0条评论)
登录后参与讨论