Answers for "c# get item by index"

C#
15

c# find index element array

int[] arr = { 3, 6, 4, 1, 6, 8 };

// returns 1
Array.IndexOf(arr, 6);
Posted by: Guest on March-04-2020
6

c# list index

// Get the first item from the list

using System.Linq;

var myList = new List<string>{ "Yes", "No", "Maybe"};
var firstItem = myList.ElementAt(0);

// Do something with firstItem
Posted by: Guest on August-13-2020
1

get both item and index in c#

// add this to your namespace
public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source)
{
    return source.Select((item, index) => (item, index));
}

//do something like this

foreach (var (item, index) in collection.WithIndex())
{
    DoSomething(item, index);
}
Posted by: Guest on October-02-2020

C# Answers by Framework

Browse Popular Code Answers by Language