Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • IEnumerable in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 106
    Comment on it

    While storing and retrieving list of elements we use many classes for that like ICollection,IList .

     

     

     

     

    IEnumerable<T>


    The read only type of collection is used to repeat the IEnumerable<T>. It gives only one method named GetEnumerator() using which user can repeat the read only elements using foreach loop. IEnumerable <T> is defined in as follows:

     

     

     

    public void PrintAgeUpto30(IEnumerator<int> age_IEnumerator)
    {
       while (age_IEnumerator.MoveNext()){
          Console.WriteLine(age_IEnumerator.Current);
          if (age_IEnumerator.Current > 20) {
             Console.WriteLine("PrintGreaterThan30 is called");
             PrintGreaterThan30(age_IEnumerator);
          }
       }
    }
    
    public void PrintGreaterThan30(IEnumerator<int> age_IEnumerator)
    {
        while (age_IEnumerator.MoveNext())
            Console.WriteLine(age_IEnumerator.Current);
    }
    // Now Call PrintUpto30 which will call PrintGreaterThan30 
    // by using our previous age IEnumerator
    PrintUpto30(IEnumerator);

    We use IEnumerable when we don't want to add or remove element from a collection.

    We don't have any scope for changing the contents while performing any iteration over it and for iteration only for each loop  is used.

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: