Posts

Showing posts from March 28, 2025

Ref in .NET Core

Introduction to ref in .NET Core In .NET Core, the ref keyword is used to pass arguments by reference to methods, rather than by value. When a parameter is passed by reference, any changes made to the parameter within the method are reflected in the calling code. This differs from the default behavior in C# where arguments are passed by value (meaning a copy of the argument is made). The ref keyword allows you to modify the original value of a variable from within a method, making it an essential tool for scenarios where you need to update the value of a variable in the caller's scope. How ref Works in .NET Core When a parameter is passed by reference, both the caller and the callee share the same memory location for that parameter. As a result, changes made to the parameter inside the method will affect the original argument in the calling code. Here’s an example: Syntax of ref public void SomeMethod(ref int number) { ...

Static Constructors in .NET Core

Introduction to Static Constructors In .NET Core,  static constructors  are used to initialize the type itself, rather than individual instances of a class. A static constructor is a special constructor that is called once, before any static members of the class are accessed or any instance of the class is created. This allows developers to perform any one-time setup tasks, such as initializing static fields, logging configuration, or setting up other static resources. A static constructor is called  automatically  by the runtime when the class is first accessed. Unlike instance constructors, which are invoked when an object is instantiated, static constructors are designed for one-time initialization of static data or performing actions that are shared across all instances of the class. Syntax of a Static Constructor A static constructor does not take any parameters, and it is defined with the static keyword. Here’s the syntax for defining a static construct...