How does C# use collection classes? Introduction to the use of C# data structures

Collection<T> class provides a base class for a generic collection. T is the type of element in the set. This course belongs to the System.Collections.ObjectModel namespace.

Peculiarity:

  • The Collection<T> class can be used immediately by creating an instance of one of its construct types.
  • The Collection<T> class provides protected methods that you can use to customize its behavior when adding and removing items, clearing collections, or setting values for existing items.
  • Up to Collection<T> objects can be modified. However, the IList <T> object of the Collection object initialized with read-only cannot be modified.
  • You can use an integer index to access the elements in this collection. The indexes in this collection are made from scratch.
  • Collection<T > accepts null as a valid value for the reference type and allows duplicate elements.

Table of Contents

builder

Collection<T>()A new instance of the T <> class initialized as empty.
Collection<T>(IList <T>)Initialize a new instance of the Collection <T> class as a wrapper for the specified list.

Example:

// C# code to create a 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 >();
  
         // 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);
         }
     }
}

The output is as follows:

2
3
4
5

attribute

CountGet the number of elements actually contained in the Collection <T >.
ItemsGet the IList < T > wrapper around the Collection <T >.
Item[Int32]Gets or sets the element at the specified index.

Example:

// C# Code to illustrate 
// Properties of Collection class
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 >();
   
         // 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]);
     }
}

The output is as follows:

Count : 5
Element at index 2 is : C
Element at index 3 is : D

method

Add(T)Add an object to the end of the Collection <T >.
Clear()Remove all elements from the Collection <T >.
ClearItems()Remove all elements from the Collection <T >.
Contains(T)Determines if the element is in the Collection<T >.
CopyTo(T [], Int32)Start with the specified index of the target array and copy the entire Collection <T> to a compatible one-dimensional array.
Equals(Object)Determines whether the specified object is equal to the current object.
GetEnumerator()Returns the number of enumerations that traverse the Collection<T>.
GetHashCode()Used as the default hash function.
GetType()Obtain the type of the current instance.
IndexOf(T)Searches for the specified object and returns the index that first appeared from zero in the entire Collection <T>.
Insert(Int32, T)Inserts the element into the Collection <T > at the specified index.
InsertItem(Int32, T)Inserts the element into the Collection at the specified index.
MemberwiseClone()Creates a superficial copy of the current object.
Remove(T)Removes the specific object that appears for the first time from the Collection <T >.
RemoveAt(Int32)Deletes the element at the specified index of the Collection <T >.
RemoveItem(Int32)Deletes the element at the specified index of the Collection <T >.
SetItem(Int32, T)Replaces the element at the specified index.
ToString()Returns a string representing the current object.

Example:

// C# code to check if an
// element is in 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" );
  
         // 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" ));
     }
}

The output is as follows:

True

Example 2:

// C# code to copy the entire Collection
// to a compatible one-dimensional Array, // starting at the specified index of
// the target array
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" );
  
         // 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);
         }
     }
}

The output is as follows:

A
B
C
D
E