One of the extension methods on IEnumerable<T>
is .AsEnumerable()
. This method converts the enumerable object it was called on into an instance of IEnumerable<T>
. However, since an object must implement IEnumerable<T>
in order to apply to this extension method, converting to IEnumerable<T>
is a simple matter of casting to IEnumerable<T>
. My question is why does this method exist at all?
Example:
List<string> strings = new List<string>() { "test", "test2", "test3" };
IEnumerable<string> stringsEnum1 = strings.AsEnumerable();
IEnumerable<string> stringsEnum2 = (IEnumerable<string>)strings;
In the example above, stringsEnum1
and stringsEnum2
are equivalent. What's the point of the extension method?
Edit: As a corollary, why is there an .AsQueryable()
method when casting to IQueryable<T>
is equivalent?