By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Collection question

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

TheCollection = MyClass.MethodName();

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

foreach(MyCollectionType 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::MethodName() 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?
Jul 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
1: IEnumerable<Twould 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
Jul 8 '08 #2

P: n/a
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.MethodName();

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

foreach(MyCollectionType 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::MethodName() const ;
If you return an internal array or list, and want the collection
itself to be unmodifiable, wrap it into ReadOnlyCollection<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(you
could get away with just IEnumerable, but generic version is
preferred).

Sticking to IEnumerable<Twill 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".
Jul 9 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.