Answers for "C# list get next"

C#
1

c# get next item in list

MyList.SkipWhile(item => item.Name != "someName").Skip(1).FirstOrDefault()
Posted by: Guest on November-20-2020
0

C# list get next

List<string> strings = new List<string>() { "s1", "s2", "s3", "s4", "s5" };

// Use this if you're inside of a loop enumerating over the collection
//int index = strings.IndexOf(strings.GetEnumerator().Current);

// Use this if you're finding it manually
string currentString = "s3";
int index = strings.IndexOf(currentString);

string previousString = strings[index - 1];

string nextString = strings[index + 1];

Console.WriteLine("Previous: {0}\nCurrent: {1}\nNext: {2}", previousString, currentString, nextString);

Console.Read();
Posted by: Guest on June-22-2021

C# Answers by Framework

Browse Popular Code Answers by Language