Learn C# with OOPS Concept (Types of Variables)

abhishek kumarabhishek kumar
5 min read

We will learn Variables with coding so that strong concept will be build

As a programmer we must have knowledge of 5 Types of Variables, also keep in mind that declaration of each variables must be logical:-

  1. Instance readonly Variables.

  2. Instance Variables.

  3. Static variables.

  4. Static readonly variables.

  5. Constant variables.

Note:- In examples we are declaring the variables as "public" but it is not a good practice, By default variables are private and we have mechanism called "Property" through which we can access private variables(we will talk about later about this topic).

  1. Instance readonly variables
  • Those variables that are unique for the individual purpose and can't be changeable are Instance readonly variables.

  • For example, In company every employees have unique Employee code, these employee code will remain same until he/she works for the company.

  • One very important point is, the Instance readonly variables are only initialized by a constructor. but Why?

  • Go through the below example:-

As you can see by above example, whenever the block memory for a class is allocated in Heap by "new Company()" at that time, the memory of instance readonly variable "EmpID" is also allocated in heap. If we try to initialize the Instance readonly variable by object we will get compile time error as below example:-

  • So, by above proof we can say that Instance readonly variable are stored in Heap memory (Go to Part-1 for memory management) and it can initialized by Constructor only.

  • We can initialize the Instance readonly variable at class scope level but is not recommended and not a good practice at a project level.

  • We can access the Instance readonly only variable by an object of a class, that's why it is also called object level variable.

  • Below is the example of Instance readonly only variable:-

  1. Instance Variables
  • Those variables that can changeable at any moment and based on individual purpose are instance variables.

  • Examples are:- Employee Salary, Designation are individual variables and it is not permanent, it can change according to employees performance etc.

  • These variables can be initialized by an object, by function/method, by constructor and by property also.

  • It is also known as object level variable because we can access the instance variables by an object of class.

  • Instance variables are also stored in heap memory.

  • Lets take an example, in example we will initialize the instance variable by object as well by an function/method:-

Note:- Instance readonly variables and Instance variables are object level variables because we can access these variables by an object of class.

  1. Static Variables
  • Those variables that can be changeable and it is Global in nature.

  • These variables are said to be global variables because of its characteristics and behavior, means the declaration of static variables must be done at class scope only, if we try to declare these variables in function/method and in constructor we will get compile time error.

  • These variables are stored at Global Memory, which is a top-level part of Heap Memory.

  • We can initialize these variables by an static constructor, static function, static property and by an class Name.(dot).

  • We can access static variable by an class name only not by an object.

  • Examples are:- Company Email Address, Phone Number are global and can be changeable whenever needed.

  • lets take an example:-

  • As you can see in above example that static variable is initialized by class Name.

  • Yes we can initialize static variable by an constructor also during the creation of an object but why we use memory unnecessary by creating an object.

  • If initialization of static variable is done by an class name then it is recommended not to create an object of a class specially for static variables.

  1. Static readonly Variables
  • Variables that can't be changeable so frequently and it is global in nature.

  • The declaration of static readonly variables must done at class scope otherwise we will get compile time error.

  • These variables are stored in Global Memory.

  • The most important point is that, the initialization of static readonly variable will be done by static constructor only.

  • The initialization can be done at the point of declaration and we can re-initialize by an static constructor only, but it is not recommended. try to initialize by static constructor only.

  • We can access these variables by an Class Name only.

  • Below is example of Initialization and re-initialization of static readonly memory.

  • One important point is that static constructor runs/called before the Main() function, means we don't need to allocate memory of an class in Main() function. for static constructor.

  • One more important point is that static constructor is used for initialization of internal mechanism of application. We learn later about static constructor in inheritance.

  • Examples are:- Tax Policy, Leave Policy, HR Policy, are can't be changeable so frequently and are global in nature.

  • Example of initialization by an static constructor and called before the Main() function:-

  1. Constant Variables:-
  • Variables that can't be changeable at any point, means it is fixed and Global in nature.

  • The declaration and initialization of constant variable must be done at same point. Re-initialization are not possible for Constant variables.

  • One important point is that we can declare and initialize the constant variable at class scope level as well as within function/method.

  • We can access these variables by an class Name only if it is declare and initialized at class scope otherwise by an object/instance if it is declared and initialized within the function/method.

  • Examples are:- Company Established Year, Date of Birth of an Employee are fixed that can't be changeable at any situation.

  • Below is the example of Constant variable at class level as well within function/method level

Hope you understand well and like about Types of Variables of a program.

In Next part We will discuss about Functions/Methods, little bit about constructor, static constructor etc.

For Part-2, please click on this link:- Part-2 where we discuss about Memory Management of a code/application/program.

Thanks Everyone!!

0
Subscribe to my newsletter

Read articles from abhishek kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

abhishek kumar
abhishek kumar

Serves 5+ years in IT field as a Website Administrator, E-Office Administrator, Web Developer, Full stack developer in different phase of Software Development Life Cycle(SDLC) including development, Implementation and testing.