#20 – Literals
July 7, 2010 4 Comments
A literal in C# is a representation, in your source code, of a constant value. You can use literals when assigning a value to a variable, when passing a parameter value to a method, or wherever a variable of the corresponding type is expected.
C# defines six different types of literals:
- Boolean literal: true | false
- Integer literal: e.g. 12, 24L, 100UL, 0x7DC
- Real literal: e.g. 1.0, 2.2F, 4, 6.02E23, 123.45m
- Character literal: e.g. ‘a’, ‘Z’, ‘#’, ‘\n’, ‘\”, ‘\\’, ‘\x45’, ‘\x04DA’
- String literal: e.g. “Sean”, @”Print\nMe”, “Look: \x04DA”, “Look: Ӛ”, “Line 1\nLine 2”
- Null literal: null
Pingback: #22 – Integer Literals « 2,000 Things You Should Know About C#
The first thing I come to the office is to read one or two posts in your website.
Thank you Sean.
Hi Sean, studying through all your C# posts one by one and learning lots, great work! I found literals a little hard to understand and found this little statement that helped tie together everything you explained. “The constants refer to fixed values that the program may not alter during execution.”
Woops bad comment. I understand now that literals and constants are two different things.