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?
- ReadOnly variable can be used if we don't know the value at the time of declaration.
- Static variable can be changed which we don't want in case of constant
Some valid declaration
const string STATE = "NY";
public const Single PI = 3.14159;
public const Single PI = 22/7;
public const double GRAVITATION = 6.673e-11;
//declare multiple constants
public const int CENTURY = 100, HATRIC = 3, OVER = 6;
Valid declaration of constants with constants
public const int c1 = 5;
public const int c2 = c1 + 100;
public const int c3 = c1 + c2;
public const int c4 = c1 + c2 + c3;
Some in-valid declaration
public const int PI = x;
public const int PI = x/y;
public const double DISCOUNT = (price /100 ) * 3;
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.
- If I know the value at the time of declaration then I will use constant.
- static variable can be changed but we don't want it in case of ReadOnly.
Some valid declaration
public readonly int age = 27;
public readonly single pi = 22 / 7;
public class Person
{
readonly int _age;
public Person()
{
_age = GetAverageUserAge(); // from database
}
public Person(int age)
{
_age = age;
}
}
Invalid: try to change the readonly value in a method,
public class Age
{
readonly int _year;
Age(int year)
{
_year = year;
}
void ChangeYear()
{
_year = 1967; // Compile error.
}
}
- 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
public class Variable
{
public int x = 5;
public void test()
{
x = x + 5;
Console.WriteLine(x);
}
}
public class Exercise
{
- static void Main()
{
var var = new Variable();
var.test();
var var1 = new Variable();
var1.test();
Console.ReadKey();
}
}
In this example I used x as a normal variable without static keyword, it will give the result 10 and 10
public class Variable
{
public static int x = 5;
public void test()
{
x = x + 5;
Console.WriteLine(x);
}
}
public class Exercise
{
static void Main()
{
var var = new Variable();
var.test();
var var1 = new Variable();
var1.test();
Console.ReadKey();
}
}
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