I have a generic method and it's possible that the object passed is a single object or a list of objects. Example:
public void MyGenericMethod<T>(T something, int? index)
{
// if it is a list how do I get to the object in the list?
}
There are cases when someone will pass a List. If they do pass a list of objects I will then use the index parameter to get the single object out of the list. I can assume that if index is not null then they passed in a list, but then how do I get to that value? I can't do this:
object temp = something[index.Value];
It's important to note that I cannot force the user to pass in a single object into my generic method. Also I cannot make it an array (T[]) and force the user to pass in an array (or a List).
var lst = something as IList; if (lst == null) { // something isn't a list } else { // something is a list }