Csharp Articles

Page 7 of 196

How to rethrow InnerException without losing stack trace in C#?

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

In C#, rethrowing exceptions while preserving the complete stack trace is crucial for effective debugging. When handling exceptions, you have several options for rethrowing that affect how much debugging information is preserved. The key difference lies between using throw; (which preserves the full stack trace) versus throw ex; (which resets the stack trace to the current location). Additionally, when dealing with InnerException, you need special techniques to maintain the complete exception chain. Syntax Following is the syntax for rethrowing an exception without losing stack trace − try { // code that might ...

Read More

How can I limit Parallel.ForEach in C#?

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

The Parallel.ForEach loop in C# executes iterations across multiple threads for improved performance. However, sometimes you need to limit the degree of parallelism to control resource usage, avoid overwhelming external systems, or manage thread contention. This is accomplished using ParallelOptions. Syntax Following is the basic syntax for Parallel.ForEach − Parallel.ForEach(collection, item => { // process item }); Following is the syntax for limiting parallelism using ParallelOptions − Parallel.ForEach(collection, new ParallelOptions { MaxDegreeOfParallelism = maxThreads }, item => { ...

Read More

How to implement Null object Pattern in C#?

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

The Null Object Pattern is a behavioral design pattern that helps eliminate null checks by providing a default object that implements the expected interface but performs no operations. Instead of returning null, you return a null object that behaves safely when methods are called on it. This pattern is particularly useful when you want to avoid NullReferenceException and make your code more readable by eliminating repetitive null checks. The null object provides a neutral behavior that represents "do nothing" or "no operation". Structure of Null Object Pattern Null Object Pattern Structure ...

Read More

How to implement IDisposable Design Pattern in C#?

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

The IDisposable design pattern (also called the Dispose Pattern) in C# is used to properly clean up unmanaged resources like file handles, database connections, and network streams. This pattern ensures that resources are released deterministically, rather than waiting for the garbage collector. Classes that directly or indirectly use unmanaged resources should implement the IDisposable interface. This includes classes that use FileStream, HttpClient, database connections, or any other objects that hold system resources. Syntax Following is the basic syntax for implementing IDisposable − public class ClassName : IDisposable { private bool disposed = ...

Read More

What is Facade and how to implement in C#?

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

The Facade design pattern provides a simplified interface to a complex subsystem. It acts as a wrapper that hides the complexity of multiple classes and their interactions behind a single, easy-to-use interface. This pattern is particularly useful when working with complex APIs, legacy systems, or when you need to provide a unified interface to a set of interfaces in a subsystem. Key Components The Facade pattern consists of the following components − Facade: The main interface that clients interact with. It delegates requests to appropriate subsystem objects. Subsystems: The complex classes that ...

Read More

How to implement Single Responsibility Principle using C#?

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

The Single Responsibility Principle (SRP) is the first principle of SOLID design principles in object-oriented programming. It states that a class should have only one reason to change, meaning each class should handle a single responsibility or concern. In this context, responsibility is considered to be one reason to change. When a class has multiple responsibilities, changes to one responsibility can affect the other functionality, making the code harder to maintain and test. Definition The Single Responsibility Principle states that if we have two reasons to change a class, we should split the functionality into two separate ...

Read More

How to perform a left outer join using linq extension methods in C#?

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

In LINQ, a Left Outer Join includes all elements from the left collection and matching elements from the right collection. When there's no match in the right collection, the result still includes the left element with null values for the right side. This is different from an Inner Join, which only includes matching elements from both collections. Left Outer Join ensures that no records from the left collection are lost, even when they don't have corresponding matches in the right collection. Syntax The Left Outer Join pattern using LINQ extension methods follows this structure − ...

Read More

Moving mouse pointer to a specific location or element using C# and Selenium

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 17-Mar-2026 7K+ Views

We can move mouse pointer to a specific location or element in Selenium WebDriver with C# using the Actions class. This class provides methods to simulate user interactions like moving the mouse, clicking, and performing complex gestures on web elements. To move the mouse to an element, we use the MoveToElement method and pass the element locator as a parameter. To move to specific coordinates, we use the MoveByOffset method. All actions must be executed using the Perform method. Syntax Following is the syntax for creating an Actions object and moving to an element − ...

Read More

Explain and contrast value types and reference types in C#

Akshay Khot
Akshay Khot
Updated on 17-Mar-2026 430 Views

In C#, all types can be divided into two main categories − value types and reference types. Understanding the difference between these types is crucial for memory management and avoiding common programming errors. Value Types Variables of value types directly contain their data. Each variable has its own copy of the data, stored on the stack. When you assign one value type variable to another, the entire value is copied. Value types in C# include − All numeric types: int, float, double, decimal, byte, etc. char and bool types struct ...

Read More

Provide a brief overview of the C# and .NET ecosystem

Akshay Khot
Akshay Khot
Updated on 17-Mar-2026 756 Views

C# is an object-oriented, type-safe and general-purpose programming language, which focuses on making the programmers productive. It tries to achieve this productivity through expressiveness, simplicity and a focus on performance. It works on different platforms such as Windows, Mac, and Linux. The .NET ecosystem provides the foundation for C# development, offering a comprehensive runtime environment, extensive libraries, and tools for building various types of applications from desktop to web and mobile solutions. Type-Safety C# is a statically typed language. That means the types are verified when you compile a program. This eliminates a large set of errors ...

Read More
Showing 61–70 of 1,951 articles
« Prev 1 5 6 7 8 9 196 Next »
Advertisements