Return NULL from a generic c#-Method

Return NULL from a generic c#-Method 

static T FindThing<T>(IList collection, int id) where T : IThing, new()
{
    foreach T thing in collecion
    {
        if (thing.Id == id)
            return thing;
    }
    return default(T);
}

Return default(T) which means you'll return null if T is a reference type (or a nullable value type), 0 for int, '\0' for char etc.

http://stackoverflow.com/questions/302096/how-can-i-return-null-from-a-generic-method-in-c

 

Kommentare sind geschlossen

Tags