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#)
This means that you can put any object in a collection because all classes in the C# programming language extend from the object base class. Also, we cannot simply return null from a generic method like in normal method.
The object and string types have a default value of null, representing a null reference that literally is one that does not refer to any object.
Generics are syntax components of a programming language that can be reused for different types of objects. Typically, generics take the form classes or functions, which take type(s) as a parameter. Generics are also commonly referred to as templates , and in C++ are officially called templates.
You are looking for this:
default(T);
so:
public T Foo<T>(T Bar) { return default(T); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With