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.
public class Const_VS_Readonly
{
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
public Const_VS_Readonly()
{
I_RO_VALUE = 3;
}
}
Key points about Static keyword
- 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 and public. Static constructor is always a private default constructor which is used to initialize static fields of the class.
No comments:
Post a Comment