Why arrays can use LINQ queries

Source: Internet
Author: User

Problem leads

This is a topic that does not have to be discussed at all, because LINQ (specifically LINQ to Objects) is intended for collection types, and an array type is of course available as a collection type. But I still want to write about it, the problem stems from a friend of QQ group's question:. NET array type implicitly inherits the array class, which is an abstract class, and implements the IEnumerable, ICollection, IList interfaces. But the LINQ approach is for implementing the ienumerable<t> generic interface, the array class does not implement these generic interfaces, why can you use these methods?

The essence of LINQ to Objects is to implement queries for collections by means of extension methods defined in a static class of enumerable. The first parameter of all extension methods under the enumerable class is the ienumerable<t> type, which means that it can be called through the ienumerable<t> type.

An analysis of array types

1. All array types are implicitly derived from array

When we define a filestream[] array, the CLR creates a filestream[] type for the current AppDomain that derives from the array. So the array is a reference type and allocates memory space in the heap. The array class is an abstract class that defines a number of commonly used instance methods and static methods for use by all array types. Examples are common: the Length property, the CopyTo method, and so on.

2. All array types implicitly implement the Ienumerable<t> interface

As above, this is a matter of course, in order to improve development efficiency, the array type should be able to query using LINQ. But since arrays can be multidimensional arrays or non-0 cardinality groups, the array class does not implement the ienumerable<t>, Icollection<t>, ilist<t>, but only the non-generic versions of the generic interfaces. In fact, the CLR automatically implements these generic interfaces for one-dimensional array types (specifying the concrete type of the T-type parameter) and also for their parent class. For example, if we define a filestream[] array type, then the CLR creates the following hierarchy type structure for us:

Because of the implicit implementation of the CLR, we can apply a one-dimensional array type where the ienumerable<t> generic interface is needed.

According to the above, we can pass an object of type filestream[] to the following method:

void F1 (ienumerable<object> oenumerable);

void F2 (icollection<stream> scollection);

void F3 (ilist<filestream> flist);

This is for reference types, and if they are value types, they are not implemented for their base classes. For example, the Datetimel type (base class includes ValueType and object), datetime[] array types cannot be passed to the F1 method above, because the memory layout of the array of value types differs from the array of reference types.

Available in tablets by "graph bucket"

Why arrays can use LINQ queries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.