Several times I read the difference between Readonly and const keyword in C#. But most of the time when I remember the difference, again and again, mismatch the concept ..Finlay, write simple code.
public class Diff_ConstNReadonly
{
public const int cons_Value = 2;
public readonly int readOnly_value;
public Diff_ConstNReadonly()
{
readOnly_value = 3;
}
}
Differences:
References: Thanks Who make understand the Difference easily
public class Diff_ConstNReadonly
{
public const int cons_Value = 2;
public readonly int readOnly_value;
public Diff_ConstNReadonly()
{
readOnly_value = 3;
}
}
Differences:
Const:
1. Const can only be
initialized at the time of declaration of the field.
2.
Const
values will evaluate at compile time only.
3.
Const
value
can’t be changed these will be same at all the time.
4.
This type of fields are required when one
of the field values remains constant throughout the system like Pi will remain
same in your Maths class.
Read-only:
1.
The value will be initialized either declaration
time or the constructor of the class allowing you to pass the value at run time.
2.
Read
only values will evaluate at runtime only.References: Thanks Who make understand the Difference easily
Comments
Post a Comment