The most common use with generics ; while it works for βregularβ types (ie default(string) , etc.), this is rather unusual in handwritten code.
However, I use this approach when creating code, as it means I donβt need to hardcode all the different default values ββ- I can just figure out the type and use default(TypeName) in the generated code.
In generics, classic usage is the TryGetValue pattern:
public static bool TryGetValue(string key, out T value) { if(canFindIt) { value = ...; return true; } value = default(T); return false; }
Here we have to assign a value to exit the method, but the caller does not care what it is anyway. You can compare this with a constructor constraint:
public static T CreateAndInit<T>() where T : ISomeInterface, new() { T t = new T(); t.SomeMethodOnInterface(); return t; }
Marc Gravell Nov 13 '09 at 6:19 2009-11-13 06:19
source share