Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 6 of 196

How to implement a Singleton design pattern in C#?

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

The Singleton design pattern belongs to the Creational type pattern. It ensures that only one instance of a particular class is created throughout the application lifecycle. This single instance coordinates actions across the application and provides global access to shared resources. The Singleton pattern is useful when you need exactly one instance of a class, such as for logging, caching, thread pools, or configuration settings. Implementation Guidelines Declare all constructors as private to prevent external instantiation. Provide a static property or method that returns the single instance. Store the instance in a static field to ensure ...

Read More

What is typeof, GetType or is in C#?

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

C# provides three important operators for working with types: typeof, GetType(), and is. These operators allow you to examine object types at runtime, perform type checking, and enable reflection-based operations. Syntax Following is the syntax for the typeof operator − Type type = typeof(ClassName); Following is the syntax for the GetType() method − Type type = objectInstance.GetType(); Following is the syntax for the is operator − bool result = objectInstance is TargetType; Understanding typeof, GetType, and is Type Operations in C# ...

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 207 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 703 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
Showing 51–60 of 1,958 articles
« Prev 1 4 5 6 7 8 196 Next »
Advertisements