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 Split List into Sub list in C# using LINQ ?
If you want to split the generic list in to sub list in C# , one of the simplest way is to use LINQ (Language Integrated Query). Below is a code sample demonstrating this feature. How to Split List into Sub list in C# using LINQ ?
How to convert List<string> to string with comma delimiter in C# ?
If you need to convert a collection (for example List of string) to string separated with the Comma as delimiter , you can either use the string.join method or aggregate method as shwon below. How to convert List<string> to string with comma delimiter in C# ?
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) { …
How to Get the Dictionary Key by value in C# ?
If you want to get the Key of a dictionary based on the value in C# , you can use the LINQ’s where clause o FirstOrDefault method. How to Get the Dictionary Key by value in C# ?
Using HashSet to remove duplicates from array in C#
If you want to remove duplicates from an array in C# , one of the options is to use HashSet as shown below. How to remove duplicates from an array in C# using HashSet?
How to Get the number of elements in ArrayList in C# ?
Here’s the code that demonstrates how one can get the total number of elements contained in the ArrayList using C# using the Count property of the ArrayList. How to Get the number of elements in ArrayList in C# ?
How to Merge Dictionary in C# ?
There are times when you want to merge 2 dictionary collections to one . Below is a code sample that shows how to do it. How to Merge Dictionary in C# ?
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# ?