Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, July 9, 2014 8:41 AM
Simple Explanations
Please help me out
All replies (2)
Wednesday, July 9, 2014 8:57 AM ✅Answered
IEnumerable is an interface an object can implement if it wants clients to able able to iterate through its contents. Enumerable is a helper class that contaions static methods that implement the IEnummerbale extensions. So if a class supports IEnummerable you can do this;
if (someClass.Any())
{
// the class contains data
}
the "Any" method is actually a method on the Enumerable class. As is "Where", "Min" etc etc etc, all the operations you're probably used to using
http://msdn.microsoft.com/en-us/library/system.linq.enumerable(v=vs.100).ASPX
Wednesday, July 9, 2014 10:58 AM ✅Answered
IEnumerable is an interface. this is a abstract class (not implemented) that defines a set of methods. Its part of the system.collections namespace. it has one method, GetEnumerator(). this is the method the foreach loop uses loop through a collection. this is the core feature of collections.
.net also supports extension methods. these are static methods that when used in code look like object methods but are not:
public static int MyCount (this IEnumerable collection) {
int i = 0;
foreach (object o in collection) {
++i;
}
return i;
}
notice you pass a collection to the method,
var i = MyCount(collection);
but because its an extension method (the magic this) you call it like:
var i = collection.MyCount();
the Enumerable class is part of Linq library and is a bunch of extension methods written for the IEnumerable interface. (like Any(), Count(), etc). most of the linq library is implemented as extension methods. if fact extension methods were added to .net in order to implement linq.