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
Csharp Articles
Page 46 of 196
How 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 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 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 MoreHow to create a StringCollection in C#?
The StringCollection class in C# is a specialized collection that stores only string values. It is part of the System.Collections.Specialized namespace and provides methods to add, remove, and manipulate strings efficiently. StringCollection is useful when you need to work exclusively with string data and want type safety without the overhead of generic collections. Syntax Following is the syntax for creating a StringCollection − StringCollection collection = new StringCollection(); To add multiple strings at once using an array − string[] array = {"string1", "string2", "string3"}; collection.AddRange(array); Creating and Populating a ...
Read MoreHow to get the HashCode of the tuple in C#?
To get the HashCode of a tuple in C#, you can use the GetHashCode() method. This method returns an integer hash code that represents the tuple's value. Tuples with identical values will have the same hash code, while tuples with different values will typically have different hash codes. Syntax Following is the syntax for getting the hash code of a tuple − int hashCode = tuple.GetHashCode(); Return Value The GetHashCode() method returns a 32-bit signed integer that serves as a hash code for the tuple. Equal tuples will always return the same hash ...
Read MoreHow to remove element from the specified index of the List in C#?
To remove an element from a specified index in a List in C#, you use the RemoveAt() method. This method removes the element at the given index and automatically shifts all subsequent elements one position to the left. Syntax Following is the syntax for the RemoveAt() method − list.RemoveAt(int index); Parameters index − The zero-based index of the element to remove. Must be a valid index (0 ≤ index < Count). Using RemoveAt() with String List The following example demonstrates removing an element at index 5 from a list ...
Read MoreHow to retrieve the system's reference to the specified String in C#?
The string.Intern() method in C# retrieves the system's reference to the specified string from the intern pool. The intern pool is a table maintained by the .NET runtime that contains references to all string literals and interned strings to optimize memory usage by ensuring that identical strings share the same memory location. Syntax Following is the syntax for the string.Intern() method − public static string Intern(string str) Parameters str − The string to search for in the intern pool. Return Value Returns a reference to the string ...
Read MoreCheck if Caps Lock and Num Lock is on or off through Console in C#
The C# Console class provides built-in properties to check the status of the Caps Lock and Num Lock keys. The Console.CapsLock and Console.NumberLock properties return a boolean value indicating whether these keys are currently active. Syntax Following is the syntax to check Caps Lock status − bool capsLockStatus = Console.CapsLock; Following is the syntax to check Num Lock status − bool numLockStatus = Console.NumberLock; Return Value Console.CapsLock returns true if Caps Lock is on, false otherwise. Console.NumberLock returns true if Num Lock is on, false otherwise. ...
Read MoreCheck if Input, Output and Error is redirected on the Console or not in C#
The Console class in C# provides properties to check if standard input, output, or error streams are redirected from their default console locations. This is useful when your application needs to behave differently based on how it's being executed. Console Redirection Properties The Console class offers three boolean properties to check redirection status − Console.IsInputRedirected // Checks if input is redirected Console.IsOutputRedirected // Checks if output is redirected Console.IsErrorRedirected // Checks if error is redirected Checking Input Redirection To check if the input stream is redirected ...
Read MoreCheck if HybridDictionary is Synchronized in C#
The HybridDictionary class in C# provides the IsSynchronized property to check if the dictionary is thread-safe for multi-threaded operations. By default, HybridDictionary is not synchronized, meaning it is not thread-safe. A HybridDictionary automatically switches between a ListDictionary (for small collections) and a Hashtable (for larger collections) based on the number of elements, but remains unsynchronized unless explicitly wrapped. Syntax The IsSynchronized property returns a boolean value indicating thread safety − bool isSync = hybridDictionary.IsSynchronized; To create a synchronized wrapper around a HybridDictionary − HybridDictionary syncDict = (HybridDictionary)HybridDictionary.Synchronized(originalDict); Using IsSynchronized ...
Read More