
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Adding the elements of the specified collection to the end of the List in C#
To add elements of the specified collection to the end of the List, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ List<string> list = new List<string>(); list.Add("Andy"); list.Add("Gary"); list.Add("Katie"); list.Add("Amy"); Console.WriteLine("Elements in List..."); foreach (string res in list){ Console.WriteLine(res); } string[] strArr = { "John", "Jacob" }; list.AddRange(strArr); Console.WriteLine("Elements in List...UPDATED"); foreach(String str in list){ Console.WriteLine(str); } } }
Output
This will produce the following output −
Elements in List... Andy Gary Katie Amy Elements in List...UPDATED Andy Gary Katie Amy John Jacob
Example
Let us now see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ List<int> list = new List<int>(); list.Add(25); list.Add(50); list.Add(75); list.Add(100); Console.WriteLine("Elements in List..."); foreach (int val in list){ Console.WriteLine(val); } int[] intArr = { 150, 200, 250, 300 }; list.AddRange(intArr); Console.WriteLine("Elements in List...UPDATED"); foreach(int val in list){ Console.WriteLine(val); } } }
Output
This will produce the following output −
Elements in List... 25 50 75 100 Elements in List...UPDATED 25 50 75 100 150 200 250 300
- Related Articles
- Adding elements to the end of the ArrayList in C#
- How to insert the elements of a collection into the List at the specified index in C#?
- C# Program to return specified number of elements from the end of an array
- Check if SortedSet and the specified collection contain the same elements in C#
- Check if HashSet and the specified collection contain the same elements in C#
- Reverse the order of the elements in the entire List or in the specified range in C#
- Adding an element at the end of the array in Javascript
- Add an object to the end of Collection in C#
- Searching the index of specified object in Collection in C#
- Insert all elements of other Collection to Specified Index of Java ArrayList
- How to get all elements of a List that match the conditions specified by the predicate in C#?
- Java Program to Shuffle the Elements of a Collection
- Java Program to insert all elements of other Collection to specified Index of ArrayList
- Adding new node or value at the end of LinkedList in C#
- Shifting certain elements to the end of array JavaScript

Advertisements