Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 253723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:47:09+00:00 2026-05-11T21:47:09+00:00

I have an IEnumerable list of objects in C#. I can use a for

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:47:09+00:00Added an answer on May 11, 2026 at 9:47 pm

    (For the sake of convenience, this answer assumes myList implements IEnumerable<string>; replace string with the appropriate type where necessary.)

    If you’re using .NET 3.5, use the First() extension method:

    string first = myList.First();
    

    If you’re not sure whether there are any values or not, you can use the FirstOrDefault() method which will return null (or more generally, the default value of the element type) for an empty sequence.

    You can still do it “the long way” without a foreach loop:

    using (IEnumerator<string> iterator = myList.GetEnumerator())
    {
        if (!iterator.MoveNext())
        {
            throw new WhateverException("Empty list!");
        }
        string first = iterator.Current;
    }
    

    It’s pretty ugly though 🙂

    In answer to your comment, no, the returned iterator is not positioned at the first element initially; it’s positioned before the first element. You need to call MoveNext() to move it to the first element, and that’s how you can tell the difference between an empty sequence and one with a single element in.

    EDIT: Just thinking about it, I wonder whether this is a useful extension method:

    public static bool TryFirst(this IEnumerable<T> source, out T value)
    {
        using (IEnumerator<T> iterator = source.GetEnumerator())
        {
            if (!iterator.MoveNext())
            {
                value = default(T);
                return false;
            }
            value = iterator.Current;
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a list of objects IEnumerable<MyType> myTypes; Is it possible for me
I have these container objects (let's call them Container) in a list. Each of
I have a List/IEnumerable of objects and I'd like to perform a calculation on
I'm a newbie when it comes to LINQ... I have an IEnumerable generic list
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
Say I have: public ViewResult List() { IEnumerable<IModel> myList = repository.GetMyList(); if(1 == myList.Count())
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I have the following code: List<T> list = new List<T>(); IEnumerable<T> query1 = ...
I have a couple of classes (for this example anyway) that use code first
I have an object graph that looks like this: class A () { int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.