C# Int16.ToString() 方法
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
广告