C#中的Int16和UInt16之间的区别
C#Int16和C#UInt16
在C#中,Int16被称为2字节的有符号整数,它可以存储-32768至+32767范围之间的两种类型的值,包括负数和正数。
UInt16,它是2个字节的无符号整数,只能存储0到65535范围之间的正值。
“Int16”和“UInt16”之间的区别
Int16variable;
UInt16variable;
示例
在此示例中,为了解释C#中Int16和UInt16之间的区别,我们将打印它们的最小值和最大值,同时还声明了两个数组–arr1是有符号整数类型,而arr2是无符号整数类型。根据其容量用相应的负值和正值初始化数组。
using System; using System.Text; namespace Test { class Program { static void Main(string[] args) { //Int16值范围 Console.WriteLine("Int16 value capacity..."); Console.WriteLine("Min: {0}, Max: {1}\n", Int16.MinValue, Int16.MaxValue); //UInt16值范围 Console.WriteLine("UInt16 value capacity..."); Console.WriteLine("Min: {0}, Max: {1}\n", UInt16.MinValue, UInt16.MaxValue); //Int16数组 Int16[] arr1 = { -32768, 0, 1000, 32000, 32767}; Console.WriteLine("UInt16 array elements..."); foreach (Int16 num in arr1) { Console.WriteLine(num); } Console.WriteLine(); //UInt16数组 UInt16[] arr2 = { 0, 100, 23000, 65000, 65525}; Console.WriteLine("UInt16 array elements..."); foreach (UInt16 num in arr2) { Console.WriteLine(num); } //按ENTER退出 Console.ReadLine(); } } }
输出结果
Int16 value capacity... Min: -32768, Max: 32767 UInt16 value capacity... Min: 0, Max: 65535 UInt16 array elements... -32768 0 1000 32000 32767 UInt16 array elements... 0 100 23000 65000 65525