how to add elements to a list in c# code example

Example 1: add all elements in a list c#

using System.Collections.Generic;
using System.Linq;

List<int> intList = new List<int>(){1, 2, 3, 4};
int sum = intList.Aggregate((x, y) => x + y);

Example 2: how to add to a list c#

var list = new List<string>();
list.Add("Hello");

Example 3: c# adding to a list

//Declaring that its a list
List<string> test = new List<string>(); 
test.Add("Hello")

Example 4: c list add element

// Create a list  
List<string> AuthorList = new List<string>();  
  
// Add items using Add method   
AuthorList.Add("Mahesh Chand");

Example 5: c# add list to list

List<string> initialList = new List<string>();
// Put whatever you want in the initial list
List<string> listToAdd = new List<string>();
// Put whatever you want in the second list
initialList.AddRange(listToAdd);

Tags:

C Example