Thursday, March 25, 2021

What is the startup class in ASP.NET core?

 Startup class is the entry point of the ASP.NET Core application. Every .NET Core application must have this class. This class contains the application configuration rated items. It is not necessary that class name must "Startup", it can be anything, we can configure startup class in Program class.

public class Program

 {

 public static void Main(string[] args)

 {

 CreateWebHostBuilder(args).Build().Run();

 }

 

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

 WebHost.CreateDefaultBuilder(args)

 .UseStartup<TestClass>();

 }

StringBuilder in C#

StringBuilder is a mutable string class in C# that provides better performance than the standard string class when you need to perform multiple string manipulations.

Key features of StringBuilder:

  1. It's located in the System.Text namespace
  2. It's mutable (unlike string, which is immutable)
  3. More efficient for multiple string operations
  4. Particularly useful when concatenating many strings

Basic usage:


using System;
using System.Text;

class Program
{
    static void Main()
    {
        // Create a new StringBuilder
        StringBuilder sb = new StringBuilder();
        
        // Append text
        sb.Append("Hello");
        sb.Append(" ");
        sb.Append("World!");
        
        // Convert to string when done
        string result = sb.ToString();
        
        Console.WriteLine(result); // Outputs: Hello World!
    }
}

Common methods:

  • Append() - Adds text to the end
  • AppendLine() - Adds text with a line break
  • Insert() - Inserts text at a specified position
  • Remove() - Removes characters
  • Replace() - Replaces specified characters
  • Clear() - Removes all characters
  • ToString() - Converts to a string

StringBuilder is particularly valuable in loops or when you're building strings iteratively, as it avoids creating multiple string objects in memory.

Would you like me to explain any specific aspect of StringBuilder in more detail?

Tuesday, March 23, 2021

we will understand how to set up middleware. Middleware in ASP.NET Core controls how our application responds to HTTP requests. It can also control how our application looks when there is an error, and it is a key piece in how we authenticate and authorize a user to perform specific actions.

 

  • Middleware are software components that are assembled into an application pipeline to handle requests and responses.

  • Each component chooses whether to pass the request on to the next component in the pipeline, and can perform certain actions before and after the next component is invoked in the pipeline.

  • Request delegates are used to build the request pipeline. The request delegates handle each HTTP request.

  • Each piece of middleware in ASP.NET Core is an object, and each piece has a very specific, focused, and limited role.

  • Ultimately, we need many pieces of middleware for an application to behave appropriately.

  • Let us now assume that we want to log information about every request into our application.

  • In that case, the first piece of middleware that we might install into the application is a logging component.

  • This logger can see everything about the incoming request, but chances are a logger is simply going to record some information and then pass along this request to the next piece of middleware.

  • Middleware is a series of components present in this processing pipeline.

  • The next piece of middleware that we've installed into the application is an authorizer.

  • An authorizer might be looking for specific cookie or access tokens in the HTTP headers.

  • If the authorizer finds a token, it allows the request to proceed. If not, perhaps the authorizer itself will respond to the request with an HTTP error code or redirect code to send the user to a login page.

  • But, otherwise, the authorizer will pass the request to the next piece of middleware which is a router.

  • A router looks at the URL and determines your next step of action.

  • The router looks over the application for something to respond to and if the router doesn't find anything to respond to, the router itself might return a 404 Not Found error.


Monday, March 22, 2021

Dependency Injection

 Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

The Dependency Injection pattern involves 3 types of classes.

  1. Client Class: The client class (dependent class) is a class which depends on the service class
  2. Service Class: The service class (dependency) is a class that provides service to the client class.
  3. Injector Class: The injector class injects the service class object into the client class.

Sunday, March 21, 2021

Difference between Static Constructors and Non Static Constructors in c#

 Static constructors are used to initializing the static members of the class and implicitly called before the creation of the first instance of the class. Non-static constructors are used to initializing the non-static members of the class. Below are the differences between the Static Constructors and Non-Static Constructors.

