how to check type of a variable c# code example
Example 1: get type of variable c#
string a = "This is a string";
Console.WriteLine(a.GetType())
Example 2: 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!")
}