Closed Thread
Results 1 to 2 of 2

Thread: Collection

  1. #1
    one198 is offline Newbie
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Collection

    Hello Guys

    I Am new to this forum as well as C# programming Language .Nowadays I am studying Collection C# and used some web site and E book to learn C#.Below Coding I got from a one of web site but I could not understand what is happen in that coding.


    Code:
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    
    public class Fruit
     {
      //this is a bad way to declare fields in a class
      //but this is just an example 
      public string name;
      public bool ripe;
      public int num;
    
      public Fruit(string nme, bool rp, int n)
      {
       name = nme;
       ripe = rp;
       num = n;
      }
     }
    
    
    public class Fruits :IEnumerable,IEnumerator
    {
     Fruit[] fruitArray;
     //current position in array set to initial position
     int position = -1;
     //Create a basket of fruit    
     public Fruits()
     {
      fruitArray = new Fruit[3];
      fruitArray[0] = new Fruit("Banana",false,3);
      fruitArray[1] = new Fruit("Apples",true,5);
      fruitArray[2] = new Fruit("Oranges",true,3);
     }
    
     //Implement IEnumerable
    
     public IEnumerator GetEnumerator()
     {
     return (IEnumerator)this;
     }
    
     //Implement IEnumerator
     public bool MoveNext()
     {
      position++;
      if (position < fruitArray.Length)
      {
       return true;
      }
      else
      {
       return false;
      }
     }
     public void Reset()
     {
      position = -1;
     }
     public object Current
     {
      get{return fruitArray[position];}
     }
    }
    
    class FruitBar
    {
     static void Main(string[] args)
     {
      Fruits fruitbasket = new Fruits();
      //Present only the ripe fruit
      foreach (Fruit f in fruitbasket)
      {           
       if (f.ripe == true)
       Console.WriteLine("Name : {0}",f.name);
       Console.WriteLine("Amount : {0}",f.num);  
      }     
      string x = Console.ReadLine();
      }
     }


    So anyone can tell me ,which part we used IEnumerator?Purpose Of the IEnumerator?

    Thank Youd
    Last edited by one198; 01-25-2008 at 02:27 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    gokuajmes's Avatar
    gokuajmes is offline Programming God
    Join Date
    Jan 2010
    Location
    India
    Posts
    516
    Blog Entries
    5
    Rep Power
    12

    Re: Collection

    Inumerator derives from the Interface ,based . It checks if the Coolection ,ListItem ..etc is enumerable that is are they able to count the number of items ,so that it may be looped using a For or a Foreach statement.KickBack to the MSdn Library for more information.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Garbage collection
    By Pradeepa in forum Java Help
    Replies: 5
    Last Post: 09-14-2011, 03:10 PM
  2. Photo collection for a website
    By graphic360 in forum Website Design
    Replies: 1
    Last Post: 01-01-2011, 12:46 PM
  3. What is garbage collection?
    By DevilsCharm in forum C# Programming
    Replies: 4
    Last Post: 11-12-2007, 09:39 AM
  4. Python Collection
    By reachpradeep in forum Python
    Replies: 1
    Last Post: 03-03-2007, 12:50 PM
  5. Collection of 70+ pre-designed tables.
    By MrDiaz in forum HTML Programming
    Replies: 1
    Last Post: 07-04-2006, 11:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts