Csharp Articles

Page 52 of 196

How to get hash code for the specified key of a Hashtable in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 322 Views

To get the hash code for a specified key in a Hashtable, you need to use the GetHash() method. This method is protected in the base Hashtable class, so you must create a derived class to access it. The hash code is an integer value used internally by the hashtable to determine where to store the key-value pair. Syntax Following is the syntax to access the protected GetHash() method − public class CustomHashtable : Hashtable { public int GetHashCodeForKey(object key) { return GetHash(key); } ...

Read More

How to get all elements of a List that match the conditions specified by the predicate in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 708 Views

To get all elements of a List that match specific conditions, C# provides the FindAll method which accepts a predicate − a delegate that defines the filtering criteria. This method returns a new List containing only the elements that satisfy the condition. Syntax Following is the syntax for using FindAll with a predicate − List result = list.FindAll(predicate); The predicate can be defined as a method, lambda expression, or anonymous delegate − // Method predicate private static bool MethodName(T item) { return condition; } // Lambda expression predicate ...

Read More

How to get Synchronize access to an Array in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 212 Views

To get synchronized access to an array in C#, you can use the SyncRoot property of the array along with the lock statement. This ensures thread-safe access when multiple threads are accessing the same array concurrently. The SyncRoot property returns an object that can be used to synchronize access to the array, preventing data races and ensuring thread safety in multi-threaded applications. Syntax Following is the syntax for synchronizing access to an array − lock(array.SyncRoot) { // Thread-safe operations on the array foreach (var item in array) { ...

Read More

StringDictionary Class in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 275 Views

The StringDictionary class implements a hash table with the key and the value strongly typed to be strings rather than objects. It is part of the System.Collections.Specialized namespace and provides a case-insensitive string-based collection. Note: The StringDictionary class is considered obsolete. For new development, use Dictionary which provides better performance and more features. Syntax Following is the basic syntax to create and use a StringDictionary − StringDictionary stringDict = new StringDictionary(); stringDict.Add("key", "value"); string value = stringDict["key"]; Properties Following are the key properties of the StringDictionary class − ...

Read More

Count the total number of elements in the List in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 696 Views

To count the total number of elements in a List in C#, you use the Count property. This property returns an integer value representing the current number of elements stored in the list. Syntax Following is the syntax for using the Count property − List list = new List(); int count = list.Count; Return Value The Count property returns an int value representing the number of elements currently in the list. It returns 0 for an empty list. Using Count Property with List Operations Example using System; using System.Collections.Generic; ...

Read More

Create a Queue from another collection in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 157 Views

Creating a Queue from another collection in C# can be accomplished using the Queue constructor that accepts an IEnumerable parameter. This allows you to initialize a new Queue with elements from arrays, lists, other queues, or any collection that implements IEnumerable. Syntax Following is the syntax to create a Queue from another collection − Queue newQueue = new Queue(sourceCollection); Where sourceCollection can be any collection implementing IEnumerable such as arrays, lists, or other queues. Using Queue Constructor with Array Example using System; using System.Collections.Generic; public class Demo { ...

Read More

Get the specified members of the current Type in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 153 Views

To get the specified members of the current Type in C#, we use the GetMember() method from the System.Reflection namespace. This method allows us to retrieve information about specific members of a type, such as fields, properties, methods, and events. Syntax Following is the syntax for getting specified members of a Type − Type.GetMember(string name) Type.GetMember(string name, BindingFlags bindingAttr) Type.GetMember(string name, MemberTypes type, BindingFlags bindingAttr) Parameters name − The string containing the name of the member to get. bindingAttr − A bitmask comprised of one or more BindingFlags that ...

Read More

How to get Synchronize access to the Stack in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 178 Views

In C#, the Stack class is not thread-safe by default. To achieve synchronized access to a Stack in a multithreaded environment, you can use the SyncRoot property with the lock statement. This ensures that only one thread can access the Stack at a time, preventing data corruption and race conditions. Syntax Following is the syntax for synchronizing access to a Stack − lock(stack.SyncRoot) { // Thread-safe operations on stack foreach(Object item in stack) { // Process items safely } } ...

Read More

How to check whether a List contains the elements that match the specified conditions in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 496 Views

To check whether a List contains elements that match specific conditions in C#, you can use the Exists() method. This method takes a predicate (a function that returns true or false) and returns true if at least one element in the list satisfies the condition. Syntax Following is the syntax for using List.Exists() method − public bool Exists(Predicate match) Parameters match − The predicate delegate that defines the conditions to check against the elements. Return Value Returns true if the List contains one or more elements that match the ...

Read More

Check whether a SortedList object contains a specific key in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 190 Views

To check whether a SortedList object contains a specific key in C#, you use the Contains() method. This method returns true if the key exists in the SortedList, otherwise false. A SortedList automatically maintains its elements in sorted order by key. Syntax Following is the syntax for checking if a key exists in a SortedList − bool result = sortedList.Contains(key); Parameters key: The key to search for in the SortedList. The key can be of any type that implements IComparable. Return Value The Contains() method returns a bool value ...

Read More
Showing 511–520 of 1,951 articles
« Prev 1 50 51 52 53 54 196 Next »
Advertisements