Csharp Articles

Page 48 of 196

Insert at the specified index in StringCollection in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 213 Views

The StringCollection class in C# provides the Insert() method to add elements at specific positions within the collection. This method shifts existing elements to the right to make room for the new element at the specified index. Syntax Following is the syntax for the Insert() method − stringCollection.Insert(index, value); Parameters index − The zero-based index at which to insert the element. value − The string value to insert into the collection. Using Insert() Method Example using System; using System.Collections.Specialized; public class Demo { ...

Read More

Insert into OrderedDictionary with key and value at specified index in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 420 Views

The OrderedDictionary class in C# provides an Insert() method to add key-value pairs at a specific index position. Unlike regular dictionaries, OrderedDictionary maintains the insertion order of elements and allows indexed access. Syntax Following is the syntax for inserting into an OrderedDictionary at a specified index − orderedDict.Insert(index, key, value); Parameters index − The zero-based index at which to insert the key-value pair key − The key of the element to insert value − The value of the element to insert Using Insert() Method The following example demonstrates inserting ...

Read More

Intersection of SortedSet with a collection in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 208 Views

The IntersectWith method in C# allows you to find the common elements between a SortedSet and any collection that implements IEnumerable. This method modifies the original SortedSet to contain only elements that exist in both collections. Syntax Following is the syntax for the IntersectWith method − public void IntersectWith(IEnumerable other) Parameters other − The collection to compare with the current SortedSet. How It Works The IntersectWith method performs an in-place intersection operation, meaning it modifies the original SortedSet and removes all elements that are not present in the specified ...

Read More

Intersection of two HashSets in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 596 Views

The intersection of two HashSets in C# finds the common elements present in both collections. The HashSet class provides the IntersectWith() method to perform this operation efficiently. Syntax Following is the syntax for finding the intersection of two HashSets − hashSet1.IntersectWith(hashSet2); Parameters other − The collection to compare to the current HashSet. Return Value The IntersectWith() method does not return a value. It modifies the current HashSet to contain only the elements that exist in both HashSets. HashSet Intersection ...

Read More

Check if every List element matches the predicate conditions in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 420 Views

To check if every List element matches the predicate conditions in C#, you can use the List.TrueForAll() method. This method takes a predicate delegate and returns true if all elements in the list satisfy the condition, or false if any element fails the test. Syntax Following is the syntax for the TrueForAll() method − public bool TrueForAll(Predicate match) Parameters match: A delegate that defines the conditions to check against the elements. It takes an element of type T and returns a boolean. Return Value Returns true if every element ...

Read More

How to create a ListDictionary in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 149 Views

A ListDictionary in C# is a specialized collection class from the System.Collections.Specialized namespace that implements IDictionary using a singly linked list. It is optimized for small collections (typically 10 items or fewer) and offers better performance than Hashtable for small datasets. The ListDictionary class is ideal when you need a dictionary-like collection with a small number of elements, as it has lower memory overhead compared to hash-based collections. Syntax Following is the syntax for creating a ListDictionary − ListDictionary dictionary = new ListDictionary(); To add key-value pairs to the ListDictionary − ...

Read More

Get an enumerator that iterates through the SortedSet in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 158 Views

The SortedSet class in C# provides the GetEnumerator() method to retrieve an enumerator that iterates through the collection in sorted order. This enumerator allows you to manually control the iteration process using MoveNext() and Current properties. Syntax Following is the syntax for getting an enumerator from a SortedSet − SortedSet.Enumerator enumerator = sortedSet.GetEnumerator(); Following is the syntax for using the enumerator to iterate − while (enumerator.MoveNext()) { T currentElement = enumerator.Current; // process currentElement } Using GetEnumerator() with String Elements The following example ...

Read More

Get an enumerator that iterates through the StringDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 149 Views

The StringDictionary class in C# provides the GetEnumerator() method to obtain an enumerator that iterates through the collection. This enumerator returns DictionaryEntry objects containing key-value pairs. Note that StringDictionary automatically converts all keys to lowercase during storage. Syntax Following is the syntax for getting an enumerator from StringDictionary − IEnumerator enumerator = stringDictionary.GetEnumerator(); To iterate through the enumerator − while (enumerator.MoveNext()) { DictionaryEntry entry = (DictionaryEntry)enumerator.Current; // Use entry.Key and entry.Value } Using GetEnumerator() Method The following example demonstrates how to ...

Read More

How to use strings in switch statement in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 2K+ Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. In C#, you can use strings directly in switch statements, making it convenient to handle text-based conditions. Syntax Following is the syntax for using strings in a switch statement − switch (stringVariable) { case "value1": // code block break; case "value2": ...

Read More

Convert the value of the current DateTime object to a Windows file time in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 362 Views

The DateTime.ToFileTime() method in C# converts the value of the current DateTime object to a Windows file time. A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (UTC). This method is commonly used when working with file system operations, as Windows stores file timestamps in this format internally. Syntax Following is the syntax for the ToFileTime() method − public long ToFileTime() Return Value The method returns a long value representing the Windows file time equivalent of ...

Read More
Showing 471–480 of 1,951 articles
« Prev 1 46 47 48 49 50 196 Next »
Advertisements