Tag: Collection

How to Check if the ArrayList is read-only in C# ?

There are times when you would use the read-only wrapper in order to prevent modifying the ArrayList. We use the ArrayList.ReadOnly method to create such collection. When doing so , you might want to check if the ArrayList is Read-only or not. How to Check if the ArrayList is read-only in C# ? Use the IsReadOnly property defined in the ArrayList object to check this.

How to make a Type Sortable on Different Criteria in C# ?

IComparable interface is a great way to sort your objects by using the sorting routines of the List. There is a another interface called IComparer<T> which allows the objects to be sorted based on different criteria. How to make a Type Sortable on Different Criteria in C# ? For example , if you wanted to sort the Employee objects by Id , you can create…

How to Create an Empty array without defining the size in C# ?

When we create an array in C# , we tend to define the size of the array as shown below. Can we create an array without defining the size ? The best option in this  scenario is to use the collection. You can use the Generic List which allows you to add as many items as possible and then you can use the ToArray method…

How to Get the Top 5 elements from a List in C# ?

If you want to get the top 5 elements from a list in C# or skip the first item and get the next five elements from a List , you can use the Take and the Skip methods. How to Get the Top 5 elements from a List in C# ? Below is a sample code snippet demonstrating the usage of the Skip and Take…

How to Add item to the beginning of List in C# ?

If you want to add or insert an item to the beginning of the List in C# , you can use the Insert method defined in the collection as shown below. How to Add item to the beginning of List in C# ? using System; using System.Collections.Generic; using System.Linq; namespace ACCode {     class Program     {         static void Main(string[] args)         {            …

Difference between Hash table and Dictionary in .NET

Here’s a simple difference between Hashtable and Dictionary in .NET Dictionary Hashtable It is generic in nature It is non generic in nature It is defined in the namespace System.Collections.Generic It is defined in the namespace System.Collections When a request is made to the dictionary and if the key does not exists , you get an exception When a request is made to the Hashtable…

How to update the value in a Dictionary in C# ?

Below is a sample code snippet demonstrating how to update the value that is stored within the dictionary using C#. We access the dictionary with the key as index and set the value. How to update the value in a Dictionary in C# ?