Collection question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • (2b|!2b)==?

    Collection question

    I am a relatively new to C#. I want to write a method that returns an
    'iterable' collection:

    TheCollection = MyClass.MethodN ame();

    // Further down, I want to use for .. each

    foreach(MyColle ctionType ct in TheCollection)
    {
    //do something
    }

    The collection will store a specific type (a reference type). Since I
    cannot specify the data returned as a const (e.g. in C++, the method
    signwould look something like this):

    const TheCollection& MyClass::Method Name() const ;

    I have a number of questions:

    1). What Interface should the TheCollection class implement (Ienumerable
    IIRC?)
    2). I dont want unnecessary copies of the data (since the object stored
    in the collection are quite large) - is there anything special I need to
    do, to ensure that it is a reference (to the collection) that is passed
    from Methodname() and not a copy of the entire collection?
  • Marc Gravell

    #2
    Re: Collection question

    1: IEnumerable<Two uld be preferable to IEnumerable, to get additional
    type-safety. But easier: just use List<Tor Collection<T- or subclass
    Collection<Tif you want to customise it further.

    2: nothing; both the items and the collection are reference type; all that
    is getting passed around are references.

    Marc


    Comment

    • Pavel Minaev

      #3
      Re: Collection question

      On Jul 8, 3:40 pm, "(2b|!2b)== ?" <void-s...@ursa-major.comwrote:
      I am a relatively new to C#. I want to write a method that returns an
      'iterable' collection:
      >
      TheCollection = MyClass.MethodN ame();
      >
      // Further down, I want to use for .. each
      >
      foreach(MyColle ctionType ct in TheCollection)
      {
          //do something
      >
      }
      >
      The collection will store a specific type (a reference type). Since I
      cannot specify the data returned as a const (e.g. in C++, the method
      signwould look something like this):
      >
      const TheCollection& MyClass::Method Name() const ;
      If you return an internal array or list, and want the collection
      itself to be unmodifiable, wrap it into ReadOnlyCollect ion<T>.
      The return type can still be IEnumerable, if you want to be able to
      change to a different implementation later.
      I have a number of questions:
      >
      1). What Interface should the TheCollection class implement (Ienumerable
      IIRC?)
      If you only want to use it in foreach, then it's IEnumerable<T(y ou
      could get away with just IEnumerable, but generic version is
      preferred).

      Sticking to IEnumerable<Twi ll also let you use iterator block
      ("yield return" and "yield break") in the implementation of your
      method - it often greatly simplifies things.
      2). I dont want unnecessary copies of the data (since the object stored
      in the collection are quite large) - is there anything special I need to
      do, to ensure that it is a reference (to the collection) that is passed
      from Methodname() and not a copy of the entire collection?
      No. When you see a name of a class X used as a type of variable
      (return value, whatever) in C#, you should read it as "reference to
      X".

      Comment

      Working...