Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

create
list
c#

How to create a list in C#

Educative Answers Team

A list is a collection of elements of the same data type that provides functionalities like indexing, searching, sorting, and manipulating list elements.

The List<T> class is defined in the System.Collections.Generic namespace; it is a generic class and can store elements of ​any data type.

svg viewer

Syntax

Have a look at the syntax used to call the List<T> constructor to create a list:

List<T> my_list = new List<T>();

T can be any data type (e.g., string, int, etc.)

Code

The code snippet below illustrates the process of creating a list in C#:

using System.Collections.Generic;

class HelloWorld
{
    static void Main()
    {
      // Creating a list of strings
      List<string> Fruits = new List<string>();

      // Adding elements to the Fruits list
      Fruits.Add("Apple");    
      Fruits.Add("Banana");
      Fruits.Add("Mango");     

      // Printing the list
      Fruits.ForEach(System.Console.WriteLine);
    }
}

RELATED TAGS

create
list
c#
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring