What is a generic List in C#?


Generic List<T> is a generic collection in C#. The size can be dynamically increased using List, unlike Arrays.

Let us see an example −

We have set the List first −

List<string> myList = new List<string>()

Now add elements in the list −

List<string> myList = new List<string>() {
   "mammals",
   "reptiles",
   "amphibians"
}

Now using a property let us count the number of elements added −

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      List<string> myList = new List() {
         "mammals",
         "reptiles",
         "amphibians"
      };
      Console.WriteLine(myList.Count);
   }
}

Updated on: 20-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements