{"id":3403,"date":"2011-11-11T13:26:59","date_gmt":"2011-11-11T13:26:59","guid":{"rendered":"https:\/\/www.techopedia.com\/definition\/constant\/"},"modified":"2012-01-03T12:27:01","modified_gmt":"2012-01-03T12:27:01","slug":"constant","status":"publish","type":"definition","link":"https:\/\/www.techopedia.com\/definition\/3768\/constant-c","title":{"rendered":"Constant"},"content":{"rendered":"
In the context of C#, a constant is a type of field or local variable whose value is set at compile time and can never be changed at run time. It is similar to a variable by having a name, a value, and a memory location. However, it differs from the variable by its characteristic of getting initialized only once in the application. A constant is declared using the keyword "const".<\/p>\n
Variables of built-in type, excluding System.Object whose non-changing value is known at compile time, can be declared as constants. During compilation, the constant value is substituted for its literal value into the intermediate language code by the compiler. This results in better integrity of the application by reducing accidental bugs. Usage of constants also improves performance over regular variables. It improves code readability and provides better maintenance, since it is easier to update the constant value in a single place before recompilation of software.<\/p>\n
A constant has the following characteristics or must adhere to the following rules: <\/p>\n
Some of the best practices while using constants include: <\/p>\n
A constant differs from the readonly variable in that the former needs to be initialized during declaration time and is static, while the latter can be initialized during declaration or any one time (like in a constructor so as to have different values based on the constructor type). Hence, a constant is called a compile-time constant, and a readonly variable is a runtime constant.<\/p>\n","protected":false},"excerpt":{"rendered":"
What Does Constant Mean? In the context of C#, a constant is a type of field or local variable whose value is set at compile time and can never be changed at run time. It is similar to a variable by having a name, a value, and a memory location. However, it differs from the […]<\/p>\n","protected":false},"author":7813,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_acf_changed":false,"_lmt_disableupdate":"","_lmt_disable":"","om_disable_all_campaigns":false,"footnotes":""},"definitioncat":[273,244,216],"class_list":["post-3403","definition","type-definition","status-publish","format-standard","hentry","definitioncat-devops","definitioncat-programming-languages","definitioncat-software-development"],"acf":[],"yoast_head":"\n