Click here to Skip to main content
13,046,533 members (71,440 online)
Rate this:
Please Sign up or sign in to vote.
See more: (untagged)
I need to find a index of Object/item stored in List<string>.
It contains about 250 items.

I used List<t>.findindex method, but i am not able to use Predicate delegate.
I have read many articles but i am not able to understand it.

Please help me finding the item in List<string>.

Here is piece of code.
System.Collections.Generic.List<string> AllRanks = new List<string>();

It contains eg:
1. ABC
2. BBC

Thanks in advance.
Posted 15-May-11 23:00pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Better code would be using anonymous method:

System.Collections.Generic.List<string> list =
    new System.Collections.Generic.List<string>();
int index = list.FindIndex(new System.Predicate<string>(
    (value) => {
        return value == "ABC" || value == "BBC";

prathameshpitale 16-May-11 6:14am
SAKryukov 16-May-11 6:23am
You're very welcome.
Thanks for accepting this answer.
Good luck, call again.
Albin Abel 16-May-11 6:34am
My 5
SAKryukov 16-May-11 6:43am
Thank you, Albin.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

private static bool Findlogic(string item)
 if(item == "ABC" || item == "BBC")
    return true;
    return false

then add you code like
int i = AllRanks.FindIndex(Findlogic);

"i" will give you desired index.
SAKryukov 16-May-11 5:39am
Correct, but... I voted 3 for quite ridiculous "if".
Should be: "return item == "ABC" || item == "BBC";" (!)
(I also fixed your formatting and added missing ";")
prathameshpitale 16-May-11 5:40am
Hey thanks. but can you explain me this ?
Albin Abel 16-May-11 6:34am
My 5
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

How come you did not see it in System.Collections.Generic.List?

You will find a lot of methods including ready-to-use IndexOf (three overloads).

Ahh— predicate… sorry, now I see.

What's the problem about the code sample here:[^]?

prathameshpitale 16-May-11 5:20am
I Saw the links before Questioning on this forum.. I have mentioned there i am not able to understand the given methods in example...

Can you help me understand the codE?
SAKryukov 16-May-11 5:36am
How can I help you? By writing another piece of code? You won't understand it either.
(However, see the sample by Debojyoti Majumder. Any better?)
Maybe you need to learn about 1) generics, 2) delegates, looks at more simple examples first?
prathameshpitale 16-May-11 6:14am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 16 May 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100