Add new item to List object C#

The C# List is a dynamic data structure that allows you to add, remove, and access elements based on their index.

Following are some ways to add items to a C# List:

  1. Add method
  2. AddRange method
  3. Insert method
  4. InsertRange method
  5. List initializer syntax

Using the Add method

The Add method is the simplest way to add items to a C# List. It adds an item to the end of the list.

List<int> numbers = new List<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(3);

Using the AddRange method

The AddRange method allows you to add a collection of items to a C# List. It takes an IEnumerable as an argument.

List<int> numbers = new List<int>(); List<int> newNumbers = new List<int> { 1, 2, 3 }; numbers.AddRange(newNumbers);

Using the Insert method

The Insert method allows you to add an item at a specific index in a C# List. It takes two arguments: the index where the item should be inserted and the item itself.

List<int> numbers = new List<int> { 1, 3, 4 }; numbers.Insert(1, 2);

Using the InsertRange method

The InsertRange method allows you to add a collection of items at a specific index in a C# List. It takes two arguments: the index where the items should be inserted and the collection of items itself.

List<int> numbers = new List<int> { 1, 4 }; List<int> newNumbers = new List<int> { 2, 3 }; numbers.InsertRange(1, newNumbers);

Using the List initializer syntax

The List initializer syntax allows you to add items to a C# List when it is initialized.

List<int> numbers = new List<int> { 1, 2, 3 };

These are all the possible methods to add items to a C# List. Choose the one that best suits your needs based on the context of your code.

Possible Exceptions | C# list.add

In C#, when adding items to a List, there are several exceptions that may occur depending on the method used and the specific scenario.


how to add new item to C# list

Following are some of the most common exceptions that can occur when adding items to a List in C#:

ArgumentNullException:

This exception occurs when you try to add a null item to the List.

List<string> names = new List<string>(); string name = null; names.Add(name); // throws ArgumentNullException

To avoid this exception, you can check if the item is null before adding it to the List:

if (name != null) { names.Add(name); }

ArgumentOutOfRangeException:

This exception occurs when you try to insert an item at an index that is out of range.

List<int> numbers = new List<int> { 1, 2, 3 }; numbers.Insert(5, 4); // throws ArgumentOutOfRangeException

To avoid this exception, you can check if the index is within the range of the List before inserting the item:

if (index >= 0 && index < numbers.Count) { numbers.Insert(index, item); }

NotSupportedException:

This exception occurs when you try to add or insert an item to a List that is read-only or has a fixed size.

List<int> numbers = new List<int> { 1, 2, 3 }.AsReadOnly(); numbers.Add(4); // throws NotSupportedException

To avoid this exception, you can create a new List that is not read-only or has a variable size, and then add or insert items to it:

List<int> newNumbers = new List<int>(numbers); newNumbers.Add(4);

ArgumentException:

This exception occurs when you try to add an item to a List that has a duplicate value and the List does not allow duplicates.

List<int> numbers = new List<int> { 1, 2, 3 }; numbers.Add(2); // throws ArgumentException

To avoid this exception, you can use a HashSet instead of a List if you need to store unique values.