IEnumerable: Power behind Collections

Thought
Thought @Image courtesy of farconville/ FreeDigitalPhotos.net

IEnumerable: Power behind Collections

Welcome to CodeSpread!

One of my favorite questions which I ask very frequently to test the candidate is “What is Collections?”

And I get almost all kind of answers, possibly available on planet Mars. But one answer which is expected on Earth is “Collections are elements which implements IEnumerable interface”.

But why this answer is so important! What the heck is IEnumerable?

IEnumerable is an interface which contains a method definition GetEnumerator(). This method has a return type of IEnumerator interface.


Interface IEnumerable

{

public IEnumerator GetEnumerator();

}

 

IEnumerator is the base interface for all nongeneric enumerators. Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection. So this Interface IEnumerator ensures read only access to the collection.

Although any element in a collection can be retrieved through its index property but a collection that implements IEnumerable can be used with a foreach statement to retrieve the elements.

The IEnumerator provides two abstract methods and a property to fetch a particular element from the collection.

Property:


Object Current { get; }

It’s a read only property. It retrieves the current individual element of the collection.

Methods:

MoveNext-


bool MoveNext()

As the name says, This method advances the enumerator to the next element of the collection.

Conditional Output:

True: if the enumerator was successfully advanced to the next element

False: if the enumerator has passed the end of the collection.

Reset-


void Reset()

As the name says, it resets the collection by setting the enumerator to its initial position, which is before the first element in the collection. The Reset method is provided for COM interoperability.

To make the things more simple, the complexity of the enumerators are avoided by using  foreach statement. This drives the iterations and there is no need to directly manipulate the enumerators.

Note:

  • Enumerators cannot be used to modify the underlying collection.

  • Enumerating through a collection is intrinsically not a thread-safe procedure.

  • Enumerator remains valid as long as the collection remains unchanged.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • IQueryable: Performance boost over remote Data AccessIQueryable: Performance boost over remote Data AccessIf we also follow the inheritance principle like OOPS programming, we need to extend our previous discussion “IEnumerable: Power behind Collections”. So this will lead us to write something about I...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • C#: Anonymous Type is not anonymous!C#: Anonymous Type is not anonymous!Why Anonymous Type? Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type inform...
  • Indexers,A Short Note.Indexers,A Short Note.What is an indexer? Indexers can be easily remembered as arrays. These arrays are special as they allow the class to be treated as arrays and access the data stored within the object. For example:...
  • Simply LINQ: What,Why and How.Simply LINQ: What,Why and How.What is LINQ? LINQ is Language-Integrated Query (LINQ). It is primarily an extension to the language's capability where it adds query expressions which processes objects and data. It is a one s...
  • My first experience with LISTS in C#My first experience with LISTS in C#Hi guys, this is Sathish, Lets discuss the concepts of lists. These are the concepts which I have planned to discuss with you guys today. What is a List and how it is advantageous than array...
  • Software Testing Life Cycle (STLC)Software Testing Life Cycle (STLC)Software Testing Life Cycle (STLC) is a process which consists of a number of phases to improve the quality of the product. Each phase involves various testing activities like Requirement An...
  • 23 Dec: Must Read Codes [11-15]23 Dec: Must Read Codes [11-15]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...