Programming Articles

Page 706 of 2547

Remove elements from a HashSet with conditions defined by the predicate in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 370 Views

The RemoveWhere method in C# allows you to remove elements from a HashSet based on conditions defined by a predicate function. This method takes a delegate that returns true for elements that should be removed and false for elements that should be kept. Syntax Following is the syntax for using RemoveWhere method − public int RemoveWhere(Predicate match) Parameters match − A predicate delegate that defines the conditions for removal. Returns true for elements to be removed. Return Value Returns an int representing the number of elements that were removed ...

Read More

How to get TypeCode in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 259 Views

To get the TypeCode in C#, you use the GetTypeCode() method available on all value types and some reference types. The TypeCode is an enumeration that represents the type classification of a .NET type, providing a simplified way to identify common data types. Syntax Following is the syntax for getting TypeCode − TypeCode typeCode = value.GetTypeCode(); You can also use the static method from the Convert class − TypeCode typeCode = Type.GetTypeCode(typeof(DataType)); Return Value The GetTypeCode() method returns a TypeCode enumeration value that represents the type classification. Common TypeCode ...

Read More

How to convert byte array to an object stream in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

A stream is an abstract base class that provides a generic view of a sequence of bytes. Streams support three fundamental operations: reading, writing, and seeking. Converting a byte array to a stream allows you to work with the data using stream-based operations and can lead to performance improvements in certain scenarios. In C#, you can convert a byte array to an object stream using the MemoryStream class, which represents a stream whose backing store is memory. Syntax Following is the syntax for converting a byte array to a MemoryStream − MemoryStream stream = new ...

Read More

C# Program to Append Text to an Existing File

Ankit kumar
Ankit kumar
Updated on 17-Mar-2026 11K+ Views

Appending text means adding new information to the end of an existing file. In C#, we can append text to files using various methods from the System.IO namespace. This is particularly useful for logging, data collection, and maintaining records. Behavior with Non-Existing Files When attempting to append to a file that doesn't exist, C# automatically creates a new file with the specified name and then adds the content. This eliminates the need for separate file existence checks in most scenarios. Using File.AppendAllText() Method Syntax public static void AppendAllText(string path, string contents); This ...

Read More

Remove elements from a SortedSet that match the predicate in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 361 Views

The RemoveWhere method in C# allows you to remove elements from a SortedSet based on a specified condition or predicate. This method evaluates each element against the predicate function and removes all elements that satisfy the condition. Syntax Following is the syntax for the RemoveWhere method − public int RemoveWhere(Predicate match) Parameters match: A Predicate delegate that defines the conditions of the elements to remove. The predicate returns true for elements that should be removed. Return Value The method returns an int representing the number of elements removed from the SortedSet. ...

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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
AmitDiwan
Updated on 17-Mar-2026 164 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
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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
AmitDiwan
Updated on 17-Mar-2026 209 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
AmitDiwan
Updated on 17-Mar-2026 288 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
Showing 7051–7060 of 25,466 articles
« Prev 1 704 705 706 707 708 2547 Next »
Advertisements