gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Get the index of value in the list using C#
- User
Get the index of value in the list using C#
- Assistant
To get the index of a value in a list using C#, you can use the
IndexOfmethod. Here's an example:using System; using System.Collections.Generic; class Program { static void Main() { // Create a list of integers List<int> numbers = new List<int>() { 10, 20, 30, 40, 50 }; // Get the index of a value in the list int index = numbers.IndexOf(30); // Check if the value was found and print the index if (index != -1) { Console.WriteLine("Index of value 30: " + index); } else { Console.WriteLine("Value 30 not found in the list"); } } }In this example, the
IndexOfmethod is called on thenumberslist with the value30as a parameter. It returns the index of the first occurrence of the value in the list, or -1 if the value is not found. We then check if the value was found by comparing the index to -1 and print the result accordingly.