Static Constructor in C# with Examples

Description:

In the previous post, We have discussed about the Default constructor, Parameterized constructor, and Copy constructor.

Now in this post, We will learn about the static constructor with examples in C#.

C# Static Constructor

A  static  constructor is a special kind of constructor that invokes automatically before the first instance of a class is created.
It is used to initialize only static data members of the class or to write the code that needs to be executed only once throughout the execution of the program.

The purpose of using a static constructor is to trigger a piece of code automatically without creating an object of a class.

Static Constructor Syntax

 // Class
    public class Customer
    {
        // Static Constructor
      static Customer()
        {
            // Code
        }
    }

In the above code syntax, a static constructor is created without any parameter and access modifier.

Continue reading “Static Constructor in C# with Examples”

Constructors in C# with Examples

What is a Constructor in C#?

In C#, Constructors are special types of methods that get executed when an instance of the class or struct is created.

The main use of the constructor is to initialize and set the default values for the data members of the new object. It looks very similar to a method, but with no return type and have the same name as the class or struct.

A constructor is invoked when we use the new operator or the reflection (Example: Activator.CreateInstance) to create an object of a class.

If we forget to define the default constructor in a class, the compiler will automatically create one system-defined default constructor for that class.

C# Constructor Syntax

Syntax to create a constructor in C#

constructor syntax

In the above example, we have created constructor “User” with the same name as class or struct.

Continue reading “Constructors in C# with Examples”

Difference Between Const, static, ReadOnly and Static ReadOnly variables in C#

Introduction

In this article, we will discuss about the keywords const, read-only, static and static-read-only in details.

Static in C#

In C#,  static  is a keyword that can be applied to classes, variables, methods, properties, constructor and events.

The static modifier is useful to create a single copy of an object (class or data members) to share common data among all the instances.
The static modifier makes a class or data member non-instantiable, that means if we mark a class as static it will not allow us to create an object using the  new  operator.
A class member declared as static can be accessed directly by its class name.

In C# the static keyword is used to define static members which are common to all the objects and not bound to any specific object.

Static variable memory allocation

Static variable in C#
Static variable memory allocation in high-frequency heap

In C#, Static and constant data exist for the lifetime of the application, therefore they stored in a high frequency heap and can’t be garbage collected.
A high frequency heap is a kind of loader heap that exists for each AppDomain where all the static data are stored.

Continue reading “Difference Between Const, static, ReadOnly and Static ReadOnly variables in C#”

Difference between Ref and Out keywords in C#

Introduction

In C# ref and out keywords are basically used to pass arguments as a reference within a method.

With the ref keyword, we have the choice to not make changes to the parameter inside the called method. Whereas using the out keyword, we must initialize the parameter inside the called method.

Ref keyword

The ref keyword in C# is used to pass an argument as a reference. That means any changes to the variable value inside the method will get reflected in the original variable in the calling method.

In C# ref keyword returns the address of the variable rather than the value.

Continue reading “Difference between Ref and Out keywords in C#”

Difference Between String and StringBuilder in C#

String in C#

In C#, String is a keyword that is used to hold the text value. It is an object of System.String type.

String is immutable (unchangeable), the value once assigned to a String object cannot be modified after its creation.

If we try to change the value of the String object, it will create a new instance in a different memory location with the modified value.
In C#, a string is a series of characters that is used to represent text.

String memory allocation

Following is the pictorial representation of memory allocation for the string.

String memory allocation in heap
String memory allocation in the heap memory

Continue reading “Difference Between String and StringBuilder in C#”

Page 4 of 5
1 2 3 4 5