AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 175 of 840

Remove all objects from the Queue in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 178 Views

To remove all objects from a Queue in C#, use the Clear() method. This method removes all elements from the Queue and resets its count to zero. The Queue class provides this built-in method for efficient bulk removal of all elements. Syntax Following is the syntax for the Clear() − queue.Clear(); Where queue is the Queue object from which all elements will be removed. Using Clear() Method Example 1 The following example demonstrates clearing a Queue containing string elements − using System; using System.Collections.Generic; public class Demo { ...

Read More

Count the number of key/value pairs in HybridDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 161 Views

A HybridDictionary in C# is a collection that automatically switches between a ListDictionary for small collections and a Hashtable for larger collections. To count the number of key/value pairs in a HybridDictionary, we use the Count property. The HybridDictionary optimizes performance by using different internal storage mechanisms based on the number of elements. For collections with 10 or fewer items, it uses a ListDictionary, and for larger collections, it switches to a Hashtable. Syntax Following is the syntax for getting the count of key/value pairs in a HybridDictionary − HybridDictionary dictionary = new HybridDictionary(); int ...

Read More

Get an enumerator that iterates through the HybridDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 167 Views

The HybridDictionary class in C# provides the GetEnumerator() method to iterate through its key-value pairs. This method returns an IDictionaryEnumerator object that allows you to traverse the collection efficiently. You can also use foreach loops to iterate through a HybridDictionary. Syntax Following is the syntax for getting an enumerator from a HybridDictionary − IDictionaryEnumerator enumerator = hybridDict.GetEnumerator(); The enumerator provides these key properties − enumerator.Key // Current key enumerator.Value // Current value enumerator.Entry // Current DictionaryEntry HybridDictionary Enumeration ...

Read More

Creating a synchronized wrapper for a SortedList object in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 230 Views

A synchronized wrapper for a SortedList object provides thread-safe access to the collection in multi-threaded environments. The SortedList.Synchronized() method creates a wrapper that ensures only one thread can access the collection at a time. Syntax Following is the syntax for creating a synchronized wrapper − SortedList synchronizedList = SortedList.Synchronized(originalList); To check if a SortedList is synchronized, use the IsSynchronized property − bool isSync = sortedList.IsSynchronized; Creating a Synchronized SortedList The following example demonstrates how to create a synchronized wrapper and verify its synchronization status − using System; ...

Read More

Get an ICollection containing the values in ListDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 196 Views

The ListDictionary class in C# provides a Values property that returns an ICollection containing all the values stored in the dictionary. This is useful when you need to iterate through or manipulate only the values without accessing the keys. Syntax Following is the syntax to get the values collection from a ListDictionary − ICollection values = listDictionary.Values; Return Value The Values property returns an ICollection object that contains all the values in the ListDictionary. The collection maintains the same order as the original dictionary entries. Using Values Property with Device Names ...

Read More

Creating StringBuilder having specified capacity in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 155 Views

The StringBuilder class in C# provides several constructors to create instances with different initial capacities. When you specify a capacity, you're setting the initial buffer size that StringBuilder allocates for string operations, which can improve performance by reducing memory reallocations. Syntax Following are the main constructors for creating StringBuilder with specified capacity − StringBuilder sb = new StringBuilder(int capacity); StringBuilder sb = new StringBuilder(string value, int capacity); Creating StringBuilder with Initial Capacity You can create a StringBuilder with a specific initial capacity to optimize memory usage − using System; using System.Text; ...

Read More

Get an enumerator that iterates through the ListDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 193 Views

To get an enumerator that iterates through the ListDictionary, you can use the GetEnumerator() method, which returns an IDictionaryEnumerator. This allows you to iterate through the key-value pairs in the dictionary using a while loop or foreach loop. The ListDictionary is a specialized collection class that implements a dictionary using a singly linked list, making it efficient for small collections (typically 10 or fewer elements). Syntax Following is the syntax for getting an enumerator from a ListDictionary − IDictionaryEnumerator enumerator = listDictionary.GetEnumerator(); while (enumerator.MoveNext()) { // Access enumerator.Key and enumerator.Value } ...

Read More

Enumerator that iterates through the BitArray in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 193 Views

The BitArray class in C# implements the IEnumerable interface, which allows you to iterate through its elements using an enumerator. This enables the use of foreach loops to traverse through each bit value in the array sequentially. The enumerator returns each bit as a bool value, making it easy to process the individual bits in a BitArray collection. Syntax Following is the syntax for iterating through a BitArray using an enumerator − BitArray bitArray = new BitArray(size); IEnumerable enumerable = bitArray; foreach (Object bit in enumerable) { // Process each bit } ...

Read More

Get an enumerator that iterates through the SortedDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 182 Views

The SortedDictionary class in C# provides the GetEnumerator() method to iterate through its key-value pairs. This method returns an IDictionaryEnumerator that allows you to traverse the collection in ascending order of keys. Syntax Following is the syntax for getting an enumerator from a SortedDictionary − IDictionaryEnumerator enumerator = sortedDictionary.GetEnumerator(); To iterate through the enumerator − while (enumerator.MoveNext()) { Console.WriteLine("Key = " + enumerator.Key + ", Value = " + enumerator.Value); } Return Value The GetEnumerator() method returns an IDictionaryEnumerator object that provides access to the key-value ...

Read More

Find the first node in LinkedList containing the specified value in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 152 Views

The LinkedList.Find() method in C# searches for the first node that contains the specified value and returns a LinkedListNode object. If the value is not found, it returns null. Syntax Following is the syntax for the Find() − public LinkedListNode Find(T value); Parameters value − The value to locate in the LinkedList. Return Value Returns the first LinkedListNode that contains the specified value, or null if the value is not found. Using Find() with String Values Example using System; using System.Collections.Generic; public class Demo ...

Read More
Showing 1741–1750 of 8,392 articles
« Prev 1 173 174 175 176 177 840 Next »
Advertisements