Remove element at specified index of Collection in C#

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

232 Views

To remove an element at a specified index from a Collection in C#, you use the RemoveAt() method. This method removes the element at the specified zero-based index and shifts all subsequent elements down by one position. Syntax Following is the syntax for the RemoveAt() method − collection.RemoveAt(index); Parameters index − The zero-based index of the element to remove. Must be a valid index within the collection bounds. Using RemoveAt() for Single Element Removal The following example demonstrates removing a single element at index 3 from a Collection − ... Read More

How to get the Standard Input and Output Stream through Console in C#?

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

473 Views

The Console class in C# provides three standard streams for input and output operations: Console.In for standard input, Console.Out for standard output, and Console.Error for standard error. These streams allow you to access the underlying TextReader and TextWriter objects used by the console. Standard Streams Overview The console streams are represented as properties that return the following types − Console.In − Returns a TextReader object for reading standard input Console.Out − Returns a TextWriter object for writing to standard output Console.Error − Returns a TextWriter object for writing to standard error ... Read More

What is the difference between Func delegate and Action delegate in C#?

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

2K+ Views

A delegate is a type that represents references to methods with a particular parameter list and return type. When we instantiate a delegate, we can associate its instance with any method with a compatible signature and return type. We can invoke (or call) the method through the delegate instance. The .NET Framework provides two built-in generic delegate types: Func and Action. Both serve different purposes based on whether the method returns a value or not. Func Delegate The Func delegate is a generic delegate included in the System namespace. It has zero or more input parameters and ... Read More

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

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

367 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
Updated on 17-Mar-2026 07:04:36

258 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
Updated on 17-Mar-2026 07:04:36

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
Updated on 17-Mar-2026 07:04:36

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
Updated on 17-Mar-2026 07:04:36

358 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
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

159 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

Advertisements