Skip to main content
edited body
Source Link

CONST

  1. const keyword can be applied to fields or local variables
  2. We must assign const field at time of declaration
  3. No Memory Allocated Because const value is embedded in IL code itself after compilation. It is like find all occurrences of const variable and replace by its value. So the IL code after compilation will have hard-coded values in place of const variables
  4. Const in C# are by default static.
  5. The value is constant for all objects
  6. There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
  7. Only C# built-in types can be declared as constant
  8. Const field can not be passed as ref or out parameter

ReadOnly

  1. readonly keyword applies only to fields not local variables
  2. We can assign readonly field at the time of declaration or in constructor,not in any other methods.
  3. dynamic memory allocated for readonly fields and we can get the value at run time.
  4. Readonly belongs to the object created so accessed through only through instance of class. To make it class member we need to add static keyword before readonly.
  5. The value may be different depending upon constructor used (as it belongs to object of the class)
  6. If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
  7. AsSince the value is obtained at run time, there is no dll versioning problem with readonly fields/ properties.
  8. We can pass readonly field as ref or out parameters in the constructor context.

CONST

  1. const keyword can be applied to fields or local variables
  2. We must assign const field at time of declaration
  3. No Memory Allocated Because const value is embedded in IL code itself after compilation. It is like find all occurrences of const variable and replace by its value. So the IL code after compilation will have hard-coded values in place of const variables
  4. Const in C# are by default static.
  5. The value is constant for all objects
  6. There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
  7. Only C# built-in types can be declared as constant
  8. Const field can not be passed as ref or out parameter

ReadOnly

  1. readonly keyword applies only to fields not local variables
  2. We can assign readonly field at the time of declaration or in constructor,not in any other methods.
  3. dynamic memory allocated for readonly fields and we can get the value at run time.
  4. Readonly belongs to the object created so accessed through only through instance of class. To make it class member we need to add static keyword before readonly.
  5. The value may be different depending upon constructor used (as it belongs to object of the class)
  6. If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
  7. As the value obtained at run time there is no dll versioning problem with readonly fields/ properties
  8. We can pass readonly field as ref or out parameters in the constructor context.

CONST

  1. const keyword can be applied to fields or local variables
  2. We must assign const field at time of declaration
  3. No Memory Allocated Because const value is embedded in IL code itself after compilation. It is like find all occurrences of const variable and replace by its value. So the IL code after compilation will have hard-coded values in place of const variables
  4. Const in C# are by default static.
  5. The value is constant for all objects
  6. There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
  7. Only C# built-in types can be declared as constant
  8. Const field can not be passed as ref or out parameter

ReadOnly

  1. readonly keyword applies only to fields not local variables
  2. We can assign readonly field at the time of declaration or in constructor,not in any other methods.
  3. dynamic memory allocated for readonly fields and we can get the value at run time.
  4. Readonly belongs to the object created so accessed through only instance of class. To make it class member we need to add static keyword before readonly.
  5. The value may be different depending upon constructor used (as it belongs to object of the class)
  6. If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
  7. Since the value is obtained at run time, there is no dll versioning problem with readonly fields/ properties.
  8. We can pass readonly field as ref or out parameters in the constructor context.
Source Link

CONST

  1. const keyword can be applied to fields or local variables
  2. We must assign const field at time of declaration
  3. No Memory Allocated Because const value is embedded in IL code itself after compilation. It is like find all occurrences of const variable and replace by its value. So the IL code after compilation will have hard-coded values in place of const variables
  4. Const in C# are by default static.
  5. The value is constant for all objects
  6. There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
  7. Only C# built-in types can be declared as constant
  8. Const field can not be passed as ref or out parameter

ReadOnly

  1. readonly keyword applies only to fields not local variables
  2. We can assign readonly field at the time of declaration or in constructor,not in any other methods.
  3. dynamic memory allocated for readonly fields and we can get the value at run time.
  4. Readonly belongs to the object created so accessed through only through instance of class. To make it class member we need to add static keyword before readonly.
  5. The value may be different depending upon constructor used (as it belongs to object of the class)
  6. If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
  7. As the value obtained at run time there is no dll versioning problem with readonly fields/ properties
  8. We can pass readonly field as ref or out parameters in the constructor context.