比如如下的代码使用三种方式来处理不同类型的数据:ArrayList list = new ArrayList();
list.Add(123);
list.Add("字符串");
list.Add(true);foreach(object obj in list)
{
Console.WriteLine(obj.GetType().Name); if (obj is int)
{
}
else if (obj is string)
{
}
else if (obj is bool)
{
} if (obj.GetType() == typeof(int))
{
}
else if (obj.GetType() == typeof(string))
{
}
else if (obj.GetType() == typeof(bool))
{
}
}
list.Add(123);
list.Add("字符串");
list.Add(true);foreach(object obj in list)
{
Console.WriteLine(obj.GetType().Name); if (obj is int)
{
}
else if (obj is string)
{
}
else if (obj is bool)
{
} if (obj.GetType() == typeof(int))
{
}
else if (obj.GetType() == typeof(string))
{
}
else if (obj.GetType() == typeof(bool))
{
}
}
object[] arr = new object[10];
arr[0] = 1;
arr[1] = "hello";
// ...
foreach(object o in arr) {
Type t = o.GetType();
// check the t
}