![]() |
VOOZH | about |
A constant is a variable whose value cannot be changed once assigned. It is defined using the "const" keyword and is used to store fixed values in a program these values are called literals. These values are assigned to variables and remain unchanged during program execution.
24 100
In the above code, the variable 'c' is a constant whose value cannot be changed. The value 100 assigned to variable b is a literal, which is directly written in the program.
A constant is a named value that remains unchanged throughout the program. It must be initialized at the time of declaration using the const keyword. Constants are useful when a value should not be modified during execution. Let's take a look at an example:
24
In the above code, the variable c is a constant whose value cannot be changed. Its value is determined at compile time and embedded directly into the program.
const with integer type. The value cannot be changed after initialization.float or double. Float constants require a suffix 'f'.' '). The value remains unchanged during execution." "). The value cannot be modified after assignment.10 3.14 A Hello True
A literal represents a fixed value written directly in the program. It does not have a name and is used to assign values to variables during initialization. Literals remain unchanged during execution. Let's take a look at an example:
100
In the above code, the value 100 assigned to the variable x is a literal. It is a fixed value directly written in the program and used to initialize the variable. This value remains constant during program execution.
100 3.14 A Hello True
Constants and literals are often confused as the same, but in C#, they are different entities with distinct meanings.
Features | Constants | Literals |
|---|---|---|
Definition | A constant is a named variable whose value cannot change. | A literal is a fixed value directly written in the code. |
Declaration | Declared using the const keyword. | No declaration required. |
Storage | Stored in memory with a name (identifier). | Used directly without any name. |
Example | const int x = 10; | 10, "Hello", true |