C#中的Int16.ToString()方法用于将该实例的数值转换为其等效的字符串表示形式。
语法如下-
public override string ToString (); public string ToString (string format);
上面的format参数是一个数字格式字符串。
现在让我们看一个例子-
using System; public class Demo { public static void Main() { short val1 = 10; short val2 = 10; Console.WriteLine("Value1 = "+val1.ToString()); Console.WriteLine("Value2 = "+val2.ToString()); Console.WriteLine("HashCode for value1 = "+val1.GetHashCode()); Console.WriteLine("HashCode for value2 = "+val2.GetHashCode()); Console.WriteLine("Are they equal? = "+(val1.Equals(val2))); } }
输出结果
这将产生以下输出-
Value1 = 10 Value2 = 10 HashCode for value1 = 655370 HashCode for value2 = 655370 Are they equal? = True
现在让我们来看另一个示例-
using System; public class Demo { public static void Main() { short val1 = 20; short val2 = 18; Console.WriteLine("Value 1 = "+val1.ToString("F1")); Console.WriteLine("Value 2 = "+val2.ToString("000000.0000")); Console.WriteLine("Return value (comparison) = "+val1.CompareTo(val2)); } }
输出结果
这将产生以下输出-
Value 1 = 20.0 Value 2 = 000018.0000 Return value (comparison) = 2