How to verify an exception that has been thrown in unit testing C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

4K+ Views

Unit testing in C# requires verifying that methods throw expected exceptions under certain conditions. There are two primary approaches to verify exceptions in MSTest: using Assert.ThrowsException and using the ExpectedException attribute. Both methods allow you to test that your code properly handles error conditions and throws appropriate exceptions when invalid input or unexpected scenarios occur. Syntax Using Assert.ThrowsException method − var exception = Assert.ThrowsException(() => methodCall); Assert.AreEqual("Expected message", exception.Message); Using ExpectedException attribute − [ExpectedException(typeof(ExceptionType), "Expected message")] [TestMethod] public void TestMethod() { // method call that should throw exception ... Read More

Get or set the value associated with specified key in ListDictionary in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:36

160 Views

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 More

How to create a comma separated string from a list of string in C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

13K+ Views

A List of strings can be converted to a comma separated string using the built-in string.Join() method. This method takes a separator and a collection of strings, returning a single concatenated string. This type of conversion is useful when collecting user data (such as checkbox selections) and converting it to a comma separated string for database queries or API calls. Syntax Following is the syntax for string.Join() method − string.Join(separator, collection); Parameters separator − The string to use as separator between elements collection − A collection that contains ... Read More

Check if two StringCollection objects are equal in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:36

208 Views

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 More

How to perform a specified action on each element of the List in C#?

AmitDiwan
Updated on 17-Mar-2026 07:04:36

284 Views

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 More

Why the error Collection was modified; enumeration operation may not execute occurs and how to handle it in C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

21K+ Views

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 More

Check if two StringDictionary objects are equal or not in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:36

209 Views

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()

How to play Beep sound through Console in C#?

AmitDiwan
Updated on 17-Mar-2026 07:04:36

363 Views

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 More

How to convert byte array to string in C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

20K+ Views

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 More

Get or set the value associated with specified key in SortedList in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:36

157 Views

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 More

Advertisements