Say we have the following :
object[] objs = new object[3]{ "the ultimate question", 42L, 1};
and we want to do something to the individual objects in the array like :
foreach (object o in objs)
{
dosomething(o);
}
Now
dosomething
will only take
object
parameters so there is boxing going on, but we require to be able to do something based on the type of the object, so we would do something like :
public void dosomething(object obj)
{
if(obj.GetType() == typeof(string))
{
}
if(obj is long)
{
}
}
Well it's ugly and non performant, is there a better way possible using generics etc.?