当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Int64.ToString()用法及代码示例


C# 中的 Int64.ToString() 方法用于将此实例的数值转换为其等效的字符串表示形式。

用法

语法如下 -

public override string ToString ();
public string ToString (string format);

上面,参数格式是一个数字格式字符串。

示例

现在让我们看一个例子 -

using System;
public class Demo {
   public static void Main(){
      long val1 = 0;
      long val2 = Int64.MaxValue;
      Console.WriteLine("Value1 = "+val1.ToString());
      Console.WriteLine("Value2 = "+val2.ToString());
      Console.WriteLine("Are they equal? = "+val1.Equals(val2));
      Console.WriteLine("Value1 (HashCode) = "+val1.GetHashCode());
      Console.WriteLine("Value2 (HashCode) = "+val2.GetHashCode());
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      Console.WriteLine("TypeCode for val1 = "+val1.GetTypeCode());
      Console.WriteLine("TypeCode for val2 = = "+val1.GetTypeCode());
   }
}

输出

这将产生以下输出 -

Value1 = 0
Value2 = 9223372036854775807
Are they equal? = False
Value1 (HashCode) = 0
Value2 (HashCode) = -2147483648
TypeCode for val1 = Int64
TypeCode for val2 = = Int64

示例

现在让我们看另一个例子 -

using System;
public class Demo {
   public static void Main(){
      long val1 = 87987687;
      long val2 = 35436367;
      Console.WriteLine("Value1 = "+val1.ToString("D8"));
      Console.WriteLine("Value2 = "+val2.ToString("X"));
      Console.WriteLine("Are they equal? = "+val1.Equals(val2));
      Console.WriteLine("Value1 (HashCode) = "+val1.GetHashCode());
      Console.WriteLine("Value2 (HashCode) = "+val2.GetHashCode());
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      Console.WriteLine("TypeCode for val1 = "+val1.GetTypeCode());
      Console.WriteLine("TypeCode for val2 = = "+val1.GetTypeCode());
   }
}

输出

这将产生以下输出 -

Value1 = 87987687
Value2 = 21CB74F
Are they equal? = False
Value1 (HashCode) = 87987687
Value2 (HashCode) = 35436367
TypeCode for val1 = Int64
TypeCode for val2 = = Int64

相关用法


注:本文由纯净天空筛选整理自AmitDiwan大神的英文原创作品 Int64.ToString() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。