views:

2040

answers:

3

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 unin

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#)

+7  A: 
default(T);
siz
Yep.. introduced along with generics (.NET 2.0, 2005)
Andrei Rinea
+4  A: 

You are looking for this:

default(T);

so:

public T Foo<T>(T Bar)
{
   return default(T);
}
Nathan W
+2  A: 

Using the default keyword:

T t = default(T)
Darin Dimitrov