Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Nizamuddin Siddiqui
Page 21 of 196
What is dependency inversion principle and how to implement in C#?
The Dependency Inversion Principle (DIP) is one of the five SOLID principles in object-oriented design. It states that high-level modules should not depend on low-level modules. Both should depend on abstractions. Additionally, abstractions should not depend on details — details should depend on abstractions. This principle helps reduce tight coupling between classes, making code more flexible, testable, and maintainable. By depending on abstractions rather than concrete implementations, you can easily swap out dependencies without modifying existing code. Key Rules of Dependency Inversion High-level modules should not depend directly on low-level modules. Both high-level ...
Read MoreWhat is the purpose of Program.cs file in C# ASP.NET Core project?
The Program.cs file in ASP.NET Core serves as the entry point for your web application. It contains the Main() method that bootstraps and configures the web host, essentially turning your web application into a console application that can be executed. Purpose and Structure ASP.NET Core web applications are console applications at their core. The Program.cs file is responsible for − Creating and configuring the web host Setting up default configurations and services Specifying the startup class Running the application Syntax Following is the basic structure of Program.cs in ASP.NET Core − ...
Read MoreWhat is the difference between All and Any in C# Linq?
The Any() and All() methods are LINQ extension methods that return bool values based on predicate conditions. Any() returns true if at least one element matches the predicate, while All() returns true only if every element matches the predicate. Both methods provide overloads − one that accepts a predicate and another parameterless version that simply checks if the collection contains any elements. Syntax Following is the syntax for Any() method − bool result = collection.Any(); bool result = collection.Any(predicate); Following is the syntax for All() method − bool result = collection.All(predicate); ...
Read MoreWhat is Metapackage in C# Asp.net Core?
A metapackage in ASP.NET Core is a special type of NuGet package that doesn't contain any actual code or DLLs itself, but instead serves as a collection of dependencies on other packages. The most common example is the Microsoft.AspNetCore metapackage, which is automatically included in many ASP.NET Core project templates. When you install a metapackage, it automatically brings in all its dependent packages, providing a convenient way to include multiple related packages with a single reference. What is Microsoft.AspNetCore Metapackage? The Microsoft.AspNetCore metapackage is designed to provide the essential packages needed for a basic ASP.NET Core application. ...
Read MoreHow to run multiple async tasks and waiting for them all to complete in C#?
In C#, there are several ways to run multiple asynchronous tasks and wait for them all to complete. The two main approaches are Task.WaitAll (synchronous blocking) and Task.WhenAll (asynchronous non-blocking). Task.WaitAll blocks the current thread until all tasks have completed execution, while Task.WhenAll returns a task that completes when all provided tasks have finished, without blocking the calling thread. Syntax Following is the syntax for using Task.WaitAll − Task.WaitAll(task1, task2, task3); Following is the syntax for using Task.WhenAll − await Task.WhenAll(task1, task2, task3); Using Task.WhenAll (Non-blocking) The Task.WhenAll ...
Read MoreHow to handle errors in middleware C# Asp.net Core?
Error handling in ASP.NET Core middleware provides a centralized way to catch and process exceptions that occur during HTTP request processing. By creating custom exception middleware, you can standardize error responses and logging across your entire application. Custom exception middleware intercepts exceptions before they reach the client, allowing you to log errors, format responses consistently, and hide sensitive internal details from users. Creating Custom Exception Middleware First, create a folder named CustomExceptionMiddleware and add a class ExceptionMiddleware.cs inside it. This middleware will handle all unhandled exceptions in your application − using System; using System.Net; using ...
Read MoreHow to get formatted JSON in .NET using C#?
JSON formatting in C# allows you to control how JSON data is presented when serialized. The Newtonsoft.Json library provides formatting options through the Formatting enumeration to make JSON output more readable or compact as needed. Syntax Following is the syntax for serializing objects with different formatting options − string json = JsonConvert.SerializeObject(object, Formatting.Indented); string json = JsonConvert.SerializeObject(object, Formatting.None); Formatting Options Option Description Use Case Formatting.None No special formatting is applied. This is the default. Compact JSON for APIs and storage Formatting.Indented Child objects are ...
Read MoreWhat is the AddSingleton vs AddScoped vs Add Transient C# Asp.net Core?
Dependency injection in ASP.NET Core provides three service lifetime options that determine when service instances are created and disposed. These service lifetimes − AddSingleton, AddScoped, and AddTransient − control the lifecycle of your registered services. Understanding these lifetimes is crucial for proper memory management and ensuring your application behaves correctly across different requests and users. Syntax All three methods follow the same registration syntax in the ConfigureServices method − public void ConfigureServices(IServiceCollection services) { services.AddSingleton(); services.AddScoped(); services.AddTransient(); } ...
Read MoreHow to implement Single Responsibility Principle using C#?
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 MoreHow do I copy items from list to list without foreach in C#?
There are several efficient methods to copy items from one list to another in C# without using a foreach loop. These methods provide different approaches depending on whether you need a complete copy, partial copy, or want to append items to an existing list. Using List Constructor The most straightforward method is to use the List constructor that accepts an IEnumerable parameter. This creates a new list with all elements from the source list − using System; using System.Collections.Generic; class Program { public static void Main() { ...
Read More