#20 – Literals

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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

4 Responses to #20 – Literals

  1. Pingback: #22 – Integer Literals « 2,000 Things You Should Know About C#

  2. kai zhou says:

    The first thing I come to the office is to read one or two posts in your website.
    Thank you Sean.

  3. Brendon Christoffersen says:

    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.”

  4. Brendon Christoffersen says:

    Woops bad comment. I understand now that literals and constants are two different things.

Leave a comment