C# | Add an object to the end of Collection<T> Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Collection<T>.Add(T) method is used to add an object to the end of the Collection<T>. Syntax : public void Add (T item); Here, item is the object to be added to the end of the Collection<T>. The value can be null for reference types. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to add an object to // the end of the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection<string> myColl = new Collection<string>(); myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("E"); // Displaying the number of elements in Collection Console.WriteLine("The number of elements in myColl are : " + myColl.Count); // Displaying the elements in Collection Console.WriteLine("The elements in myColl are : "); foreach(string str in myColl) { Console.WriteLine(str); } } } Output: The number of elements in myColl are : 5 The elements in myColl are : A B C D E Example 2: CSHARP // C# code to add an object to // the end of the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection<int> myColl = new Collection<int>(); myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // Displaying the number of elements in Collection Console.WriteLine("The number of elements in myColl are : " + myColl.Count); // Displaying the elements in Collection Console.WriteLine("The elements in myColl are : "); foreach(int i in myColl) { Console.WriteLine(i); } } } Output: The number of elements in myColl are : 4 The elements in myColl are : 2 3 4 5 Note: Collection<T> accepts null as a valid value for reference types and allows duplicate elements. This method is an O(1) operation. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.add?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Add an object to the end of Collection<T> S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections.ObjectModel-Namespace CSharp-Collection-Class Similar Reads C# | Add an object to the end of the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Add(Object) method adds an object to the end of the ArrayList. Pr 2 min read C# | Copying the Collection<T> elements to an array Collection<T>.CopyTo(T[], Int32) method is used to copy the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : The one-dimensional Array that is the destin 3 min read C# | Remove all elements from the Collection<T> Collection<T>.Clear method is used to remove all elements from the Collection<T>. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all // elements from the Collection using System; using S 2 min read C# | Adding the elements of the specified collection to the end of the List List<T>.AddRange(IEnumerable<T>) Method is used to add the elements of the specified collection to the end of the List<T>. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for refer 3 min read C# | Get an enumerator that iterates through Collection<T> Collection<T>.GetEnumerator Method is used to get an enumerator that iterates through the Collection<T>. Syntax: public System.Collections.Generic.IEnumerator<T> GetEnumerator (); Return Value: This method returns an IEnumerator<T> for the Collection<T>. Below programs 2 min read Like