C# | Add an object to the end of Collection<T> (original) (raw)
Last Updated : 01 Feb, 2019
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:
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 elements to an array Collection.CopyTo(T[], Int32) method is used to copy the entire Collection 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 Collection.Clear method is used to remove all elements from the Collection. 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.AddRange(IEnumerable) Method is used to add the elements of the specified collection to the end of the List. 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 Collection.GetEnumerator Method is used to get an enumerator that iterates through the Collection. Syntax: public System.Collections.Generic.IEnumerator GetEnumerator (); Return Value: This method returns an IEnumerator for the Collection. Below programs 2 min read
- C# | Add an object to the end of the Queue - Enqueue Operation Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called dequeue. Queue.Enqueue(T) Method is used to add an object to the end 3 min read
- Object and Collection Initializer in C# An object and collection initializer is an interesting and very useful feature of C# language. This feature provides a different way to initialize an object of a class or a collection. This feature is introduced in C# 3.0 or above. The main advantages of using these are to makes your code more reada 3 min read
- C# | Insert an element into Collection at specified index Collection.Insert(Int32, T) method is used to insert an element into the Collection at the specified index. Syntax: public void Insert (int index, T item); Parameters: index : The zero-based index at which item should be inserted. item : The object to insert. The value can be null 3 min read
- C# | Get or set the element at specified index in Collection Collection.Item[Int32] property is used to get or set the element at the specified index. Syntax: public T this[int index] { get; set; } Here, index is the zero-based index of the element to get or set. Return Value: The element at the specified index. Exception: This method will give Argum 3 min read
- C# | Adding an element to the List List.Add(T) Method is used to add an object to the end of the List. 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 reference types and it also allows duplicate elements. If the Count becomes eq 2 min read