What is this cast code doing?

Go To StackoverFlow.com

1

What is this code doing? Specifically the default(XX) part. I've never seen it before.

Entities.BizTalkRequestResult result = default(Entities.BizTalkRequestResult);
2012-04-05 22:53
by Shai Cohen
possible duplicate of C# -Default KeywordChris Farmer 2012-04-05 22:55


3

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

2012-04-05 22:55
by Ry-


1

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.

2012-04-05 22:55
by Zach Johnson


1

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.

2012-04-05 22:59
by Sleiman Jneidi
Ads