hi there!)
friends, i have deal with IEnumerable interface. At msdn there is an example but it's quite difficult to understand, so if someone can i ask to comment this lines which are in bold -

public class Person
{
    public Person(string fName, string lName)
    {
        this.firstName = fName;
        this.lastName = lName;
    }

    public string firstName;
    public string lastName;
}

public class People : [U]IEnumerable[/U]
{
    private Person[] _people;
    public People(Person[] [B]pArray[/B])
    {
        _people = new Person[pArray.Length];

       [B] for (int i = 0; i < pArray.Length; i++)
        {
            _people[i] = pArray[i];
        }[/B]
    }

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

    public PeopleEnum GetEnumerator()
    {
        return new PeopleEnum(_people);
    }[/B]
}

public class PeopleEnum : IEnumerator
{ }
the sourse (MSDN)

2) what is pArray?)
big thank you in advance!)

Recommended Answers

All 7 Replies

pArray is an array of custom object, in your case the Person is a class.
Array of cusom class means you have (will have) more then one Person. If there is more then one, you need to put them into an array for having them together.
Mitja

commented: ++++++++++ +1

pArray is short for personArray I guess.
pArray stands indeed for an array of Person.
You can pass an array of Person to the constructor of the People class.
Because the People class inherits from the IEnumerable interface it has to implement its methods.

commented: ++++++++++++ +1

ok. thank you , Mitja and ddanbe but why we need this piece (of code)) -

IEnumerator IEnumerable.GetEnumerator()
    {
       return (IEnumerator) GetEnumerator();
    }

what is this (I can't understand syntax) -

IEnumerator IEnumerable.GetEnumerator()

what is this (I can't understand syntax) -

IEnumerator IEnumerable.GetEnumerator()

When something implements IEnumerable it needs a method called GetEnumerator that returns and IEnumerator object. This object is used by foreach and LINQ method to iterate through the collection.

The syntax of IEnumerable.GetEnumerator() is what as known as an explicit implementation of the interface. The main reason to do this is when you inherit two interfaces that declare the same method. You need to tell the system which one you are implementing. It also means that you can only access that member through an IEnumerable object, not an object of the class. There is a tutorial on the MSDN site here.

commented: +++++++ +1

Momerath, thanks for the detailed explanation!

I don't understand - if my class inherits from IEnumerable - how should describe such class? as i understand i should implement two methods - one of them is "<T>".....

I need to create a generic class that implements the "mathematical set" based on an array ..... should be able to work with foreach ......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.