tags:

views:

73

answers:

1

Good afternoon,

I have an generic method like

 public T GetLevelElement<T>(string name) where T : ILevelElement
        {
[...]
        }

Which basically performs a lookup in a db and in some cases it does not (and cannot return) a result and I would like to return null.

However that's obviously not possible because of 'There is no implicit conversion between T and null'. What should I do in this case?

-J

+12  A: 

T cannot be null, because T could be a value type. Try returning default(T) or adding a class constraint to indicate that T can only be a reference type like so:

public T GetLevelElement<T>(string name) where T : ILevelElement, class
{
    [...]
}
Dustin Campbell