C# Programming Language: How to Add Elements Into a List

Jesse L
2 min readJul 8, 2022

Hi everyone, welcome back. In this example, we will be going over how to add elements into a list in C#. Lists can be used to store a collection of data, similar to that of an array. The differences being that the lists provide more functionality compared to that of an array, which leaves it as a great option when wanting to modify a collection of data. With this introduction out of the way, let’s get into it.

Creating List

Let’s start by creating a list. Before we can create the list, we need to make sure that we have our list import, which is included within the System.Collections.Generic package:

using System.Collections.Generic;

Now, we can create our list:

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

We have now successfully create our list.

Adding Elements Into The List

Now, let’s try to add elements into our list. We can do this by using the built in Add() function that is provided. Let’s see an example:

List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
Console.WriteLine(String.Join(" ", myList));
Output:
One Two Three

In the example above, we can see that we have added a few elements into our list by using the Add() function. We can verify that they were successfully added by printing our list and checking the output.

Conclusion

That is how we can add elements into a list in C#. We can do so by using the Add() function. I hope this helps. If you have any questions or comments, please let me know. Thanks for reading.

--

--

Jesse L
Jesse L

Written by Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.

No responses yet