Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Use collection of elements like an indexed array

Use collection of elements like an indexed array

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Monin D
    wrote on last edited by
    #1

    I want to create my own collection of elements. I do it so:

    public class MyCollectionName : IEnumerable
    {
    List<string> m_Collection = new List<string>();
    // Some methods here

     public int Count
     {
         get { return m\_Collection.Count; }
     }
     
     public IEnumerator GetEnumerator()
     {
          return m\_Collection.GetEnumerator();
     }
    

    }

    Is it possible to use instance of MyCollectionName like array and what should I change in MyCollectionName class for it? for example:

    for(int i = 0; i < myCollectionInstance.Count; i++)
    {
    // And here use my collection as array
    myCollectionInstance[i];
    }

    L G 2 Replies Last reply
    0
    • M Monin D

      I want to create my own collection of elements. I do it so:

      public class MyCollectionName : IEnumerable
      {
      List<string> m_Collection = new List<string>();
      // Some methods here

       public int Count
       {
           get { return m\_Collection.Count; }
       }
       
       public IEnumerator GetEnumerator()
       {
            return m\_Collection.GetEnumerator();
       }
      

      }

      Is it possible to use instance of MyCollectionName like array and what should I change in MyCollectionName class for it? for example:

      for(int i = 0; i < myCollectionInstance.Count; i++)
      {
      // And here use my collection as array
      myCollectionInstance[i];
      }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      yes, thats called an indexer; it is like a property that takes an integer argument. Example: public Record this[int index] { get {return (Record)currentRecords[index];} } gives the containing class a getter that returns an item of type Record. :)

      Luc Pattyn [My Articles]

      1 Reply Last reply
      0
      • M Monin D

        I want to create my own collection of elements. I do it so:

        public class MyCollectionName : IEnumerable
        {
        List<string> m_Collection = new List<string>();
        // Some methods here

         public int Count
         {
             get { return m\_Collection.Count; }
         }
         
         public IEnumerator GetEnumerator()
         {
              return m\_Collection.GetEnumerator();
         }
        

        }

        Is it possible to use instance of MyCollectionName like array and what should I change in MyCollectionName class for it? for example:

        for(int i = 0; i < myCollectionInstance.Count; i++)
        {
        // And here use my collection as array
        myCollectionInstance[i];
        }

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Add a default indexer:

        public string this[int index] {
        get { return m_Collection[index]; }
        }

        --- single minded; short sighted; long gone;

        M 1 Reply Last reply
        0
        • G Guffa

          Add a default indexer:

          public string this[int index] {
          get { return m_Collection[index]; }
          }

          --- single minded; short sighted; long gone;

          M Offline
          M Offline
          Monin D
          wrote on last edited by
          #4

          Thanks!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups