ALERT

[FREE DEMO] Deploy Your Enterprise Cloud in Minutes

Constant

Definition - 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 variable by its characteristic of getting initialized only once in the application. A constant is declared using the keyword "const".

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.

Techopedia explains Constant

A constant has the following characteristics or must adhere to the following rules:

  • One or more constants of a given type can be declared in a single declaration.
  • A constant can be of any type, such as an sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, enum-type, or reference type.
  • A type of a constant should be at least as accessible as the constant itself.
  • A constant can depend on other constants if dependencies are not of a circular nature.
  • A constant cannot be used for a method, property, or event.
  • A constant cannot be of user-defined types, such as class, struct, and array.
  • While declaring a constant, a static modifier cannot be used.
  • Assigning an already assigned constant variable at runtime results in a compilation error.
  • As no address is associated with a constant at runtime, it cannot be passed by reference and cannot appear as an l-value in an expression.
  • A constant can be used with access modifiers such as public, private, protected, internal, or protected internal.
  • Constant declarations made at the class level are stored in assembly metadata.

Some of the best practices while using constants include:

  • Constants need to be initialized during declaration.
  • Constants are to be used with meaningful names as they represent special values.
  • To define multiple non-integral/integral constants, a single static class (containing constant member variables) can be used to group them.
  • The scope of the constant variable is limited to a single assembly, class, or method. Hence, while referring constant values defined in other assemblies, it has to be recompiled for any modification before compiling the dependent assembly.

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.

This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.