C# | Add an object to the end of Collection<T>

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

The number of elements in myColl are : 5
The elements in myColl are : 
A
B
C
D
E

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.