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
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
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
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
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
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
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()
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance