C# Literal Values: Strings, Numbers and Booleans

Duration: 10 Minutes
Level: Beginner
Audience: Students, Developers, Educators
Track: .NET Development


Introduction

In this exercise, you will practice printing different types of literal values in C# and understand why data types are essential. Literal values are constants that never change during program execution. For example, printing a “Hello” message to the console uses a literal string.

You’ll also learn how to combine hard-coded values with dynamic data collected from users, using variables of specific types. This exercise will help you confidently create and display literal values of various data types in your applications.


What is a Literal Value?

A literal value is a fixed constant that does not change. Previously, you printed a literal string to the output console, like "Hello". This literal is written exactly as it appears, which is why it’s called a literal.

Tip: Use the string data type for words, phrases, or any alphanumeric data meant for display or presentation, not for calculation.


Exercise – Print Different Literal Data Types

C# supports many data types, but you’ll focus on the several common types for now. Let’s see how to print them:


1. Character Literals (char)

To display a single UTF-16 code unit, use a char literal with single quotes:

Console.WriteLine('b');

Output:

b

Using multiple characters in single quotes will cause an error:

Console.WriteLine('Hello');

Error: CS1012 – Too many characters in character literal.


2. Integer Literals (int)

Use int literals for whole numbers:

Console.WriteLine(123);

Output:

123

3. Floating-Point Literals (float, double, decimal)

Float: append F or f to the number:

Console.WriteLine(0.25F);

Double: default for numbers with decimals:

Console.WriteLine(2.625);

Decimal: append M or m for high-precision fractional values:

Console.WriteLine(12.39816m);

Output Example:

0.25
2.625
12.39816

Float has the least precision; decimal is ideal for financial calculations.


4. Boolean Literals (bool)

Use bool literals to represent true/false:

Console.WriteLine(true);
Console.WriteLine(false);

Output:

True
False

Boolean values are used extensively in decision-making and logical evaluations.


Why Emphasize Data Types?

C# strongly enforces data types to reduce common programming errors. Each data type defines what you can do with the value:

  • string / char → for presentation/text

  • int / decimal → for calculations

  • bool → for logical conditions

Example: Phone numbers or postal codes are numeric but usually stored as strings because we don’t perform calculations on them.

Also, note that literal appearance may be similar to strings, but the operations you can perform differ:

Console.WriteLine("123"); // string
Console.WriteLine(123);   // int

Console.WriteLine("true"); // string
Console.WriteLine(true);   // bool

Recap

For now, focus on these common literal types:

  • string → Words, phrases, alphanumeric display

  • char → Single UTF-16 code unit

  • int → Whole numbers

  • decimal → Fractional numbers

  • bool → True/False values

These are the building blocks for working with data in C# applications.

Continue learning

Further reading: Microsoft documentation.

Literal values: common questions

Why does a decimal literal often need an m suffix?

A real-number literal such as 1.5 is a double by default. The suffix m selects decimal, while f selects float. Choose the suffix that matches the intended numeric type rather than treating every value with a decimal point as the decimal type.

Practice and check

Compare the string "12" with the integer 12. The first contains text characters; the second supports arithmetic. Also compare the character 'A' with the string "A": they display similarly but have different types.

Comments

Popular posts from this blog

C# Student Grades: Format Console Output

C# Console Challenge Solution: Write vs WriteLine

C# String Formatting Challenge: Solution Explained