Csharp Articles

Page 28 of 196

Check if two StringBuilder objects are Equal in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 202 Views

To check if two StringBuilder objects are equal in C#, you need to understand that StringBuilder.Equals() checks for reference equality, not content equality. For content comparison, you must convert the StringBuilder objects to strings first. Syntax Following is the syntax for checking StringBuilder equality − // Reference equality bool isEqual = stringBuilder1.Equals(stringBuilder2); // Content equality bool isContentEqual = stringBuilder1.ToString().Equals(stringBuilder2.ToString()); Using Reference Equality The Equals() method on StringBuilder checks if both objects reference the same instance − using System; using System.Text; public class Demo { public static ...

Read More

Check if two Tuple Objects are equal in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 211 Views

To check if two Tuple objects are equal in C#, you can use the Equals() method or the equality operator (==). Tuples are compared element-wise, meaning all corresponding elements must be equal for the tuples to be considered equal. Syntax Following is the syntax for comparing tuples using the Equals() method − bool result = tuple1.Equals(tuple2); You can also use the equality operator − bool result = tuple1 == tuple2; How Tuple Equality Works Tuple equality comparison follows these rules: Both tuples must have the same number ...

Read More

Get a read-only copy of the OrderedDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 215 Views

The OrderedDictionary class in C# provides the AsReadOnly() method to create a read-only copy of the collection. This wrapper prevents any modifications to the dictionary while maintaining access to all elements in their insertion order. Syntax Following is the syntax for creating a read-only copy of an OrderedDictionary − OrderedDictionary readOnlyDict = originalDict.AsReadOnly(); Return Value The AsReadOnly() method returns an OrderedDictionary wrapper that is read-only. Any attempt to modify this wrapper will throw a NotSupportedException. Using AsReadOnly() Method The following example demonstrates how to create a read-only copy and verify its ...

Read More

Remove all elements from a HashSet in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 274 Views

To remove all elements from a HashSet in C#, you use the Clear() method. This method removes all elements from the HashSet and resets its count to zero. The Clear() method is the most efficient way to empty a HashSet completely. Syntax Following is the syntax for the Clear() method − hashSet.Clear(); Parameters The Clear() method takes no parameters. Return Value The Clear() method returns void (no return value). Using Clear() Method Example using System; using System.Collections.Generic; public class Demo { public static ...

Read More

Remove all elements from a SortedList in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 219 Views

To remove all elements from a SortedList in C#, you use the Clear() method. This method removes all key-value pairs from the SortedList and sets the Count property to zero. Syntax Following is the syntax for the Clear() method − sortedList.Clear(); Parameters The Clear() method does not take any parameters. Return Value The Clear() method does not return any value. It has a return type of void. SortedList.Clear() Operation Before Clear() {A:1, B:2, C:3} Count = 3 ...

Read More

Check if ListDictionary is read-only in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 193 Views

The ListDictionary class in C# provides an IsReadOnly property to check if the dictionary is read-only. This property returns false for a standard ListDictionary, as it allows adding, removing, and modifying elements. However, you can create read-only wrappers using collection utilities. Syntax Following is the syntax to check if a ListDictionary is read-only − bool isReadOnly = listDictionary.IsReadOnly; Using IsReadOnly Property Example using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main(){ ListDictionary dict1 ...

Read More

Check if ListDictionary is synchronized in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 156 Views

The ListDictionary class in C# provides the IsSynchronized property to check if the collection is synchronized (thread-safe). By default, ListDictionary is not synchronized, meaning it is not thread-safe for concurrent access by multiple threads. Syntax Following is the syntax to check if a ListDictionary is synchronized − bool isSynchronized = listDictionary.IsSynchronized; Return Value The IsSynchronized property returns a bool value − true − if the ListDictionary is synchronized (thread-safe) false − if the ListDictionary is not synchronized (not thread-safe) Example The following example demonstrates how to check if ...

Read More

Remove all elements from OrderedDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 177 Views

The OrderedDictionary class in C# provides the Clear() method to remove all elements from the collection. This method is efficient and maintains the structure of the OrderedDictionary while removing all key-value pairs. The OrderedDictionary combines features of both Hashtable and ArrayList, allowing access to elements by both key and index while preserving insertion order. Syntax Following is the syntax for the Clear() method − public void Clear() Parameters The Clear() method takes no parameters. Return Value The Clear() method does not return any value. It simply removes all elements from ...

Read More

Remove all elements from the ArrayList in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 237 Views

To remove all elements from an ArrayList in C#, you can use the Clear() method. This method removes all elements from the ArrayList and sets the Count property to zero. Syntax Following is the syntax for the Clear() method − arrayList.Clear(); Parameters The Clear() method does not take any parameters. Return Value The Clear() method does not return any value. It is a void method that modifies the ArrayList in-place. Using Clear() to Remove All Elements The following example demonstrates how to use the Clear() method to remove all ...

Read More

How to get Sixth Element of the Tuple in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 211 Views

To access the sixth element of a Tuple in C#, you use the Item6 property. This property is available for tuples that contain six or more elements. Tuples in C# are immutable data structures that can store multiple values of different types. Syntax Following is the syntax to access the sixth element of a tuple − var sixthElement = tuple.Item6; The Item6 property returns the value stored at the sixth position in the tuple. Using Item6 Property The following example demonstrates how to create a 7-tuple and access its sixth element using ...

Read More
Showing 271–280 of 1,951 articles
« Prev 1 26 27 28 29 30 196 Next »
Advertisements