Monday 6 January 2020

Real life examples on Constant, ReadOnly and Static in C#

Constant:

If we want to declare a variable with fixed value at the time of declaration then we can use constant. Say we want to use state "NY" in our entire class then we can declare a variable and immediately assign the value to it with "NY".
Why cannot we use ReadOnly or Static in this situation?
  1. ReadOnly variable can be used if we don't know the value at the time of declaration.
  2. Static variable can be changed which we don't want in case of constant
Some valid declaration
  1. const string STATE = "NY";
  2. public const Single PI = 3.14159;
  3. public const Single PI = 22/7;
  4. public const double GRAVITATION = 6.673e-11;
  5. //declare multiple constants
  6. public const int CENTURY = 100, HATRIC = 3, OVER = 6;
Valid declaration of constants with constants
  1.  public const int c1 = 5;
  2.  public const int c2 = c1 + 100;
  3.  public const int c3 = c1 + c2;
  4.  public const int c4 = c1 + c2 + c3;
Some in-valid declaration
  1.  public const int PI = x;
  2.  public const int PI = x/y;
  3.  public const double DISCOUNT = (price /100 ) * 3;
  4.  const string city = GetFromDB();
  • Value is evaluated at compile time
  • We cannot use variables to calculate the constants.
  • Constant fields and locals aren't variables
  • Constants cannot be modified
  • Constants can be of any data type or a null reference.
  • Constants are by default state so we cannot use static
  • Constants can be use to calculate the constants
  • Constants can have any access modifier
  • Constants can be accessed by using class name directy without creating instances because by default it is static, it cannot be accessed by using the instance of the class but inside the call without class name.
Some real life examples where we cannot use constant:
  • To store the price of a product
  • To store the student name
  • To store the brand name of a company
  • To store the values which we expect to change during the execution of the program

ReadOnly:

A ReadOnly variable is similar to constant but we can assign the value either at the time of declaration or in the constructor. Suppose you don't know the value at the time of declaration but you will know it at the time of initializing the object then we can use ReadOnly.
  1. If I know the value at the time of declaration then I will use constant.
  2. static variable can be changed but we don't want it in case of ReadOnly.
Some valid declaration
  1.  public readonly int age = 27;
  2.  public readonly single pi = 22 / 7;
  3.  public class Person
  4.  {
  5.     readonly int _age;
  6.     public Person()
  7.     {
  8.         _age = GetAverageUserAge();  // from database
  9.     }
  10.     public Person(int age)
  11.     {
  12.         _age = age;
  13.     }
  14.  }
Invalid: try to change the readonly value in a method,
  1. public class Age
  2. {
  3.     readonly int _year;
  4.     Age(int year)
  5.     {
  6.         _year = year;
  7.     }
  8.     void ChangeYear()
  9.     {
  10.         _year = 1967; // Compile error.
  11.     }
  12. }
  • ReadOnly Value is evaluated at run time.
  • ReadOnly can be either initialized in declaration or in the constructor
  • ReadOnly cannot change the value once constructor is called.
  • ReadOnly can be used with any data type
  • ReadOnly constants can have any access modifier
  • ReadOnly property can be change by using reflection see this how

Static:

Static fields and instance fields are two of the several kinds of variables supported by C#, and at times they are referred to as static variables and instance variables, respectively. A static field is not the part of any instance of the class, no matter how many instances of a class is created, there is only one copy of the static field, let's try to understand it with an example
  1. public  class Variable
  2. {
  3.     public int x = 5;
  4.     public void test()
  5.     {
  6.         x = x + 5;
  7.         Console.WriteLine(x);
  8.     }
  9. }
  10. public class Exercise
  11. {
  12.     static void Main()
  13.     {
  14.         var var = new Variable();
  15.         var.test();
  16.         var var1 = new Variable();
  17.         var1.test();
  18.         Console.ReadKey();
  19.     }
  20. }
In this example I used x as a normal variable without static keyword, it will give the result 10 and 10
  1. public  class Variable
  2. {
  3.     public static int x = 5;
  4.     public void test()
  5.     {
  6.         x = x + 5;
  7.         Console.WriteLine(x);
  8.     }
  9. }
  10. public class Exercise
  11. {
  12.     static void Main()
  13.     {
  14.         var var = new Variable();
  15.         var.test();
  16.         var var1 = new Variable();
  17.         var1.test();
  18.         Console.ReadKey();
  19.     }
  20. }
Now we used static keyword to declare the x variable, run the application and we will get 10 and 15 as a result.
It shows the static variable value shared among all the instances of the class.
  • Static field property and method can directly accessed by using the class name.
  • Static field, property and method cannot be accessed by using the instance.
  • Static field is shared for entire instances as we already checked with example.
  • Can be used where we only want to play on the passed values
  • Best example is Math class and it's methods.

No comments:

Post a Comment

Baisic Useful Git Commands

  Pushing a fresh repository Create a fresh repository(Any cloud repository). Open terminal (for mac ) and command (windows) and type the be...