C# List<T> class provides methods and properties to create a list of objects (classes).
List is a generic class. You must import the following namespace before using the List<T> class.
- using System.Collections.Generic;
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.
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- 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");
-
-
- string auth = AuthorList[0];
- Console.WriteLine(auth);
- Console.WriteLine("-------------");
-
- AuthorList[0] = "New Author";
- foreach (var author in AuthorList)
- {
- Console.WriteLine(author);
- }
- }
- }
- }
The output from above code listing is shown in below figure.