Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 177 of 840
Get or set the value associated with specified key in ListDictionary in C#
The ListDictionary class in C# provides an indexer that allows you to get or set values using a specified key. The indexer dict[key] provides convenient access to elements in the dictionary by their key. ListDictionary is part of the System.Collections.Specialized namespace and is optimized for small collections where the number of elements is usually 10 or fewer. Syntax Following is the syntax for getting a value using the indexer − object value = listDictionary[key]; Following is the syntax for setting a value using the indexer − listDictionary[key] = value; ...
Read MoreCheck if two StringCollection objects are equal in C#
In C#, checking if two StringCollection objects are equal is not as straightforward as it might seem. The default Equals() method only checks for reference equality, not content equality. This means two collections with identical elements will return false unless they reference the same object. Understanding StringCollection Equality The StringCollection class inherits the default Equals() method from Object, which performs reference comparison. To check for content equality, you need to implement custom comparison logic. StringCollection Equality Types Reference Equality strCol1.Equals(strCol2) Returns true only if ...
Read MoreHow to perform a specified action on each element of the List in C#?
In C#, you can perform a specified action on each element of a List using the ForEach() method. This method executes a delegate function on every element in the list, making it convenient for applying transformations, calculations, or operations without manually iterating through the collection. Syntax Following is the syntax for using List.ForEach() method − list.ForEach(Action action); Where action is a delegate that takes one parameter of type T and returns void. Parameters action − The Action delegate to perform on each element of the list. Using ForEach() with ...
Read MoreCheck if two StringDictionary objects are equal or not in C#
The StringDictionary class in C# is a specialized collection that stores key-value pairs as strings. When comparing two StringDictionary objects for equality, it's important to understand that the default Equals()
Read MoreHow to play Beep sound through Console in C#?
The Console.Beep() method in C# is used to play a beep sound through the system speaker. This method is particularly useful for providing audio feedback in console applications or alerting users to specific events. Syntax The Console.Beep() method has two overloads − Console.Beep(); Console.Beep(int frequency, int duration); Parameters frequency − The frequency of the beep in hertz (Hz). Must be between 37 and 32767. duration − The duration of the beep in milliseconds. Using Console.Beep() Without Parameters The parameterless Console.Beep() method plays a default beep sound ...
Read MoreGet or set the value associated with specified key in SortedList in C#
In C#, the SortedList collection allows you to get or set values using the indexer syntax with square brackets. The indexer property list[key] provides both read and write access to values associated with specific keys in the sorted list. Syntax Following is the syntax for getting a value from SortedList − object value = sortedList[key]; Following is the syntax for setting a value in SortedList − sortedList[key] = value; If the key exists, it updates the value. If the key doesn't exist, it adds a new key-value pair. Getting ...
Read MoreRemove entry with specified key from OrderedDictionary in C#
The OrderedDictionary class in C# provides the Remove() method to remove entries with a specified key. The OrderedDictionary maintains the insertion order of elements while allowing both key-based and index-based access. Syntax Following is the syntax for removing an entry by key from an OrderedDictionary − orderedDictionary.Remove(key); Parameters key − The key of the entry to remove from the OrderedDictionary. Return Value The Remove() method does not return a value. It removes the entry if the key exists, or does nothing if the key is not found. ...
Read MoreHow to create a shallow copy of SortedList Object in C#?
The shallow copy of a SortedList object in C# creates a new SortedList instance with the same key-value pairs as the original, but both collections share references to the same objects. This is accomplished using the Clone() method. Syntax Following is the syntax for creating a shallow copy of SortedList − SortedList clonedList = (SortedList)originalList.Clone(); What is a Shallow Copy? A shallow copy creates a new collection object, but the elements inside both the original and copied collections point to the same memory locations. For value types like strings and integers, this behaves ...
Read MoreRemove entry with specified key from the StringDictionary in C#
The StringDictionary class in C# provides the Remove() method to delete entries with a specified key. This method removes both the key and its associated value from the collection. StringDictionary is case-insensitive, meaning keys are automatically converted to lowercase. Syntax Following is the syntax for the Remove() method − public virtual void Remove(string key) Parameters key − The string key to remove from the StringDictionary. Using Remove() Method Example using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() ...
Read MoreHow to get hash code for the specified key of a Hashtable in C#?
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