How to get and set an item in a C# List

List.Item

The Item property gets and sets the value associated with the specified index.

The following code snippet gets and sets the first item in a list.
 
// Set Item value
AuthorList["Mahesh Chand"] = 20;
// Get Item value
Int16 age = Convert.ToInt16(AuthorList["Mahesh Chand"]);

// Create a list of strings
List<string> AuthorList = new List<string>();
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Praveen Kumar");
AuthorList.Add("Raj Kumar");
AuthorList.Add("Nipun Tomar");
AuthorList.Add("Dinesh Beniwal");

// Get first item of a List
string auth = AuthorList[0];
Console.WriteLine(auth);
// Set first item of a List
AuthorList[0] = "New Author";
           

Download Free book: Programming List with C#