如何获取静态对象C#的哈希码?

问题描述 投票:0回答:1

我是初学者.NET开发人员。我正在尝试了解基本内容,以便更好地了解代码中的实际情况。

是否可以获取静态对象的哈希码?在任何情况下都可能有必要吗?

代码如下:

class DynamicClass
{
 //Class body
}


static class StaticClass
{
  //Class body
}

class program 
{
   static void Main()
   { 
     //Getting hashcode of DynamicClass object
     DynamicClass x = new DynamicClass();
     Console.WriteLine(x.GetHashCode());

    //Getting hashcode of StaticClass object
    //Since the class is static i can't instantiate it, so i am
    //trying to call GetHashCode method right on the object
    Console.WriteLine(StaticClass.GetHashCode()); // ERROR CS120 

   }
}

谢谢!

c# .net static
1个回答
0
投票

您应该获取类型并改为调用GetHashCode。示例:

typeof(StaticClass).GetHashCode()

来源:Microsoft Documentation

© www.soinside.com 2019 - 2024. All rights reserved.