Csharp Articles

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

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 fetch a property value dynamically in C#?

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

We can use Reflection to fetch a property value dynamically in C#. Reflection provides objects of type Type that describe assemblies, modules, and types, allowing us to dynamically access and manipulate object properties at runtime without knowing them at compile time. The System.Reflection namespace and System.Type class work together to enable dynamic property access through methods like GetProperty() and GetValue(). Syntax Following is the syntax for getting a property value dynamically − Type type = typeof(ClassName); PropertyInfo property = type.GetProperty("PropertyName"); object value = property.GetValue(instanceObject, null); Following is the syntax for setting a property ...

Read More

How to delete all files and folders from a path in C#?

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

Deleting all files and folders from a directory is a common task in C# file operations. The System.IO namespace provides the DirectoryInfo class and Directory class that offer multiple approaches to accomplish this task safely and efficiently. Syntax Following is the syntax for using DirectoryInfo to delete directories and files − DirectoryInfo di = new DirectoryInfo(path); di.Delete(true); // true = recursive delete Following is the syntax for using Directory class methods − Directory.Delete(path, true); // true = recursive delete Using DirectoryInfo for Recursive Deletion The DirectoryInfo class provides detailed ...

Read More

What are the various return types of a controller action in C# ASP.NET WebAPI?

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

ASP.NET Web API controller actions can return different types depending on your application's requirements. Understanding these return types helps you choose the most appropriate approach for your specific scenarios. Available Return Types Void − Returns no content with HTTP 204 status Primitive/Complex Types − Returns data directly with automatic serialization HttpResponseMessage − Provides full control over the HTTP response IHttpActionResult − Offers a cleaner, testable approach to response creation Web API Return Type Evolution Void 204 No Content Primitive/ ...

Read More

How to return custom result type from an action method in C# ASP.NET WebAPI?

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

In ASP.NET Web API, you can create custom result types by implementing the IHttpActionResult interface. This interface provides a flexible way to customize HTTP responses beyond the standard return types like Ok(), BadRequest(), or NotFound(). The IHttpActionResult interface contains a single method that asynchronously creates an HttpResponseMessage instance, giving you full control over the HTTP response. Syntax The IHttpActionResult interface has the following structure − public interface IHttpActionResult { Task ExecuteAsync(CancellationToken cancellationToken); } How It Works When a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync ...

Read More

How to resolve CORS issue in C# ASP.NET WebAPI?

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

Cross-Origin Resource Sharing (CORS) is a security mechanism that uses additional HTTP headers to allow web applications running at one origin to access selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own. For example, consider an application with its front-end served from https://demodomain-ui.com and backend from https://demodomain-service.com/api. When the UI tries to make API calls to the backend, browsers restrict these cross-origin HTTP requests for security reasons, resulting in CORS errors. CORS ...

Read More

What are the different types of filters in C# ASP.NET WebAPI?

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

Filters in ASP.NET Web API provide a way to inject additional logic at different stages of request processing. They enable cross-cutting concerns such as authentication, authorization, logging, and caching without cluttering your controller logic. Filters can be applied declaratively using attributes or programmatically, offering flexibility in how you handle these concerns. Web API supports several types of filters, each serving a specific purpose in the request pipeline. Understanding these filter types helps you choose the right approach for implementing various functionalities in your API. Types of Filters Web API Filter Pipeline ...

Read More

How to do versioning with the Querystring parameter in C# ASP.NET WebAPI?

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

Querystring parameter versioning in ASP.NET Web API allows different API versions to be accessed using a query parameter like ?v=1 or ?v=2. By default, the DefaultHttpControllerSelector cannot route to version-specific controllers, so we need to create a custom controller selector. This approach enables you to maintain multiple API versions simultaneously while using descriptive controller names like StudentsV1Controller and StudentsV2Controller. How It Works The default controller selection process looks for a controller named exactly as specified in the route (e.g., StudentsController). When you have version-specific controllers like StudentsV1Controller, the default selector fails and returns a 404 error. ...

Read More

How to do versioning with accept header in C# ASP.NET WebAPI?

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

The Accept header in HTTP tells the server what file format the client wants the response data in. These formats are commonly called MIME-types (Multipurpose Internet Mail Extensions). In ASP.NET Web API, you can use the Accept header to implement API versioning by including version information as a parameter in the Accept header. This approach allows you to maintain multiple API versions while using the same URL endpoints, routing requests to different controllers based on the version specified in the Accept header. How Accept Header Versioning Works When a client makes a request, it includes an Accept ...

Read More
Showing 31–40 of 1,951 articles
« Prev 1 2 3 4 5 6 196 Next »
Advertisements