C# | Collection Class - GeeksforGeeks

01 Feb, 2019
Collection<T> Class provides the base class for a generic collection. Here T is the type of elements in the collection. This class comes under the System.Collections.ObjectModel namespace.
Characteristics:
The Collection<T> class can be used immediately by creating an instance of one of its constructed types.
The Collection<T> class provides protected methods that can be used to customize its behavior when adding and removing items, clearing the collection, or setting the value of an existing item.
Most Collection<T> objects can be modified. However, a Collection object that is initialized with a read-only IList<T> object cannot be modified.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
Collection<T> accepts null as a valid value for reference types and allows duplicate elements.
Example:
// C# code to create a Collectionusing 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>();          // Adding elements in Collection myColl        myColl.Add(2);        myColl.Add(3);        myColl.Add(4);        myColl.Add(5);          // Displaying the elements in myColl        foreach(int i in myColl)        {            Console.WriteLine(i);        }    }}
Output:
2
3
4
5

Example:
// C# Code to illustrate the // Properties of Collection classusing 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>();           // Adding elements in Collection myColl        myColl.Add("A");        myColl.Add("B");        myColl.Add("C");        myColl.Add("D");        myColl.Add("E");                  // ------- Count Property ----------                  // To print the count of        // elements in Collection        Console.WriteLine("Count : " + myColl.Count);           // -------- Item[Int32] Property --------                  // Get the element at index 2        Console.WriteLine("Element at index 2 is : " + myColl[2]);           // Get the element at index 3        Console.WriteLine("Element at index 3 is : " + myColl[3]);    }}
Output:
Count : 5
Element at index 2 is : C
Element at index 3 is : D

Example:
// C# code to check if an// element is in the Collectionusing 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");          // Checking if an element is in the Collection        // The function returns "True" if the        // item is present in Collection        // else returns "False"        Console.WriteLine(myColl.Contains("A"));    }}
Output:
True

Example 2:
// C# code to copy the entire Collection// to a compatible one-dimensional Array,// starting at the specified index of// the target arrayusing 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");          // Creating a string array        string[] myArr = new string[myColl.Count];          // Copying the entire Collection to a        // compatible one-dimensional Array,        // starting at the specified index        // of the target array        myColl.CopyTo(myArr, 0);          // Displaying the elements in myArr        foreach(string str in myArr)        {            Console.WriteLine(str);        }    }}
Output:
A
B
C
D
E

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1?view=netframework-4.7.2
CSharp-Collection-Class
CSharp-Collections.ObjectModel-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | IsNullOrEmpty() Method
Main Method in C#
C# | Dictionary.Add() Method
Lambda Expressions in C#
Different ways to sort an array in descending order in C#
C# | Arrays of Strings
C# | How to use strings in switch statement
C# | List Class
C# | How to check whether a List contains a specified element
How to find the length of an Array in C#