get type of object code example
Example 1: find type of variable c#
string StringType
Type TheType = StringType.GetType();
Console.WriteLine(TheType.Name)
if(YourVar is YourType)
Console.WriteLine("Is your type you are testing for")
Example 2: c# get type of object
object.GetType();
Example 3: c# find the type of a variable
using System.Text
ArrayList myArray = new ArrayList();
myArray.Add("Hello");
myArray.Add(12);
myArray.Add('+');
myArray.Add(10);
int myVariable = 0;
foreach(object obj in myArray){
if(obj is int)
myVariable += Convert.ToInt32(obj);
if(obj is string)
Console.WriteLine(obj + " World");
}
Console.WriteLine(myVariable);
Cube myTest = myVariable as Cube;
if(myTest == null){
Console.WriteLine("MyVariable it's not a Cube!")
}
else {
Console.WriteLine("MyVariable is a Cube!")
}
Example 4: how to find the type of a object c#
use the "is" keyword