C#2.0实例程序STEP BY STEP--实例二:数据类型
与其他.NET语言一样,C#支持Common Type Sysem(CTS),其中的数据类型集合不仅包含我们熟悉的基本类型,例如int,char和float等,还包括比较复杂的类型,例如内部的string类型和表示货币值的decimal类型。而且,每种数据类型不仅是一种基本数据类型,还是一个真正的类,其方法可以用与格式化、系列化和类型转换等。
下面看第一个例子:值类型--整数类型
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

class DataTypes


{
public static void Main(string[] args)

{
bool gobool=true;
sbyte sbyteNu; //声明一个sbyte变量,CTS类型为System.sbyte
short shortNu; //声明一个short变量,CTS类型为System.Int16
int intNu; //声明一个int变量,CTS类型为System.Int32
long longNu; //声明一个long变量,CTS类型为System.Int64
byte byteNu; //声明一个byte变量,CTS类型为System.Byte
ushort ushortNu; //声明一个ushort变量,CTS类型为System.Uint16
uint uintNu; //声明一个uint变量,CTS类型为System.Uint32
ulong ulongNu=0; //声明一个ulong变量,CTS类型为System.Uint64
bool testbool=false;

sbyte类型#region sbyte类型
Console.WriteLine("请输入一个sbyte类型的整数.sbyte是有8位符号的整数,范围为-128到127之间:");
while(gobool)

{
try

{
sbyteNu=sbyte.Parse(Console.ReadLine());
Console.WriteLine("{0}这是一个sbyte类型的整数。",sbyteNu);
gobool=false;
}
catch(Exception caught)

{
Console.WriteLine(caught.Message+" 请您重新输入(sbyte是有8位符号的整数,范围为-128到127之间):");
}
}
Console.WriteLine("\n");
#endregion

short类型#region short类型
Console.WriteLine("请输入一个Short类型的整数.\nshort是有16位符号的整数,范围为-32768到32767之间:");
while(gobool==false)

{
try

{
shortNu=short.Parse(Console.ReadLine());
Console.WriteLine("{0}这是一个short类型的整数。",shortNu);
gobool=true;
}
catch(Exception caught)

{
Console.WriteLine(caught.Message+"\n请您重新输入(short是有16位符号的整数,范围为-32768到32767之间):");
}
}
#endregion

Int类型#region Int类型
Console.WriteLine();
Console.WriteLine("请输入一个int类型的整数.\nint是有32位符号的整数,范围为-2147483648到2147483647之间:");
while(gobool==true)

{
try

{
intNu=int.Parse(Console.ReadLine());
Console.WriteLine("{0}这是一个int类型的整数。",intNu);
gobool=false;
}
catch(Exception caught)

{
Console.WriteLine(caught.Message+"\n请您重新输入(int是有32位符号的整数,范围为-2147483648到2147483647之间):");
}
}
Console.WriteLine();
#endregion

long类型#region long类型
Console.WriteLine("请输入一个long类型的整数.\nlong是有64位符号的整数,范围为-9223372036854775808到9223372036854775807之间:");
while(gobool==false)

{
try

{
longNu=long.Parse(Console.ReadLine());
Console.WriteLine("{0}这是一个long类型的整数。",longNu);
gobool=true;
}
catch(Exception caught)

{
Console.WriteLine(caught.Message+"\n请您重新输入(long是有64位符号的整数,范围为-9223372036854775808到9223372036854775807之间):");
}
}
Console.WriteLine();
#endregion

byte类型#region byte类型
Console.WriteLine("byte类型的最小值是{0},最大值是{1}!",byte.MinValue,byte.MaxValue);
byteNu=127;
sbyteNu=127;
testbool=(byteNu==sbyteNu);
Console.WriteLine("判断byteNu和sbyteNu是否相等?"+testbool);
Console.WriteLine();
#endregion

ushort类型#region ushort类型
Console.WriteLine("ushort类型的最小值是{0},最大值是{1}!",ushort.MinValue,ushort.MaxValue);
ushortNu=65535;
shortNu=(short)ushortNu;
testbool=(ushortNu==shortNu);
Console.WriteLine("shortNu="+shortNu);
Console.WriteLine("ushortNu是否能与shortNu相等?"+testbool);
Console.WriteLine();
#endregion

uint类型#region uint类型
Console.WriteLine("uint类型的最小值是{0},最大值是{1}!",uint.MinValue,uint.MaxValue);
intNu=-1;
uintNu=(uint)intNu;
testbool=(intNu==uintNu);
Console.WriteLine("uintNu="+uintNu);
Console.WriteLine("uintNu是否能与intNu相等?"+testbool);
Console.WriteLine();
#endregion

ulong类型#region ulong类型
Console.WriteLine("ulong类型的最小值是{0},最大值是{1}!",ulong.MinValue,ulong.MaxValue);
ulongNu=ulong.MaxValue;
longNu=(long)ulongNu;
Console.WriteLine("ulongNu={0}\tlongNu={1}",ulongNu,longNu);
Console.WriteLine();
#endregion
}
} 第二个例子:理解值类型和值类型的转换,还有什么是枚举和结构类型。
using System;
class CValue
{
static void Main(string[] args)
{
//值类型定义及初始化
System.Int16 aShort = new System.Int16 ();//System.Int16 等同与 short关键字
byte aByte = new Byte ();
decimal aDecimal = 100.5m;
char aChar = 'y';
Console.WriteLine ("值类型的初始化值:{0} ,{1} ,{2} ,{3}",aShort,aByte,aDecimal,aChar);
//值类型的赋值与操作
float aFloat = 123.123F;
bool aBool = ((aFloat > 121) && (aFloat < 125));
if (aBool) {
int aInteger;
aInteger = (int) aFloat;//显示转换
double aDouble;
aDouble = aFloat; //隐式转换
Console.WriteLine ("类型转换结果:{0} ,{1}",aInteger,aDouble);
}
//枚举类型的使用
long x = (long)Data.Min ;
long y = (long)Data.Max ;
Console.WriteLine ("枚举类型的值:{0} ,{1}",x,y);
//结构的使用
MyPoint aPoint;
aPoint.x = 10;
aPoint.y = 100;
Console.WriteLine ("结构类型的值:{0} ,{1}",aPoint.x ,aPoint.y );
}
//枚举类型的定义
enum Data : long {
Min = 255L, Mid = 1024L, Max = 32768L
};
//结构的定义
public struct MyPoint
{
public int x, y;
public MyPoint( int x, int y)
{
this.x = x;
this.y = y;
}
}
}
Step by step!Create Sunshine!