C# int uint long ulong byte sbyte float double decimal 范围,及类型!

时间:2023-11-29 14:51:50
static void Main(string[] args)
{
Console.WriteLine(" byte {0,7:g}{1,32:g}{2,32:g}",typeof(byte).Name, byte.MinValue, byte.MaxValue);
Console.WriteLine(" sbyte {0,7:g}{1,32:g}{2,32:g}",typeof(sbyte).Name, sbyte.MinValue, sbyte.MaxValue);
Console.WriteLine(" short {0,7:g}{1,32:g}{2,32:g}", typeof(short).Name, short.MinValue, short.MaxValue);
Console.WriteLine(" ushort {0,7:g}{1,32:g}{2,32:g}", typeof(ushort).Name, ushort.MinValue, ushort.MaxValue);
Console.WriteLine(" int {0,7:g}{1,32:g}{2,32:g}",typeof(int).Name, int.MinValue, int.MaxValue);
Console.WriteLine(" uint {0,7:g}{1,32:g}{2,32:g}",typeof(uint).Name, uint.MinValue, uint.MaxValue);
Console.WriteLine(" long {0,7:g}{1,32:g}{2,32:g}",typeof(long).Name, long.MinValue, long.MaxValue);
Console.WriteLine(" ulong {0,7:g}{1,32:g}{2,32:g}",typeof(ulong).Name, ulong.MinValue, ulong.MaxValue);
Console.WriteLine(" float {0,7:g}{1,32:g}{2,32:g}", typeof(float).Name, float.MinValue, float.MaxValue);
Console.WriteLine(" double {0,7:g}{1,32:g}{2,32:g}", typeof(double).Name, double.MinValue, double.MaxValue);
Console.WriteLine("decimal {0,7:g}{1,32:g}{2,32:g}",typeof(decimal).Name, decimal.MinValue, decimal.MaxValue);
}

输出:

   byte     Byte
sbyte SByte -
short Int16 -
ushort UInt16
int Int32 -
uint UInt32
long Int64 -
ulong UInt64
float Single -3.402823e+38 3.402823e+38
double Double -1.79769313486232e+308 1.79769313486232e+308
decimal Decimal -

整数型类型

可以看出,关键字与其类型命名显得有点乱!

有符号[关键字(对应.net类型)] 无符号[关键字(对应.net类型)-后缀] 长度
sbyte(SByte) byte(Byte)  8位 整数     1字节
short(Int16) ushort(UInt16) 16位 整数     2字节
int(Int32) uint(UInt32)-U|u 32位 整数     4字节
long (Int64)-L|l ulong(Int64)-UL|ul 64位 整数    8字节  

后缀:

uint long ulong 可以被推断出来,可以从int 隐式转换

D 所有小数点,都可以推断成Double

F、M比较有用指定float decimal  如:float p= 3.14;   编译器不通过默认的3.14位double类型

浮点型

C# 类型/关键字 大致范围 精度 .NET 类型 后缀
float(单精度32位数字) 4字节 ±1.5 x 10−45 至 ±3.4 x 1038 大约 6-9 位数字 System.Single f F
double(双精度64位数字)8字节 ±5.0 × 10−324 到 ±1.7 × 10308 大约 15-17 位数字 System.Double d D
decimal ±1.0 x 10-28 至 ±7.9228 x 1028 28-29 位 System.Decimal m M

关键字即是.net类型的别名以下是相同的!

double a = 12.3;
System.Double b = 12.3;