Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the AddRange method in C# lists?
AddRange method in lists adds an entire collection of elements. Let us see an example −
Firstly, set a list in C# and add elements −
List<int> list = new List<int>(); list.Add(100); list.Add(200); list.Add(300); list.Add(400);
Now set an array of elements to be added to the list −
// array of 4 elements int[] arr = new int[4]; arr[0] = 500; arr[1] = 600; arr[2] = 700; arr[3] = 800;
Use the AddRange() method add the entire collection of elements in the list −
list.AddRange(arr);
Now let us see the complete code and display the list −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> list = new List<int>();
list.Add(100);
list.Add(200);
list.Add(300);
list.Add(400);
// array of 4 elements
int[] arr = new int[4];
arr[0] = 500;
arr[1] = 600;
arr[2] = 700;
arr[3] = 800;
list.AddRange(arr);
foreach (int val in list) {
Console.WriteLine(val);
}
}
}Advertisements