What is this code doing? Specifically the default(XX)
part. I've never seen it before.
Entities.BizTalkRequestResult result = default(Entities.BizTalkRequestResult);
It's not a cast; it compiles to the default value of Entities.BizTalkRequestResult
. For a reference type, e.g., that's probably null
. See MSDN: http://msdn.microsoft.com/en-us/library/xwth0h0d(v=vs.80).aspx
It gives you the default value for the particular type inside the parentheses. E.g. 0
for primitives numeric types like int or float, or null
for reference types. It's useful particularly when the type could vary, and you want to write general code that is applicable for all possible types.
There is a misconception; this is not casting at all. The default
operator or function returns the default value. ex: 0 for int and null for reference types.
default
is often used with generics (default(T)
) because we don't know the actual type at compile time.