Monday, November 1, 2021

C# Variables and their types


In C#, there are many types of fields in a class such as static fields, non-static fields, readonly fields and constant fields. The static, readonly and const keywords are used to identify them at the time of declaration of static fields, readonly fields and constant fields respectively. We will study about them in the next sections.

NON-STATIC FIELD
In the C#, non-static fields are fields that only need to be defined by their data type and field name. Non-static field can be initialized at the very time of declaration or it can be initialized with the help of constructor. Apart from this, the object can be initialized even after it is created. Initializing means to assigning a value to the field of the class according to its data type. For example,

using System;

namespace Console1
{
    public class program
    {
        int Age;
        string Name = "Ajeet";
        double salary;
        Program()
        {
            Salary = 20000;
        }
        Program(double salary)
        {
            this.Salary = Salary;
        }
        static void Main()
        {
            Program obj = new Program();
            Console.WriteLine(obj.Age);
            Console.WriteLine(obj.Name);
            obj.Age = 18;
            obj.Name = "Shrishesh";
            Console.WriteLine(obj.Age);
            Console.WriteLine(obj.Name);

            Program obj2 = new Program();
            obj2.Age = 36;
            obj2.Name = "Rina";
            Console.WriteLine(obj2.Age);
            Console.WriteLine(obj2.Name);
            Console.ReadKey();
        }
    }
}
STATIC FIELD
Non-static fields can also be initialized with the help of parameterized constructors. In contrast, static fields cannot be initialized with the help of parameterized constructor but static fields can be initialized with the help of parameterless constructor. The static keyword modifier is used at the time of declaration of static fields.
For example, in the following code, the static field is implicitly initialized through a constructor call.

public class program
    {
        static int RoomNo;
        static void Main()
        {
            Console.WriteLine("RoomNo is: "+RoomNo);
            Console.ReadKey();
        }
    }
For example, in the following code, the static field is initialized only at the time of declaration.
    static int RoomNo=20;
        static void Main()
        {
            Console.WriteLine("RoomNo is: "+RoomNo);
            Console.ReadKey();
        }
For example in the following code the static field is initialized with the help of constructor.
    public class program
    {
        static int RoomNo;
        static Program()
        {
            RoomNo = 30;
        }
        static void Main()
        {
            Console.WriteLine("RoomNo is: "+RoomNo);
            Console.ReadKey();
        }
    }
The following code is invalid because the static constructor must be parameterless. For example

public class program
    {
        static int RoomNo;
        static Program(int room)
        {
            RoomNo = room;
        }
        static void Main()
        {
            Console.WriteLine("RoomNo is: "+RoomNo);
            Console.ReadKey();
        }
    }
Both non-static fields and static fields can be initialized with the help of parameterless constructors.

The value of non static field may be different for different object but the value of static field is same for all objects i.e. value of static field is shared by all objects.

The initialization of a static field occurs at the very beginning of the life cycle of the class whereas the initialization of a non-static field occurs during object creation. Non-static fields can be initialized at any time and any number of times within the lifecycle of the class.
readonly field
Under C# programming, a readonly field can be a static field or a non-static field within any class. The readonly keyword is used with the readonly field at the time of declaration of the field.

It can be initialized at the time of declaration of readonly field or it can be initialized with the help of constructor.
For example, in the following code, the readonly field is initialized at the time of declaration.

public class program
    {
        readonly static int RoomNo=88;
        static void Main()
        {
            Console.WriteLine("RoomNo is: "+RoomNo);
            Console.ReadKey();
        }
    }
For example, in the following code, the readonly static field is initialized through a static constructor.

public class program
    {
        readonly static int RoomNo;
        static Program()
        {
            RoomNo = 333;
        }
        static void Main()
        {
            Console.WriteLine("RoomNo is: "+RoomNo);
            Console.ReadKey();
        }
    }
