NJY404
NJY404

Reputation: 358

How to get the actual type of object

I want to get the actual type of an object even though I convert it to other types like for example I have an Integer and convert it to Object, is there a way to find out how?

Upvotes: 0

Views: 1637

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190907

.NET has .GetType() as part of every object. You could use this to do stuff with reflection.

C# also has pattern matching:

if (obj is int intVal)
{
    // use as an integer
}

When you cast or box any value from one type to System.Object, it still under the covers has the same value.

Upvotes: 3

Related Questions