default value for generic type in c#

C#GenericsDefault Value

C# Problem Overview


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

C# Solutions


Solution 1 - C#

You are looking for this:

default(T);

so:

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

Solution 2 - C#

default(T);

Solution 3 - C#

Using the default keyword:

T t = default(T)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBCSView Question on Stackoverflow
Solution 1 - C#Nathan WView Answer on Stackoverflow
Solution 2 - C#Szymon RozgaView Answer on Stackoverflow
Solution 3 - C#Darin DimitrovView Answer on Stackoverflow