In .NET, what's the difference between AsFoo () and ToFoo ()?

For example, enumerable.ToList() compared to list.AsReadOnly() .

I ask because I have a class that turns IEnumerable<T> into IDataReader . I wonder if the extension method he creates should have ToDataReader or AsDataReader .

+8
naming-conventions
source share
1 answer

As* methods return another interface without iterating the source, while To* iterates over it and creates a new object.

In your case, AsDataReader should be the right choice, since you do not AsDataReader over the source code when creating the DataReader , but only when the user calls methods on it.

+16
source share

All Articles