473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Collection question

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
2 1142
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
3
by: JJ | last post by:
Hi, I noticed in a sample app source code that the app made use of a class for example a user class and then had the user objects that got created stuffed into a user collection. I was wondering...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.