Latest updates

[5]

Constant vs ReadOnly vs Static ReadOnly vs Static

Constant
 
*   Const can only be initialized at the time of declaration of the field.
 
*   Const values will evaluate at compile time only means Compiler   evaluates the const variables.
 *  Const value can’t be changed these will be same at all the time.
*   You cannot reassign a const variable.


The static modifier is not allowed in a constant declaration. By default constant are static, hence you  cannot define a constant type as static.


*   A const field of a reference type other than string can only be initialized with null.
*   You can apply const keyword to built-in value types (byte, short, int, long, char, float, double, decimal, bool), enum, a string literal, or a reference type which can be assigned with a value null
*   Constants can be marked as public, private, protected, internal, or protected internal access modifiers. Use the const modifier when you sure that the value of a field or local variable would not be changed.

ReadOnly

  •  A readonly field can be initialized either at the time of declaration or with in  the constructor of same class. Therefore, readonly fields can be used for run-time constants.
  • Explicitly, you can specify a readonly field as static since, like constant by default it is not static. Readonly keyword can be apply to value type and  reference type (which initialized by using the new keyword) both. Also,  delegate and event could not be readonly.
  •  Use the readonly modifier when you want to make a field constant at run time.
  • Read only values will evaluate at runtime only

        
Example:

public class Readonly
{
public const int i = 2;
public readonly int x;
public  Readonly()
{
x = 3;
}
}


Note: if you donot want to change the value of the constant , use a const or if you have a constant that may change o, use a readonly.


Static ReadOnly:

A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime




Static
 
The static keyword is used to specify a static member and common to all the objects .If static member is changed by one object, it is expressed by other object of the class means  they do not tied to a specific object. static keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
class MyClass
{
 static int age = 20;
 int i = 5;
 public static void Show()
 {
 Console.WriteLine(age);
 Console.WriteLine(i); //error, since you can access only static members
 } 




    Note:
  •    If the static keyword is applied to a class, all the members of the class must be static.
  •  Static methods can only access static members of same class. Static properties are used to get or set the value of static fields of a class.
  • Static constructor can't be parameterized. Access modifiers can not be applied on Static constructor, it is always a public default constructor which is used to initialize static fields of the class.


Difference between const and readonly 


  • const fields has to be initialized while declaration only, while readonly fields can be initialized at declaration or in the constructor.
  • const variables can declared in methods ,while readonly fields cannot be declared in methods.
  • const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
  • A const field is a compile-time constant, the readonly field can be used for run time constants.

Static Readonly Fields vs. Constants

A static readonly field in a class is very similar to a constant.  Both are a constant and static value with a little difference


As a static readonly field in a Tree  class:


 public static readonly string treeCatagory = "Tree with fruits";

 As a constant:


public const string treeCatagory = " Tree with fruits ";

The value of a constant must be available at compile-time and must be initialized at  the time of  declaration.

The value of a static readonly field should be available  at run-time. It is specified in either the declaration or within a static constructor.

Use a readonly field for values that you won’t know until run-time or in doubt.  Use a constant for values that are known and never change.




Constant vs ReadOnly vs Static ReadOnly vs Static Constant vs ReadOnly vs Static ReadOnly vs Static Reviewed by Admin on 04:04:00 Rating: 5

No comments:

Sora Templates