C# Int16 结构体
Int16 结构体表示一个 16 位有符号整数,其值范围从负 32768 到正 32767。
以下是 Int16 的字段:
序号 | 字段及描述 |
---|---|
1 | MaxValue - 表示 Int16 的最大可能值。此字段为常量。 |
2 | MinValue - 表示 Int16 的最小可能值。此字段为常量。 |
以下是一些方法:
序号 | 方法及描述 |
---|---|
1 | CompareTo(Int16) - 将此实例与指定的 16 位有符号整数进行比较,并返回一个整数,该整数指示此实例的值是否小于、等于或大于指定 16 位有符号整数的值。 |
2 | CompareTo(Object) - 将此实例与指定的对象进行比较,并返回一个整数,该整数指示此实例的值是否小于、等于或大于对象的值。 |
3 | Equals(Int16) - 返回一个值,指示此实例是否等于指定的 Int16 值。 |
4 | Equals(Object) - 返回一个值,指示此实例是否等于指定的对象。 |
5 | GetHashCode() - 返回此实例的哈希代码。 |
6 | GetTypeCode() - 返回值类型 Int16 的 TypeCode。 |
7 | Parse(String) - 将数字的字符串表示形式转换为其 16 位有符号整数等效项。 |
现在让我们看看一些 Int16 结构体的示例:
C# 中的 Int16.GetHashCode() 方法用于返回当前实例的哈希代码。
语法
public override int GetHashCode ();
示例
现在让我们看一个实现 Int16.GetHashCode() 方法的示例:
using System; public class Demo { public static void Main() { short val1 = 20; short val2 = 25; Console.WriteLine("Value1 = "+val1); Console.WriteLine("Value2 = "+val2); Console.WriteLine("HashCode for value1 = "+val1.GetHashCode()); Console.WriteLine("HashCode for value2 = "+val2.GetHashCode()); Console.WriteLine("Are they equal? = "+(val1.Equals(val2))); } }
输出
这将产生以下输出:
Value1 = 20 Value2 = 25 HashCode for value1 = 1310740 HashCode for value2 = 1638425 Are they equal? = False
示例
现在让我们看另一个实现 Int16.GetHashCode() 方法的示例:
using System; public class Demo { public static void Main() { short val1 = 0; short val2 = Int16.MaxValue; Console.WriteLine("Value1 = "+val1); Console.WriteLine("Value2 = "+val2); Console.WriteLine("HashCode for value1 = "+val1.GetHashCode()); Console.WriteLine("HashCode for value2 = "+val2.GetHashCode()); Console.WriteLine("Are they equal? = "+(val1.Equals(val2))); } }
输出
这将产生以下输出:
Value1 = 0 Value2 = 32767 HashCode for value1 = 0 HashCode for value2 = 2147450879 Are they equal? = False
C# 中的 Int16.GetTypeCode() 方法用于返回值类型 Int16 的 TypeCode。
语法
public TypeCode GetTypeCode ();
示例
现在让我们看一个实现 Int16.GetTypeCode() 方法的示例:
using System; public class Demo { public static void Main() { short val1 = 0; short val2 = Int16.MaxValue; Console.WriteLine("Value1 = "+val1); Console.WriteLine("Value2 = "+val2); Console.WriteLine("HashCode for value1 = "+val1.GetHashCode()); Console.WriteLine("HashCode for value2 = "+val2.GetHashCode()); Console.WriteLine("Are they equal? = "+(val1.Equals(val2))); TypeCode type1 = val1.GetTypeCode(); TypeCode type2 = val2.GetTypeCode(); Console.WriteLine("TypeCode for val1 = "+type1); Console.WriteLine("TypeCode for val2 = "+type2); } }
输出
这将产生以下输出:
Value1 = 0 Value2 = 32767 HashCode for value1 = 0 HashCode for value2 = 2147450879 Are they equal? = False TypeCode for val1 = Int16 TypeCode for val2 = Int16
示例
现在让我们看另一个实现 Int16.GetTypeCode() 方法的示例:
using System; public class Demo { public static void Main() { short val1 = 23; short val2 = 0; Console.WriteLine("Value1 = "+val1); Console.WriteLine("Value2 = "+val2); Console.WriteLine("HashCode for value1 = "+val1.GetHashCode()); Console.WriteLine("HashCode for value2 = "+val2.GetHashCode()); Console.WriteLine("Are they equal? = "+(val1.Equals(val2))); TypeCode type1 = val1.GetTypeCode(); TypeCode type2 = val2.GetTypeCode(); Console.WriteLine("TypeCode for val1 = "+type1); Console.WriteLine("TypeCode for val2 = "+type2); } }
输出
这将产生以下输出:
Value1 = 23 Value2 = 0 HashCode for value1 = 1507351 HashCode for value2 = 0 Are they equal? = False TypeCode for val1 = Int16 TypeCode for val2 = Int16
广告