What is the difference between literal and constant in C#?


The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.

An integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal.

150
300u

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part.

3.14159
235468E-7F

String literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

@"Welcome User"
"Welcome User"

Updated on: 21-Jun-2020

352 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements