DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Adding Two Hours in DataWeave: Mule 4
  • Java String: A Complete Guide With Examples

Trending

  • Scaling Cloud Data Automation: A Practical Guide to Open Table Formats
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • From Data Movement to Local Intelligence: The Shift from Centralized to Federated AI
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  1. DZone
  2. Data Engineering
  3. Data
  4. LINQ - AddRange Method in C#

LINQ - AddRange Method in C#

By 
Jalpesh Vadgama user avatar
Jalpesh Vadgama
·
Nov. 03, 13 · Interview
Likes (2)
Comment
Save
Tweet
Share
62.4K Views

Join the DZone community and get the full member experience.

Join For Free

In this post I’m going to explain you about Linq AddRange method. This method is quite useful when you want to add multiple elements to a end of list. Following is a method signature for this.

public void AddRange(
    IEnumerable<T> collection
)

To Understand let’s take simple example like following.

using System;
using System.Collections.Generic;

namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names=new List<string> {"Jalpesh"};
            string[] newnames=new string[]{"Vishal","Tushar","Vikas","Himanshu"};
            foreach (var newname in newnames)
            {
                names.Add(newname);
            }
            foreach (var n in names)
            {
                Console.WriteLine(n);
            }
        }
    }
}

Here in the above code I am adding content of array to a already created list via foreach loop. You can use AddRange method instead of for loop like following.It will same output as above.

using System;
using System.Collections.Generic;

namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names=new List<string> {"Jalpesh"};
            string[] newnames=new string[]{"Vishal","Tushar","Vikas","Himanshu"};
            names.AddRange(newnames);
            foreach (var n in names)
            {
                Console.WriteLine(n);
            }
        }
    }
}

Now when you run that example output is like following.

AddRangeLinqCsharp

Add Range in more complex scenario:

You can also use add range to more complex scenarios also like following.You can use other operator with add range as following.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names=new List<string> {"Jalpesh"};
            string[] newnames=new string[]{"Vishal","Tushar","Vikas","Himanshu"};
            names.AddRange(newnames.Where(nn=>nn.StartsWith("Vi")));
            foreach (var n in names)
            {
                Console.WriteLine(n);
            }
        }
    }
}

Here in the above code I have created array with string and filter it with where operator while adding it to an existing list. Following is output as expected.

AddRangeLinqCsharpAdvanceScneario

That’s it. Hope you like it. Stay tuned for more..

Data structure Operator (extension) Filter (software) POST (HTTP) Strings Data Types Element

Published at DZone with permission of Jalpesh Vadgama. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Adding Two Hours in DataWeave: Mule 4
  • Java String: A Complete Guide With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook