Addrange trong C# là gì

CsharpProgrammingServer Side Programming

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 list = new List[]; 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 list = new List[];       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];       }    } }

Published on 13-Aug-2018 06:14:13

Hôm trc đc thầy giáo demo về cái hàm AddRange[] mà chưa hiểu gì nhiều. Hôm nay lên mạng và tìm đc tài liệu khá hay, diễn tả rất dễ hiểu về hàm AddRange[] nên ki cóp nhặt nhạnh lại về đây để dễ học tập 😀

Hiểu đơn giản thôi, nó là cái hàm bạn có thể add 1 lúc đc nhiều cotrol[giống nhau] vào 1 chỗ nào đó giống nhau.

vd: Add nhiều cotrol button vào 1 form chẳng hạn, hay là add nhiều dữ liệu vào 1 cơ sở dữ liệu, dùng hàm này thì nhanh lém. Dưới đây là những gì nhặt nhạnh được 😀 từ //www.dataschenk.com/Tips

Write fewer lines of code when adding dynamic controls to a form
using the AddRange[] method [VB .NET]

Typically, when you add multiple controls to a form dynamically,
you add each control one by one, like so:

Dim button1 As New System.Windows.Forms.Button Dim button2 As New System.Windows.Forms.Button

Dim button3 As New System.Windows.Forms.Button

Me.Controls.Add[button1] Me.Controls.Add[button2] Me.Controls.Add[button3] However, in VB .NET, you can save a few keystrokes by using the AddRange[] method. This method allows you to add an array of controls to the collection instead of just one control at a time.

The syntax for the AddRange[] method is as follows:

AddRange[ObjectArray[]]

where the ObjectArray[] argument holds an array of Control objects. When you use this method, you can add the same three Button objects to the Controls collection using the following line of code:

Me.Controls.AddRange[New Control[] {button1, button2, button3}]

C# List class represents a collection of a type in C#. List.Add[], List.AddRange[], List.Insert[], and List.InsertRange[] methods are used to add and insert items to a List.

AddRange - AddRange adds an entire collection of elements. It can replace tedious foreach-loops that repeatedly call Add on List.

public virtual void AddRange [System.Collections.ICollection c];

Add - Add method adds an object to the end of the List.

public virtual int Add [object value];

Example: 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 list = new List[]; list.AddRange[arr];

But if you want to use List.Add[] method,

List list = new List[]; list.Add[100]; list.Add[200]; list.Add[300]; list.Add[400];

For details, you can check Insert an Item into a C# List

Lớp List - ArrayList trong C# biểu diễn một tập hợp được sắp xếp của một đối tượng mà có thể được lập chỉ mục cho từng item riêng rẽ.

Về cơ bản, nó là một sự thay thế cho một mảng. Tuy nhiên, không giống như trong mảng, bạn có thể thêm và gỡ bỏ các item từ một list tại một vị trí đã xác định bởi sử dụng một chỉ mục và mảng chính nó có thể tự điều chỉnh kích cỡ một cách tự động. Nó cũng cho phép cấp phát bộ nhớ động, thêm, tìm kiếm và sắp xếp các item trong một list.

Phương thức và Thuộc tính của lớp ArrayList trong C#

Dưới đây là các thuộc tính được sử dụng phổ biến của lớp ArrayList trong C#:

Thuộc tính Miêu tả
Capacity Lấy hoặc thiết lập số phần tử mà ArrayList có thể chứa
Count Lấy số phần tử thực sự được chứa trong ArrayList
IsFixedSize Lấy một giá trị chỉ rằng có hay không ArrayList là có một kích cỡ cố định
IsReadOnly Lấy một giá trị chỉ rằng có hay không ArrayList là read-only
Item Lấy hoặc thiết lập phần tử tại chỉ mục đã xác định

Dưới đây là các phương thức được sử dụng phổ biến của lớp ArrayList trong C#:

STT Phương thức
1 public virtual int Add[object value];

Thêm một đối tượng vào phần cuối của ArrayList

2 public virtual void AddRange[ICollection c];

Thêm các phần tử của một ICollection vào phần cuối của ArrayList

3 public virtual void Clear[];

Gỡ bỏ các phần tử từ ArrayList đó

4 public virtual bool Contains[object item];

Xác định có hay không một phần tử là nằm trong ArrayList

5 public virtual ArrayList GetRange[int index, int count];

Trả về một ArrayList mà biểu diễn một tập con của các phần tử trong ArrayList nguồn

6 public virtual int IndexOf[object];

Trả về chỉ mục [dựa trên cơ sở 0] của lần xuất hiện đầu tiên của một giá trị trong ArrayList hoặc một phần của nó

7 public virtual void Insert[int index, object value];

Chèn một phần tử vào ArrayList tại chỉ mục đã xác định

8 public virtual void InsertRange[int index, ICollection c];

Chèn một phần tử của một collection vào ArrayList tại chỉ mục đã xác định

9 public virtual void Remove[object obj];

Gỡ bỏ lần xuất hiện đầu tiên của một đối tượng đã xác định tại chỉ mục đã xác định

10 public virtual void RemoveAt[int index];

Gỡ bỏ phần tử tại chỉ mục đã xác định của ArrayList

11 public virtual void RemoveRange[int index, int count];

Gỡ bỏ một dãy phần tử từ ArrayList

12 public virtual void Reverse[];

Đảo ngược thứ tự phần tử trong ArrayList

13 public virtual void SetRange[int index, ICollection c];

Sao chép các phần tử của một collection qua một dãy các phần tử trong ArrayList

14 public virtual void Sort[];

Sắp xếp các phần tử trong ArrayList

15 public virtual void TrimToSize[];

Thiết lập capacity tới số phần tử thực sự trong ArrayList

Ví dụ

Dưới đây là ví dụ minh họa các thuộc tính và phương thức của lớp ArrayList trong C#:

using System; using System.Collections; namespace VietJackCsharp { class TestCsharp { static void Main[string[] args] { Console.WriteLine["Vi du minh hoa ArrayList trong C#"]; Console.WriteLine["---------------------------------"]; ArrayList al = new ArrayList[]; Console.WriteLine["\nChen mot vai phan tu vao ArrayList: "]; al.Add[45]; al.Add[78]; al.Add[33]; al.Add[56]; al.Add[12]; al.Add[23]; al.Add[9]; Console.WriteLine["\nDung luong cua ArrayList: {0} ", al.Capacity]; Console.WriteLine["So phan tu trong ArrayList: {0}", al.Count]; Console.WriteLine["\nHien thi cac phan tu trong ArrayList: "]; foreach [int i in al] { Console.Write[i + " "]; } Console.WriteLine[]; Console.WriteLine["\nSap xep va hien thi ArrayList sau khi da sap xep: "]; al.Sort[]; foreach [int i in al] { Console.Write[i + " "]; } Console.WriteLine[]; Console.ReadKey[]; } } }

Nếu bạn không sử dụng lệnh Console.ReadKey[]; thì chương trình sẽ chạy và kết thúc luôn [nhanh quá đến nỗi bạn không kịp nhìn kết quả]. Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.

Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:

Đã có app VietJack trên điện thoại, giải bài tập SGK, SBT Soạn văn, Văn mẫu, Thi online, Bài giảng....miễn phí. Tải ngay ứng dụng trên Android và iOS.

Theo dõi chúng tôi miễn phí trên mạng xã hội facebook và youtube:

Các bạn có thể mua thêm khóa học JAVA CORE ONLINE VÀ ỨNG DỤNG cực hay, giúp các bạn vượt qua các dự án trên trường và đi thực tập Java. Khóa học có giá chỉ 300K, nhằm ưu đãi, tạo điều kiện cho sinh viên cho thể mua khóa học.

Nội dung khóa học gồm 16 chuơng và 100 video cực hay, học trực tiếp tại //www.udemy.com/tu-tin-di-lam-voi-kien-thuc-ve-java-core-toan-tap/ Bạn nào có nhu cầu mua, inbox trực tiếp a Tuyền, cựu sinh viên Bách Khoa K53, fb: //www.facebook.com/tuyen.vietjack

Follow facebook cá nhân Nguyễn Thanh Tuyền //www.facebook.com/tuyen.vietjack để tiếp tục theo dõi các loạt bài mới nhất về Java,C,C++,Javascript,HTML,Python,Database,Mobile.... mới nhất của chúng tôi.

collection_trong_csharp.jsp

Video liên quan

Chủ Đề