0
点赞
收藏
分享

微信扫一扫

C#的基础数据类型

C#语言在.NET基元类型的基础上,也编制了一份数据类型。所以,将来我们在开发C#程序时,声明基础数据类型,其实就有两种写法。

它们的对应如下表所示。

.NET数据类型

C#数据类型

说明

范围

System.SByte

sbyte

8 位有符号整数类型

-128 到 127

System.Byte

byte

8 位无符号整数

0 到 255

System.Int16

short

16 位有符号整数类型

-32,768 到 32,767

System.UInt16

ushort

16 位无符号整数类型

0 到 65,535

System.Int32

int

32 位有符号整数类型

-2,147,483,648 到 2,147,483,647

System.UInt32

uint

32 位无符号整数类型

0 到 4,294,967,295

System.Int64

long

64 位有符号整数类型

-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807

System.UInt64

ulong

64 位无符号整数类型

0 到 18,446,744,073,709,551,615

System.Char

char

16 位 Unicode 字符

U +0000 到 U +ffff

System.Single

float

32 位单精度浮点型

-3.4 x 1038 到 + 3.4 x 1038

System.Double

double

64 位双精度浮点型

(+/-)5.0 x 10-324 到 (+/-)1.7 x 10308

System.Boolean

bool

布尔值

True 或 False

System.Decimal

decimal

128 位精确的十进制值,28-29 有效位数

(-7.9 x 1028 到 7.9 x 1028) / 100 到 28

System.String

string

字符串

按MSND文档,String类的Length属性的类型为int。而int的最大值为2147483647。所以string的最大长度为2147483647(2,147,483,647)。

System.Object

object

引用类型

.NET下的基元类型(Primitive Type)一共有14个。从占用字节数的长度来区分:长度(字节数)分别为1、2、4、8的有/无符号的整数;外加两个基于指针宽度(x86=4; x64=8)的整数,计10个。长度(字节数)分别为4和8的单精度和双精度浮点数,计2个。外加布尔类型和字符类型, 计2个。System.String(string)、System.Decimal(decimal)、System.Object(object)并不是基元类型。

  • 整数(10):
  • System.Byte(byte)和System.SByte(sbyte),
  • System.Int16(short)和System.UInt16(ushort),
  • System.Int32(int)和System.UInt32(uint),
  • System.Int64(long)和System.UInt64(ulong),
  • System.IntPtr(nint)/System.UIntPtr(nuint)
  • 浮点(2):System.Single(float),和System.Double(double)
  • 布尔(1):System.Boolean(bool)
  • 字符(1):System.Char(char)

这些种类繁多的数据类型,在CTS(公共类型系统)中又分为引用类型和值类型。下一节,我们将介绍引用类型与值类型的概念与区别。

重庆教主 2023年12月20日

举报

相关推荐

C#基础|数据类型、变量

C# 数据类型

c#数据类型

C#数据类型

C#数据类型转换

C# 数据类型转换

0 条评论