Funny Fat Man
Funny Fat Man

Reputation: 3

How to get hash code of static object C#?

I am a begginer .NET developer. I am trying to understand basic things to better understand of what's really going in my code.

Is it possible to get a hashcode of a static object? And might it be necessary in any cases?

The code is below:

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 

   }
}

Thank you!

Upvotes: 0

Views: 569

Answers (1)

misticos
misticos

Reputation: 787

You should get type and call GetHashCode on it instead. Example:

typeof(StaticClass).GetHashCode()

Source: Microsoft Documentation

Upvotes: 3

Related Questions