Reputation: 3
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
Reputation: 787
You should get type and call GetHashCode on it instead. Example:
typeof(StaticClass).GetHashCode()
Source: Microsoft Documentation
Upvotes: 3