Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 4 of 196

How do you do a deep copy of an object in .NET?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 203 Views

A deep copy in C# creates a completely independent copy of an object, including all its nested objects and reference types. Unlike a shallow copy that only copies references, a deep copy duplicates the entire object hierarchy, ensuring that changes to the copied object do not affect the original. Deep copying is essential when working with complex objects containing reference types like arrays, lists, or custom objects. Without proper deep copying, modifications to nested objects can unexpectedly affect the original object. Shallow Copy vs Deep Copy Shallow Copy ...

Read More

How to check if a number is a power of 2 in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 694 Views

A power of 2 is a number of the form 2n where n is a non-negative integer. These numbers have a special property in their binary representation − they contain exactly one bit set to 1. For example, 8 = 23 has binary representation 1000, and 16 = 24 has binary representation 10000. n 2n Binary 0 1 0001 1 2 0010 2 4 0100 3 8 1000 4 16 10000 Bitwise Trick: n & ...

Read More

How to convert C# DateTime to "YYYYMMDDHHMMSS" format?

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

Converting a DateTime object to the "YYYYMMDDHHMMSS" format in C# is commonly needed for timestamps, file naming, and database operations. This format provides a compact, sortable representation of date and time without separators. The ToString() method with custom format strings allows you to convert DateTime objects to any desired string format using specific format specifiers. Syntax Following is the syntax for converting DateTime to "YYYYMMDDHHMMSS" format − DateTime dateTime = DateTime.Now; string formattedDate = dateTime.ToString("yyyyMMddHHmmss"); The format specifiers used are − yyyy − Four-digit year MM − Two-digit month (01-12) dd ...

Read More

How to create a folder if it does not exist in C#?

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

Creating directories programmatically is a common task in C# applications. The System.IO namespace provides the necessary classes and methods to check for directory existence and create new folders when needed. It is always recommended to check if a directory exists before performing any file operations in C#, as the compiler will throw an exception if you attempt to access a non-existent folder. Syntax Following is the syntax for checking if a directory exists − bool exists = Directory.Exists(path); Following is the syntax for creating a directory − Directory.CreateDirectory(path); Using ...

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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

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

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

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

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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

How to convert byte array to string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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
Showing 31–40 of 1,958 articles
« Prev 1 2 3 4 5 6 196 Next »
Advertisements