If a readonly field is a nonstatic field, it can be initialized at the time of declaration itself, or it can be initialized through an explicit constructor call at the time of object creation, but once initialized it cannot be re-initialized.
For example, in the following code, the readonly non-static field is initialized at the time of declaration.

public class Program
    {
        readonly int Age=18; // non static readonly field
        
        static void Main()
        {
            Program p = new Program();

            Console.WriteLine("Age is: "+p.Age);
            Console.ReadKey();
        }
    }
For example in the following code readonly non static field is initialized through parameterized constructor.

public class program
    {
        readonly int Age; // non static readonly field
        public Program(int Age)
        {
            this.Age = Age;
        }
        static void Main()
        {
            Program p1 = new Program(18);
            Program p2 = new Program(20);

            Console.WriteLine("Age is: "+p1.Age);
            Console.WriteLine("Age is: "+p2.Age);
            Console.ReadKey();
        }
    }


If the readonly field is a static field then it can be initialized at the time of declaration itself or it can be initialized by implicit constructor call at the beginning of life of the class but once it is initialized it cannot be re-initialized.

If a readonly field is a static field, its initialization occurs at the very beginning of the class's lifecycle lifecycle, whereas if a readonly field is a non-static field, it is initialized during the object creation.
For example in the following code readonly static field is initialized at the time of declaration.

public class program
    {
        readonly static DateTime date= new DateTime(2021,10,14);
        
        static void Main()
        {
            Console.WriteLine(date);
            Console.ReadKey();
        }
    }

CONSTANT FIELD
In C# programming, a constant field is a static field within any class whose initialization is done only at the time of declaration of the constant field. An exception is generated if the constant field is not initialized at the time of its declaration. Constant fields are identified by the const keyword. Note that although a constant field is a static field, the static keyword cannot be used with it, otherwise an exception is generated.
For example, the following code uses a constant field.

public class Program
    {
        const double PI = 3.1415;
        static void Main()
        {
            Console.WriteLine("Value of PI constant is "+PI.ToString());
            Console.ReadKey();
        }
    }

The following example represents the fields around the above

using System;
namespace VariablesType
{
    class program
    {
        int x; // non static field
        static int y; // static field
        const int z = 200; // constant field
        readonly int r=88; // non static readonly field
        readonly static int s=99; // static readonly field
        public Program(int r)
        {
            this.r = r;
        }
        static Program()
        {
            s = 200;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Value of variable y: "+y);
            Console.WriteLine("Value of variable z: "+z);
            Console.WriteLine("Value of variable s: "+s);
            Program p = new Program(800);
            p.x = 10;
            Console.WriteLine("Value of variable x: " + p.x);
            p.x = 15;
            Console.WriteLine("Value of variable x: " + p.x);
            y = 20;
            Console.WriteLine("Value of variable y: " + y);
            Console.WriteLine("Value of variable r: " + p.r);
            Console.ReadKey();
        }
    }
}

MAIN POINTS
  1. Non static variables are also called instance variables because the initialization of a non static variable or field occurs during the creation of an instance/object of a class. You can define the value of a non-static variable when each object or instance is created.
  2. A static variable or field is a class level field because such a field is accessed by all the objects of the class with the same value. The value of a static variable or field is defined at the beginning of the life cycle of the class. The creation of an object is not necessary for the initialization of a static variable or field. It is initialized at the very beginning of the life cycle of the class.
  3. A constant field is also a static field because its initialization does not require the creation of an object and its initialization happens as soon as the field is defined. Constant fields are always defined and initialized together.
  4. The readonly field behaves like an instance field without the static keyword. So its initialization requires the creation of the object. But readonly field cannot be re-initialized after object creation whereas instance field can be re-initialized after object creation.

Single copy and modifiable => static
Single copy and not modifiable => const
Multiple copy and modifiable => non static
Multiple copy and not modifiable => readonly

Edited on 5th July 2023

No comments:

Post a Comment

Hot Topics