网站策划与建设阶段的推广方法,云南网络推广,代运营公司的套路,成都app开发制作公司排名本文由 简悦 SimpRead 转码#xff0c; 原文地址 mp.weixin.qq.com 本期来讲讲 unity 的脚本语言 —C##xff0c;C# 的简单数据类型及范围和强制类型转化的方法。这可是 unity 游戏开发必备技能。
1. 简单数据类型 各个类型的范围#xff1a; byte - System.Byte (字节… 本文由 简悦 SimpRead 转码 原文地址 mp.weixin.qq.com 本期来讲讲 unity 的脚本语言 —C#C# 的简单数据类型及范围和强制类型转化的方法。这可是 unity 游戏开发必备技能。
1. 简单数据类型 各个类型的范围 byte - System.Byte (字节型占 1 字节表示 8 位正整数范围 0 ~ 255) ushort - System.UInt16 (无符号短整型占 2 字节表示 16 位无符号整数范围 0 ~ 65,535) uint - System.UInt32 (无符号整型占 4 字节表示 32 位无符号整数范围 0 ~ 4,294,967,295) ulong - System.UInt64 (无符号长整型占 8 字节 表示 64 位无符号整数范围 0 ~ 大约 10 的 20 次方) sbyte - System.SByte (带符号字节型占 1 字节表示 8 位整数范围 -128 ~ 127) short - System.Int16 (短整型占 2 字节表示 16 位整数范围 -32,768 ~ 32,767) int - System.Int32 (整型占 4 字节表示 32 位整数范围 -2,147,483,648 到 2,147,483,647) long - System.Int64 (长整型占 8 字节 表示 64 位有符号整数范围大约 -(10 的 19) 次方 到 10 的 19 次方) float - System.Single (单精度浮点型占 4 个字节范围-3.40282347E38F 到 3.40282347E38F) double - System.Double (双精度浮点型占 8 个字节范围-1.7976931348623157E3081.7976931348623157E308 decimal-System.Decimal(表示十进制数占 16 个字节) bool - System.Boolean (布尔型其值为 true 或者 false) char - System.Char (字符型占有两个字节表示 1 个 Unicode 字符) string - System.String (字符串型表示一系列 Unicode 字符的不可变序列) 2. 强制转换
2.1 括号法
在变量前加上 (类型)
int num;
char cA;
num(int)c;注意范围超出范围会异常正数变负数
bool 和 string 不能和其他类型进行强制转换
2.2 Parse 法
把字符串转化为对应的类型
语法
变量类型. Parse(“字符串”)
int numint.Parse(123);注意字符串必须能够转换成对应类型并且范围要符合否则报错
2.3 Convert 类法
更准确的对各个类型进行转换, 会四舍五入
语法
Convert.To 目标类型 (变量或常量)
int a Convert.ToInt32(12);aConvert.ToInt32(1.5556f);//a变成2会四舍五入
aConvert.ToInt32(true);//a变成1
//以下是其他类型的示例
sbyte sbConvert.ToSByte(1);
short sConvert.ToInt16(1);
int iConvert.ToInt32(1);
long lConvert.ToInt64(1);byte bConvert.ToByte(1);
ushort usConvert.ToUInt16(1);
uint uiConvert.ToUInt32(1);
ulong ulConvert.ToUInt64(1);float fConvert.ToSingle(13.4);
double dCOnvert.ToDouble(13.4);
decimal deConvert.ToDecimal(13.4);bool boConvert.ToBoolean(true);
char cConvert.ToChar(A);string strConvert.ToString(1000);2.4 其他类型转换为 string
语法
变量. ToString();
string str1.ToString();
strA.ToString();
int a20;
stra.ToString();//str为20