温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

(C#基本语法)3.简单类型

发布时间:2020-06-13 11:17:54 来源:网络 阅读:497 作者:tallbig 栏目:编程语言
简单类型属于C# 语言的值类型,对应于C++语言的基本类型,包括字符、布尔类型、以及整数和实数等数值类型。与C++/CLI相似,C# 中的基本类型都与.NET框架的System命名空间中的对应类型等同,是它们的别名。参见下表:
C# 的简单类型
C#类型
C++/CLI类型
.NET框架类型
值类型
字节/位数
范围和精度
bool
bool
System.Boolean
真或假
-/1
truefalse
char
wchar_t
System.Char
字符
2/16
所有UTF-16编码(0~0xFFFF
sbyte
[signed] char
System.SByte
整数
1/8
-128 ~ 127
byte
unsigned char
System.Byte
1/8
0 ~ 255
short
[signed] short
System.Int16
2/16
-32 768 ~ 32 767
ushort
unsigned short
System.UInt16
2/16
0 ~ 65 535
int
[signed] int/long
System.Int32
4/32
-2 147 483 648 ~ 2 147 483 647
uint
unsigned int/long
System.UInt32
4/32
0 ~ 4 294 967 295
long
[signed] long long
System.Int64
8/64
-9 223 372 036 854 775 808
~ 9 223 372 036 854 775 807
ulong
unsigned long long
System.UInt64
8/64
0 ~ 18 446 744 073 709 551 615
float
float
System.Single
浮点数
4/32
±1.5×10-45 ~ ±3.4×1038
double
double
System.Double
8/64
±5.0×10-324 ~ ±1.7×10308
decimal
Decimal
System.Decimal
高精度十进制小数
16/128
±1.0×10-28 ~ ±7.9×1028
其中的sbytebyteshortushortintuintlongulongchar9种类型被为整数类型(integral types)。
可见,C# 的简单类型的名称,比C++的更简洁明了。如signed被省略;unsigned简写成了u,从而unsigned shortunsigned intunsigned long long分别被改成了ushort uintulongchar对应于C++wchar_tsbyte部分对应于C++char,但是sbyte只表示单字节的有符号整数,不再表示单字节的普通字符,因为在C# 不支持单字节字符。因此,在C# 中,不再需要C++中的L"……"运算符来进行普通字符串常量向宽字符串的转换。
C++非常不同等一点是,C# 中所有×××类型(如intlong)的字节数都是固定的,不再像C/C++那样依赖于CPU的字长。
还有几点与C++不同,但是与C++/CLI相同的是:
l 可以用(从System.Object继承的)GetType()方法来获得指定变量或对象的类型名称。简单类型返回的是.NET的类型名,对象则返回类或结构的名称。例如:
(C#基本语法)3.简单类型int i = 1;
(C#基本语法)3.简单类型MyClass obj;
(C#基本语法)3.简单类型Console::WriteLine(L"The type of variable i is {0} and object obj is {1}.", i.GetType(), obj->GetType());
的输出为:
The type of variable i is System.Int32 and the object obj is MyClass.
l  具有.NET的高精度十进制小数类型System.Decimal的对应类型decimal。在C# 中可以用mM后缀,将一个实数常量指定为decimal类型。没有后缀的实数会被视为double类型,直接赋值给decimal变量会导致编译错误。例如:
(C#基本语法)3.简单类型decimal money = 1234.5m; // 正确
(C#基本语法)3.简单类型decimal d = 1234.5; // 编译错误
注意:在C++/CLI中没有类似的后缀指示符,需要用Decimal的构造函数。例如:
(C#基本语法)3.简单类型// C++/CLI例
(C#基本语法)3.简单类型decimal d = 1234.5; // 编译错误
(C#基本语法)3.简单类型Decimal money = Decimal(1234.5); // 正确
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI