当前位置: 首页>>代码示例>>C#>>正文


C# RuntimeHelpers.GetHashCode方法代码示例

本文整理汇总了C#中System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode方法的典型用法代码示例。如果您正苦于以下问题:C# RuntimeHelpers.GetHashCode方法的具体用法?C# RuntimeHelpers.GetHashCode怎么用?C# RuntimeHelpers.GetHashCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.CompilerServices.RuntimeHelpers的用法示例。


在下文中一共展示了RuntimeHelpers.GetHashCode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Runtime.CompilerServices;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("{0,-18} {1,6} {2,18:N0}    {3,6} {4,18:N0}\n",
                        "", "Var 1", "Hash Code", "Var 2", "Hash Code");
      
      // Get hash codes of two different strings.
      String sc1 = "String #1";
      String sc2 = "String #2";
      ShowHashCodes("sc1", sc1, "sc2", sc2);
 
      // Get hash codes of two identical non-interned strings.
      String s1 = "This string";
      String s2 = String.Format("{0} {1}", "This", "string");
      ShowHashCodes("s1", s1, "s2", s2);

      // Get hash codes of two (evidently concatenated) strings.
      String si1 = "This is a string!";
      String si2 = "This " + "is " + "a " + "string!";
      ShowHashCodes("si1", si1, "si2", si2);
   }

   private static void ShowHashCodes(String var1, Object value1, 
                                     String var2, Object value2)
   {
      Console.WriteLine("{0,-18} {1,6} {2,18:X8}    {3,6} {4,18:X8}",
                        "Obj.GetHashCode", var1, value1.GetHashCode(),
                        var2, value2.GetHashCode());

      Console.WriteLine("{0,-18} {1,6} {2,18:X8}    {3,6} {4,18:X8}\n",
                        "RTH.GetHashCode", var1, RuntimeHelpers.GetHashCode(value1),
                        var2, RuntimeHelpers.GetHashCode(value2));
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.CompilerServices,代码行数:39,代码来源:RuntimeHelpers.GetHashCode

输出:

Var 1          Hash Code     Var 2          Hash Code

Obj.GetHashCode       sc1           94EABD27       sc2           94EABD24
RTH.GetHashCode       sc1           02BF8098       sc2           00BB8560

Obj.GetHashCode        s1           29C5A397        s2           29C5A397
RTH.GetHashCode        s1           0297B065        s2           03553390

Obj.GetHashCode       si1           941BCEA5       si2           941BCEA5
RTH.GetHashCode       si1           01FED012       si2           01FED012


注:本文中的System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。