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
Programming Articles
Page 707 of 2547
Why the error Collection was modified; enumeration operation may not execute occurs and how to handle it in C#?
This error occurs when you modify a collection (such as adding or removing items) while iterating through it with a foreach loop or enumerator. The .NET runtime throws this exception to prevent unpredictable behavior and maintain collection integrity. Why This Error Occurs When you use foreach, it creates an internal enumerator that tracks the collection's state. If the collection is modified during iteration, the enumerator detects this change and throws an InvalidOperationException to prevent data corruption or infinite loops. Collection Modification During Iteration Original Collection: [Item1, Item2, Item3] ...
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 MoreHow to convert byte array to string in C#?
In C#, converting a byte array to a string requires understanding character encoding. Every string has a character set and encoding that tells the computer how to interpret raw bytes into characters. The Encoding class provides various methods to decode byte arrays into strings. The most common approach is using the Encoding.GetString() method, which decodes all bytes in a specified byte array into a string. Several encoding schemes are available such as UTF8, Unicode, UTF32, and ASCII. Syntax Following is the basic syntax for converting byte array to string − string result = Encoding.EncodingType.GetString(byteArray); ...
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 MoreHow to fetch a property value dynamically in C#?
We can use Reflection to fetch a property value dynamically in C#. Reflection provides objects of type Type that describe assemblies, modules, and types, allowing us to dynamically access and manipulate object properties at runtime without knowing them at compile time. The System.Reflection namespace and System.Type class work together to enable dynamic property access through methods like GetProperty() and GetValue(). Syntax Following is the syntax for getting a property value dynamically − Type type = typeof(ClassName); PropertyInfo property = type.GetProperty("PropertyName"); object value = property.GetValue(instanceObject, null); Following is the syntax for setting a property ...
Read MoreHow to delete all files and folders from a path in C#?
Deleting all files and folders from a directory is a common task in C# file operations. The System.IO namespace provides the DirectoryInfo class and Directory class that offer multiple approaches to accomplish this task safely and efficiently. Syntax Following is the syntax for using DirectoryInfo to delete directories and files − DirectoryInfo di = new DirectoryInfo(path); di.Delete(true); // true = recursive delete Following is the syntax for using Directory class methods − Directory.Delete(path, true); // true = recursive delete Using DirectoryInfo for Recursive Deletion The DirectoryInfo class provides detailed ...
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 MoreWhat is the main difference between int.Parse() and Convert.ToInt32 in C#?
Both int.Parse() and Convert.ToInt32() methods in C# are used to convert string representations of numbers to integers. However, they handle null values and certain edge cases differently. The key difference is that Convert.ToInt32() handles null values gracefully by returning 0, while int.Parse() throws an ArgumentNullException when encountering null. Syntax Following is the syntax for int.Parse() method − int result = int.Parse(stringValue); Following is the syntax for Convert.ToInt32() method − int result = Convert.ToInt32(stringValue); Using int.Parse() with Valid String The int.Parse() method converts a valid numeric string to an ...
Read More