『 再看.NET7』数值类型
在C#中,有int16,用short来定义;有int32,用int定义;用int64,用long来定义。在.NET7中,添加了int128,和unint128,位数更大的整型。
var i16 = short.MaxValue;
Console.WriteLine($"Int16(short):{i16}");
var i32 = int.MaxValue;
Console.WriteLine($"Int32(int):{i32}");
var i64 = long.MaxValue;
Console.WriteLine($"Int64(long):{i64}");
var i128 = Int128.MaxValue;
Console.WriteLine($"Int128(nint):{i128}");
运行结果如下:
这是四种整型的最大值,如下图,长度越来越长。我在想,如果想,是否有比这一个比这更长的呢?在C#中,还真有,那就是BigInterger类型。
做了一个实现,取Int128的最大值,加上1,看看BigInter的表现,发现是负值,不科学呀,这明显是越出范围了,再回头一想,可不是吗,=号左边是按Int128类型进行计算的,算完后的结果再赋值给BigInteger,肯定不正确。
BigInteger bi = Int128.MaxValue + 1;
Console.WriteLine(bi);
//输出结果是
//-170141183460469231731687303715884105728
下面是25个Int128最大值相乘的结果:
BigInteger bi = Int128.MaxValue;
Console.WriteLine(bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi * bi);
结果有点壮观,这里不是凑字数,只是为了体现这个BigInteger真的是Big。
58916404216961979676909042591987947822359513725557205849149542555007692652347381264636907638253647314262850348235426824312868335375923591974778384825147349073463542641219884491858801121852450747293521207624100878094057158371253020749844894622137275848581525103728983399170867363637009921601295586614986927881247817386641170766685289471008770749268484605197710958804904735795192578092441774416789002857630151605320763562597782346561384317187923574742179610656057122932294986320480310202786926147268988027802165835084551052581664662548315426718440303766210682391833223357482334422560789744394872838038735794130736180567547347803910439868126971229865036834997854634982160372133268191831626580300194293142529507929212993242469682465540748803280832612453265211011403601272300450012493433025656806407894868992349543408694552546581050551449691803479528787515077512618351797659200540297508115576736370164059967042268186433173185465139879902310589995579447923703807
下面是C#中常见的数值类型。
Console.WriteLine($"short max:{short.MaxValue}");
Console.WriteLine($"short min:{short.MinValue}");
short s = 123;
Console.WriteLine($"short值:{s},占{Marshal.SizeOf(s)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"int max:{int.MaxValue}");
Console.WriteLine($"int min:{int.MinValue}");
int i = 123;
Console.WriteLine($"int值:{i},占{Marshal.SizeOf(i)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"nint max:{nint.MaxValue}");
Console.WriteLine($"nint min:{nint.MinValue}");
nint n = 123;
Console.WriteLine($"nint值:{n},占{Marshal.SizeOf(n)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"long max:{long.MaxValue}");
Console.WriteLine($"long min:{long.MinValue}");
long l = 123;
Console.WriteLine($"long值:{l},占{Marshal.SizeOf(l)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"Int128 max:{Int128.MaxValue}");
Console.WriteLine($"Int128 min:{Int128.MinValue}");
Int128 i128 = 123;
Console.WriteLine($"Int128值:{i128},占{Marshal.SizeOf(i128)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"Half max:{Half.MaxValue}");
Console.WriteLine($"Half min:{Half.MinValue}");
Half h = (Half)43210.123456789;
Console.WriteLine($"Half值:{h},占{Marshal.SizeOf(h)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"float max:{float.MaxValue}");
Console.WriteLine($"float min:{float.MinValue}");
float f = 0.123456789f;
Console.WriteLine($"float值:{f},占{Marshal.SizeOf(f)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"double max:{double.MaxValue}");
Console.WriteLine($"double min:{double.MinValue}");
double d = 0.123456789012345678d;
Console.WriteLine($"double值:{d},占{Marshal.SizeOf(d)}字节");
Console.WriteLine("----------------------------------");
Console.WriteLine($"decimal max:{decimal.MaxValue}");
Console.WriteLine($"decimal min:{decimal.MinValue}");
var m = 0.12345678901234567890123456m;
Console.WriteLine($"decimal值:{m},占{Marshal.SizeOf(m)}字节");
Console.WriteLine("----------------------------------");
运行结果: