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.
  1. 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.
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApp1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. // Create a list of strings
  10. List<string> AuthorList = new List<string>();
  11. AuthorList.Add("Mahesh Chand");
  12. AuthorList.Add("Praveen Kumar");
  13. AuthorList.Add("Raj Kumar");
  14. AuthorList.Add("Nipun Tomar");
  15. AuthorList.Add("Dinesh Beniwal");
  16. // Get first item of a List
  17. string auth = AuthorList[0];
  18. Console.WriteLine(auth);
  19. Console.WriteLine("-------------");
  20. // Set first item of a List
  21. AuthorList[0] = "New Author";
  22. foreach (var author in AuthorList)
  23. {
  24. Console.WriteLine(author);
  25. }
  26. }
  27. }
  28. }
The output from above code listing is shown in below figure.
Next >> C# List Tutorial