Using IEnumerable and GetEnumerator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Magnus.Moraberg@gmail.com

    Using IEnumerable and GetEnumerator

    Hi,

    I have the following class snippet -

    public class ResultsForTestC ases : IEnumerable
    {
    public int Length
    {
    get { return resultsForTestC ases.Length; }
    }

    IEnumerator IEnumerable.Get Enumerator() { return
    resultsForTestC ases.GetEnumera tor(); }
    ResultsForTestC ase[] resultsForTestC ases;
    }

    The problem though is if resultsForTestC ases is null, then
    resultsForTestC ases.GetEnumera tor() and indeed Length will fail. How
    should I handle this.

    Thanks,

    Barry
  • Marc Gravell

    #2
    Re: Using IEnumerable and GetEnumerator

    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 ResultsForTestC ase[] EmptyCases = new
    ResultsForTestC ase[0];
    ResultsForTestC ase[] resultsForTestC ases = EmptyCases;

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

    Marc

    Comment

    Working...