Look at C# constants


const Variable:
1) It's a compile-time constant.
2) It can be used for primitive datatypes and string objects only. We cannot initialize constant using new operator.
3) It's always static by definition.
4) It need to be always initialized in declaration.
5) We can use it within method scope.
6) Replaces const variable with actual value in referred places in object code. Need to rebuild complete application to update const value changes.
7) Performance is slightly better than using readonly.
readonly Variable:
1) It's a runtime constant.
2) It can be used with any type.
3) It can be initialized in declaration or in constructor.
3) Cannot use within method scope.
4) Can be used for instance constants to store different values for each object.
5) Performance is low.
6) Variable name will not be replaced with value in object code. So, no need to rebuild application for readonly value changes.
Always prefer using readonly apart from performance critical applications.
Ebook Download
View all
Learn
View all