default value for generic type in c#

BCS picture BCS · Dec 15, 2008 · Viewed 39.5k times · Source

The docs for Dictionary.TryGetValue say:

When this method returns, [the value argument] contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

I need to mimic this in my class. How do I find the default value for type T?


How can this question be modified to make it show up in the search?

Exact duplicate of Returning a default value. (C#)

Answer

Nathan W picture Nathan W · Dec 16, 2008

You are looking for this:

default(T);

so:

public T Foo<T>(T Bar)
{
   return default(T);
}