Difference between Statics vs Constant vs Read-Only in C#
1). The constant and read-only perform as a similar function on data members, but they have a few important differences. The constant member is defined at compile time and cannot be changed at runtime. The constants are declared as a field, using the const keyword and must be initialized as they are declared.
2). A static modifier is used to declare a static member, this means that the member is no longer tied to a specific object. A value belongs to the class, additionally, the member can be accessed without creating an instance of the class.
Only one copy of static fields and events exist, and static methods and properties can only access static fields and static events
3). A static keyword is used to specify a static member, which means static members are common to all the objects and they do not tie to a specific object. It (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.
4). The read-only field can be initialized either at the time of declaration or within the constructor of the same class. Therefore, read-only fields can be used for run-time constants.
5). The constant fields or local variables must be assigned a value at the time of declaration and after that, they cannot be modified. By default constant is static, hence you cannot define a constant type as static.
Liked By
Write Answer
Difference between Statics vs Constant in C#
Join MindStick Community
You have need login or register for voting of answers or question.
Anonymous User
12-Dec-2018Difference between Statics vs Constant vs Read-Only in C#
1). The constant and read-only perform as a similar function on data members, but they have a few important differences. The constant member is defined at compile time and cannot be changed at runtime. The constants are declared as a field, using the const keyword and must be initialized as they are declared.
2). A static modifier is used to declare a static member, this means that the member is no longer tied to a specific object. A value belongs to the class, additionally, the member can be accessed without creating an instance of the class.
Only one copy of static fields and events exist, and static methods and properties can only access static fields and static events
3). A static keyword is used to specify a static member, which means static members are common to all the objects and they do not tie to a specific object. It (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.
4). The read-only field can be initialized either at the time of declaration or within the constructor of the same class. Therefore, read-only fields can be used for run-time constants.
5). The constant fields or local variables must be assigned a value at the time of declaration and after that, they cannot be modified. By default constant is static, hence you cannot define a constant type as static.