Constants

A constant is a meaningful name that takes the place of a value that does not change.
You should always use constants in place of hard coded values or strings.
Constants are accessed as if they were static fields.
Constants that are being accessed outside the class must be prefixed with the class name.
Constants can be marked as public, private, protected, internal or protected internal
Constants are immutable values which are known at compile time and do not change for the life of the program.
Constants must be initialized when it is declared.
Unlike variables, constants cannot be inadvertently changed at run-time.
You cannot use any variables, user defined function or any built-in functions in your declarations.


Constant Scope

The default scope for constants is private.
Constants have scope and this scope is identical to that defined for variables.

public const int month = 12; 

Multiple constants of the same type can be declared on the same line.

const int months = 12, weeks = 52, days = 365; 

Expressions

An expression can be used that is based on other constants
Assuming there is no circular reference.


Literal Constants

A specific value such as a number, date or text string that cannot change that is hard coded.
more details


Symbolic Constants

A literal constant that is represented by a name.
more details


Enumerations

A grouping of symbolic constants into categories.
Enumerations are value types which default to integers.
The enum type enables you to define named constants
more details


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext