What is the AddRange method in C# List?

王林
Release: 2023-09-05 12:41:02
forward
1719 people have browsed it

C# 列表中的 AddRange 方法是什么?

The AddRange method in the list adds the entire collection of elements. Let us see an example -

First, set up 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);
Copy after login

Now set the 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;
Copy after login

Use AddRange() method adds the entire collection of elements to the list −

list.AddRange(arr);
Copy after login

Now let’s 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);
      }
   }
}
Copy after login

The above is the detailed content of What is the AddRange method in C# List?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!