Difference between AddRange and RemoveRange in C#
AddRange :
List<T>.AddRange Method
List<T>.AddRange Method
Adds the elements of the specified collection to the end of the List<T>.
If you are retrieving records from database and want to add to current array list , then it will help to add range to current record array.
Example :
string[] oldinput= { "Nihar",
"technical",
"Funda" };
string[] newInput = { "Blog",
"spot",
".com" };
List<string> oldList = new List<string>();
oldList.AddRange(oldinput);
foreach (string ip in oldList)
{
Console.WriteLine(ip);
}
oldList.AddRange(newInput);
Console.WriteLine("\n ### New members added ####");
foreach (string ip in oldList)
{
Console.WriteLine(ip);
}
Output :
Nihar
technical
Funda
### New members added ####
Nihar
technical
Funda
Blog
spot
.com
RemoveRange :
Syntax :
Take reference of above output and continue it....
oldList.RemoveRange(2, 2);
Console.WriteLine("\n ### Remove members ####");
foreach (string ip in oldList)
{
Console.WriteLine(ip);
}
Output :
### Remove members ####
Nihar
technical
spot
.com
If you are retrieving records from database and want to add to current array list , then it will help to add range to current record array.
Example :
string[] oldinput= { "Nihar",
"technical",
"Funda" };
string[] newInput = { "Blog",
"spot",
".com" };
List<string> oldList = new List<string>();
oldList.AddRange(oldinput);
foreach (string ip in oldList)
{
Console.WriteLine(ip);
}
oldList.AddRange(newInput);
Console.WriteLine("\n ### New members added ####");
foreach (string ip in oldList)
{
Console.WriteLine(ip);
}
Output :
Nihar
technical
Funda
### New members added ####
Nihar
technical
Funda
Blog
spot
.com
RemoveRange :
Removes a range of elements from the List<T>.
Syntax :
public void RemoveRange(int index,int count)
Take reference of above output and continue it....
oldList.RemoveRange(2, 2);
Console.WriteLine("\n ### Remove members ####");
foreach (string ip in oldList)
{
Console.WriteLine(ip);
}
Output :
### Remove members ####
Nihar
technical
spot
.com
Comments
Post a Comment