Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

For example, enumerable.ToList() versus list.AsReadOnly().

I ask because I've got a class that turns an IEnumerable<T> into an IDataReader. I wonder whether the extension method that creates it should be ToDataReader or AsDataReader.

like image 900
Roger Lipscombe Avatar asked Jan 30 '11 13:01

Roger Lipscombe


1 Answers

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

In your case, AsDataReader should be the right choice, as you don't iterate over the source when creating the DataReader, but only when the user calls methods on it.

like image 65
Femaref Avatar answered Nov 20 '22 21:11

Femaref