How can I check the type of an object at runtime?

You can use the is keyword. For example:
using System;

class CApp
{
public static void Main()
{
string s = "fred";
long i = 10;

Console.WriteLine( "{0} is {1}an integer", s, (IsInteger(s) ? "" : "not ") );
Console.WriteLine( "{0} is {1}an integer", i, (IsInteger(i) ? "" : "not ") );
}

static bool IsInteger( object obj )
{
if( obj is int || obj is long )
return true;
else
return false;
}
}
produces the output:

fred is not an integer
10 is an integer

Comments

Popular posts from this blog

How do I calculate a MD5 hash from a string?

What is WSDL?