Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add integer values to a C# list?
To add integer values to a List<int> in C#, you can use several methods including Add(), AddRange(), and collection initializer syntax. The Add() method is the most common way to add individual values one at a time.
Syntax
Following is the syntax for declaring an integer list and adding values −
List<int> listName = new List<int>(); listName.Add(value);
To add multiple values at once using AddRange() −
listName.AddRange(new int[] { value1, value2, value3 });
Using collection initializer syntax −
List<int> listName = new List<int> { value1, value2, value3 };
Using Add() Method
The Add() method appends a single integer to the end of the list −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<int> list1 = new List<int>();
list1.Add(900);
list1.Add(400);
list1.Add(300);
Console.WriteLine("List contains " + list1.Count + " elements:");
foreach (int value in list1) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
List contains 3 elements: 900 400 300
Using AddRange() Method
The AddRange() method adds multiple integers at once from an array or another collection −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<int> list1 = new List<int>();
list1.AddRange(new int[] { 100, 200, 300 });
List<int> moreNumbers = new List<int> { 400, 500 };
list1.AddRange(moreNumbers);
Console.WriteLine("List after AddRange operations:");
foreach (int value in list1) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
List after AddRange operations: 100 200 300 400 500
Using Collection Initializer
You can initialize a list with values at the time of declaration using collection initializer syntax −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
// You can still add more values after initialization
numbers.Add(60);
numbers.Add(70);
Console.WriteLine("Numbers in the list:");
for (int i = 0; i < numbers.Count; i++) {
Console.WriteLine($"Index {i}: {numbers[i]}");
}
}
}
The output of the above code is −
Numbers in the list: Index 0: 10 Index 1: 20 Index 2: 30 Index 3: 40 Index 4: 50 Index 5: 60 Index 6: 70
Comparison of Methods
| Method | Use Case | Performance |
|---|---|---|
Add() |
Adding single values one at a time | Good for individual additions |
AddRange() |
Adding multiple values from arrays or collections | More efficient for bulk additions |
| Collection Initializer | Setting initial values at declaration | Most readable for known values |
Conclusion
Adding integer values to a C# list can be done using Add() for individual values, AddRange() for multiple values, or collection initializer syntax for initial values. Choose the method that best fits your specific use case and coding style preferences.
