473,395 Members | 1,666 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,395 software developers and data experts.

Using IEnumerable and GetEnumerator

Hi,

I have the following class snippet -

public class ResultsForTestCases : IEnumerable
{
public int Length
{
get { return resultsForTestCases.Length; }
}

IEnumerator IEnumerable.GetEnumerator() { return
resultsForTestCases.GetEnumerator(); }
ResultsForTestCase[] resultsForTestCases;
}

The problem though is if resultsForTestCases is null, then
resultsForTestCases.GetEnumerator() and indeed Length will fail. How
should I handle this.

Thanks,

Barry
Sep 19 '08 #1
1 1566
Well, what do you want it to do? Personally, I suspect the simplest
option is to default the array to an empty array, perhaps one shared
by all instances (to avoid lots of zero-length arrays):

static readonly ResultsForTestCase[] EmptyCases = new
ResultsForTestCase[0];
ResultsForTestCase[] resultsForTestCases = EmptyCases;

Otherwise, you have to check for null and return 0 (for length) and
perhaps an empty enumerator (such as EmptyCases.GetEnumerator()) if it
is null.

Marc
Sep 19 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Benjamin Piorczig | last post by:
Hello list memebers, I'd like to use a C# Collection with from within Visual Basic 6. I have derived a my ComInterface class from IList and implemented those Members. Everything seems to work...
10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
3
by: Adam Clauss | last post by:
I am developing an abstract class which implements IEnumerable<T>. I need the actual implemented methods here to be abstract as well - they will be implemented by MY subclasses. However, I...
3
by: KH | last post by:
I can't seem to figure this one out... I've searched MSDN and Goog, and made my best guesses to no avail,, so help would be much appreciated! public ref class T sealed : public...
1
by: nick_tucker | last post by:
Hi, Could someone tell me which of the following is correct if I want to make the MyCollection class below enumerable public class MyCollection:System.Collections.IEnumerable { private...
5
by: Tin Gherdanarra | last post by:
Dear mpdls, here is a simple example of an IEnumerable that generates integers: It works, but I have only a vague idea of what's going on. I understand that /yield/ wraps the humble integer...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
3
by: rossum | last post by:
I have been trying to get the IEnumerable interface to compile, and am having some difficulties. When I try to compile: class TestIEnum : IEnumerable<string{ string theStrings; public...
2
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return",...
6
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.