Alex Rivera | Logout

C# IEnumerable Retrieve The First Record

Asked 2009-06-05T10:52:45.033
10

I have an IEnumerable list of objects in C#. I can use a for each to loop through and examine each object fine, however in this case all I want to do is examine the first object is there a way to do this without using a foreach loop?

I've tried mylist[0] but that didnt work.

Thanks

Edit
Report

1 Answer

3

Remember, there may be no "first element" if the sequence is empty.

        IEnumerable<int> z = new List<int>();
        int y = z.FirstOrDefault();
answered 2009-06-05T10:55:58.910

Your Answer