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 7008397
In Process

The Archive Base Latest Questions

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

IndexOf is returning 1 for the last element of the list. Why? This code

  • 0

IndexOf is returning 1 for the last element of the list. Why?

This code is not used by me . I am just giving you guys an idea about my asked context through the code mentioned below
E.g. List:

 List<int> abc = new List<int>();
 abc.Add(1);
 abc.Add(2);
 abc.Add(41);
// i.e index 0 hold 1 index 1 hold 2 and index 3 hold 41
//abc[0] = 1;
//abc[1] = 2;
//abc[3] = 41;

then checking index like:

foreach (int i in abc)
{
    //for first two index[0,1] IndexOf() works fine but when foreach loop hole i=41       then IndexOf() returns 1 i.e is index=1 why??
    int index = abc.IndexOf(i);
}
  • 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-27T21:43:48+00:00Added an answer on May 27, 2026 at 9:43 pm

    List.IndexOf() method returns first occurrence of element (item) within the list if an element is found. If your list contains duplicate element then you may continue your search into the list using List.IndexOf(item,index).

        var abc = new List<int>();
        abc.Add(1);
        abc.Add(2);
        abc.Add(41);
        abc.Add(2);
        int index=-1;
        foreach (int i in abc)
        {
            index = abc.IndexOf(i,index+1);
            Console.WriteLine(index);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following Java code but List.indexOf() seems to do pretty much the
I found this post: Python: finding an element in an array and it's about
I'm confused as to why .index() is returning 0 in this code. Shouldn't it
I have this setup, just to find the index() of an element, but it
In Titanium mobile indexOf function of String is not working properly. console.log( test2.indexOf( /tes/i
Is there any equivalent of String.indexOf() for arrays? If not, is there any faster
searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == - 1) return;
Quick question, indexof() find the first occur position of the string character? what about
My code is function getID( swfID ){ if(navigator.appName.indexOf(Microsoft) != -1){ me = window[swfID]; }else{
I'm curious why the String.indexOf is returning a 0 (instead of -1) when asking

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.