Implicit Arrays

One of new features in C# 3.0 that I am really fond of, is not having to declare what type of array it is. It starts by checking all the values in the array and seeing if they can be implicitly converted to one type. If it can, than it is considered the array type.

image

Implicit arrays does not only apply to simple value type arrays. In fact implicit arrays can be applied to array consisting of anonymous complex types. In the above example, I start with declaring the type of array to be integer. Next I declare an array consisting of numbers. Compiler, based on values automatically infers that it is an array of integers. In the last example, I am creating an array which consist of complex anonymous types. Since all the complex anonymous types resolves to the same anonymous types, compiler resolves it to an array of that specific complex type.

No Comments