How do you get the index of an element in a List<T>?
Short answer: Use IndexOf(item) or FindIndex(predicate). Example: int index = list.IndexOf(10); int indexByCondition = list.FindIndex(x => x > 100); π C# Dictionary<TKey, TValue> β Interview Questions & Answers
Example code
Use IndexOf(item) or FindIndex(predicate). Example: int index = list.IndexOf(10);
int indexByCondition = list.FindIndex(x => x > 100); π C# Dictionary<TKey, TValue> β Interview Questions & Answers
Real-world example (ShopNest)
In ShopNest, an order page loads products with List<Product> so you can Add items to the cart and access them by index. Use IEnumerable<T> when you only need to loop (for example, printing invoice lines).
Say this in the interview
- Define β one clear sentence (the short answer above).
- Example β relate it to a project like ShopNest or your real work.
- Trade-off β when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png