Search code examples
.netreturn-valueinitializer

.Net return initializer?


I’m trying to get my head around this. I know about object initializers in recent versions of .Net but the code below confuses me. First is the calling code, and below that I’ve put the needed class and interface definitions.

var mockCustomer = new Customer_GetUserByCustIdResult();
var testSingleResult = new SingleResult<Customer_GetUserByCustIdResult> { mockCustomer };


public class SingleResult<T> : ISingleResult<T>
public interface ISingleResult<T> : IEnumerable<T>, IEnumerable, IFunctionResult, IDisposable

Using Visual Studio 2012’s intellisense I see that when newing up testSingleResult the mockCustomer class is treated as a return value—something to do with the IFunctionResult interface I believe. I’m not 100% sure how this is all working—rather than initializing member variables, we’re initializing a return value? That’s what it looks like anyway and I haven't seen this before.


Solution

  • What you're looking at is a collection initializer, see: http://msdn.microsoft.com/en-us/library/bb384062.aspx