default keyword in C#?
The most common use is with generics; while it works for "regular" types (i.e. default(string) etc), this is quite uncommon in hand-written code.
I do, however, use this approach when doing code-generation, as it means I don't need to hard-code all the different defaults - I can just figure out the type and use default(TypeName) in the generated code.
In generics, the 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 shouldn't really care what it is. You can contrast this to the constructor constraint:
public static T CreateAndInit() where T : ISomeInterface, new() {
T t = new T();
t.SomeMethodOnInterface();
return t;
}