Parameters: We cannot pass any parameters to the static constructors because these are called implicitly and for passing parameters, we have to call it explicitly which is not possible. It will give runtime error. However, we can pass the parameters to the non-static constructors.

Overloading: Non-static constructors can be overloaded but not the static constructors. Overloading is done on the parameters criteria. So if you cannot pass the parameters to the Static constructors then we can’t overload it.

Cases in which the constructor will be implicit: Every class except the static class(which contains only static members) always contains an implicit constructor if the user is not defining any explicit constructor. If the class contains any static fields then the static constructors are defined implicitly.

Execution: Static constructor executes as soon as the execution of a class starts and it is the first block of code which runs under a class. But the non-static constructors executes only after the creation of the instance of the class. Each and every time the instance of the class is created, it will call the non-static constructor.


Times of Execution: A static constructor will always execute once in the entire life cycle of a class. But a non-static constructor can execute zero time if no instance of the class is created and n times if the n instances are created.

Initialization of fields: Static constructors are used to initialize the static fields and non-static constructors are used to initialize the non-static fields.

Declaration: Static constructors are declared using a static modifier explicitly while all other remaining constructors are non-static constructors. Non-static constructors can also be called as Instance Constructors as they need instance to get executed.

Calling: Static constructors are always called implicitly but the non-static constructors are called explicitly i.e by creating the instance of the class.



Wednesday, March 17, 2021

.NET Core Characteristics

 Open-source Framework: .NET Core is an open-source framework maintained by Microsoft and available on GitHub under MIT and Apache 2 licenses. It is a .NET Foundation project

Cross-platform: .NET Core runs on Windows, macOS, and Linux operating systems. There are different runtime for each operating system that executes the code and generates the same output.

Consistent across Architectures: Execute the code with the same behavior in different instruction set architectures,  including x64, x86, and ARM.

Wide-range of Applications: Various types of applications can be developed and run on .NET Core platform such as mobile, desktop, web, cloud, IoT, machine learning, microservices, game, etc.

Supports Multiple Languages: You can use C#, F#, and Visual Basic programming languages to develop .NET Core applications. You can use your favorite IDE, including Visual Studio 2017/2019, Visual Studio Code, Sublime Text, Vim, etc.

Modular Architecture: .NET Core supports modular architecture approach using NuGet packages. There are different NuGet packages for various features that can be added to the .NET Core project as needed. Even the .NET Core library is provided as a NuGet package. The NuGet package for the default .NET Core applicationmodel is Microsoft.NETCore.App.This way, it reduces the memory footprint, speeds up the performance, and easy to maintain.

CLI Tools: .NET Core includes CLI tools (Command-line interface) for development and continuous-integration.

Flexible Deployment: .NET Core application can be deployed user-wide or system-wide or with Docker Containers.

Compatibility: Compatible with .NET Framework and Mono APIs by using .NET Standard specification

Why .NET Core?

There are some limitations with the .NET Framework. For example, it only runs on the Windows platform. Also, you need to use different .NET APIs for different Windows devices such as Windows Desktop, Windows Store,Windows Phone, and Web applications. In addition to this, the .NET Framework is a machine-wide framework. Any changes made to it affect all applications taking a dependency on it.

Today, it's common to have an application that runs across devices; a backend on the web server, admin front-end on windows desktop, web, and mobile apps for consumers. So, there is a need for a single framework that works everywhere. So, considering this, Microsoft created .NET Core. The main objective of .NET Core is to make .NET Framework open-source, cross-platform compatible that can be used in a wide variety of verticals,from the data center to touch-based devices. 

What is .NET Core (.NET Core Overview)

 .NET Core is a new version of .NET Framework, which is a free, open-source, general-purpose development platform maintained by Microsoft. It is a cross-platform framework that runs on Windows, macOS, and Linux operating systems.

.NET Core Framework can be used to build different types of applications such as mobile, desktop, web,cloud, IoT, machine learning, microservices, game, etc..NET Core is written from scratch to make it modular, lightweight, fast, and cross-platform Framework. .NET Core application speed up the performance, reduce the memory footprint and becomes easy to maintain.