C#2.0实例程序STEP BY STEP--实例二:数据类型
C#2.0实例程序STEP BY STEP--实例二:数据类型
与其他.NET语言一样,C#支持Common Type Sysem(CTS),其中的数据类型集合不仅包含我们熟悉的基本类型,例如int,char和float等,还包括比较复杂的类型,例如内部的string类型和表示货币值的decimal类型。而且,每种数据类型不仅是一种基本数据类型,还是一个真正的类,其方法可以用与格式化、系列化和类型转换等。
下面看第一个例子:值类型--整数类型
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
}
}
第二个例子:理解值类型和值类型的转换,还有什么是枚举和结构类型。
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!
转载于:https://www.cnblogs.com/CreateSunshine/archive/2007/07/09/810734.html
C#2.0实例程序STEP BY STEP--实例二:数据类型相关推荐
- ActionScript 3.0 Step By Step系列(五):走在面向对象开发的路上,以类为基础去思考编程问题...
面向对象的程序设计(Object-Oriented Programming,简记为OOP)是一种功能非常强大的编程方法,立意于创建软件重用代码,以类为基础去思考编程问题. ActionScript 3 ...
- python写一个通讯录step by step V3.0
python写一个通讯录step by step V3.0 更新功能: 数据库进行数据存入和读取操作 字典配合函数调用实现switch功能 其他:函数.字典.模块调用 注意问题: 1.更优美的格式化输 ...
- ActionScript 3.0 Step By Step系列(四):来自面向对象开发之前的呐喊:“学会写可重用的代码”...
增强代码的可重用能力,从创建可重用的代码开始,可重用的代码则是通过从现有代码中重构加以封装,使其成为功能单一的可复用代码块.这句话笼统点说便是"封装"或"抽象" ...
- Qt新安装之后出现Error while building/deploying (kit: Desktop Qt 5.7.0 GCC 64bit) When executing step Make”
Ubuntu14.04初次安装Qt之后可能出现Error while building/deploying project *** (kit: Desktop Qt 5.7.0 GCC 64bit) ...
- e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (四) Q 反回调...
上一篇文章"e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (三) Sq ...
- python基础复习(30)--string[start:end:step] start默认0 end默认列尾 step默认1
#字符串操作 string[start:end:step] start默认0 end默认列尾 step默认1 s="abcdefghijk" print("s---&qu ...
- 【Step By Step】将Dotnet Core部署到Docker下
一.使用.Net Core构建WebAPI并访问Docker中的Mysql数据库 这个的过程大概与我之前的文章<尝试.Net Core-使用.Net Core + Entity FrameWor ...
- GoFrame Step by Step Demo - P1
GoFrame Step by Step Demo P1 框架说明文档 GFTool 安装 Web框架学习 文章目录 GoFrame Step by Step Demo P1 参考Demo 记录 安装 ...
- 书讯--Microsoft Windows Communication Foundation Step by Step
SOA 观念提出后,经过多年的时间终于开始发酵.微软世界里因 WCF 的出现,简化了实做的基础.SOA 强调如下的重点: l 一切技术遵循公开标准 l 服务定义的边界明确 l 服务自主而不 ...
最新文章
- TSNE——目前最好的降维方法
- [NET] 如何从 Winform 移植到 Webform [自己搞定HTTP协议]
- Nginx+Tomcat集群与负载均衡
- 201521123014 《Java程序设计》第8周学习总结
- linux把root用户删了,linux root用户没法删除文件
- 在vs 2008中使用iis来调试,或者说在iis中集成vs 2008的调试
- sqlserver isnull函数使用
- c语言中有无注释重要吗,求助!有什么问题没考虑到吗? (c语言,大部分注释都写啦)...
- Spring MVC 切面 ResponseBodyAdvice 对返回值增强
- 翻译:PropertyWrapper swift 5 aop特性
- 下载应用 ipa 包,不妨试试这款开源工具
- Java程序员的职业规划
- 计算机主板显卡接口,【我想给电脑加个显卡,但是不知道这个主板的显卡接口是什么类型的?】.请大家帮忙...
- Xposed框架分析
- jsp获取服务器中信息,jsp 获取服务器主机名
- 腾讯游戏安全高级工程师胡和君:定制化对抗——游戏反外挂的安全实践
- 不常用SQL语句整理
- 视频教程-Java工程师必学系列课程之4--《Java Swing》视频课程-Java
- “麦田音乐节·超时空歌会”即将破空 探索元宇宙虚拟演唱会新形式
- RHEL 8.0安装中文输入法
热门文章
- Java项目:校园二手市场系统(java+SSM+mysql+maven+tomcat)
- mysql udf 性能_适当的mysql udf
- Android架构篇-5 CI/CD(持续集成、持续交付、持续部署)
- 微信小程序的数字有部分会自动加粗的解决方法
- 按照文字内容动态设置TableViewCell的高度
- iOS Socket Client 通讯
- 20位程序员关于求职的疑问,以及我给出的参考答案
- C/C++利用三元组实现稀疏矩阵运算
- 新闻发布项目——业务逻辑层(newsTbService)
- 测试驱动开发与行为驱动开发中的测试先行方法