Literals in C#

In C#, literals are specific values that are written directly in the code. They are used to represent fixed values such as numbers, characters, and strings, which do not change throughout the program. Literals are the actual data you assign to variables or use in expressions.


using System;

class LiteralExamples
{
    static void Main()
    {
        // Numeric literals
        int integerLiteral = 42;
        double doubleLiteral = 3.14;

        // Character literals
        char charLiteral = 'A';

        // String literals
        string stringLiteral = "Hello, World!";

        // Boolean literals
        bool trueLiteral = true;
        bool falseLiteral = false;

        // Output the literals
        Console.WriteLine("Integer Literal: " + integerLiteral);
        Console.WriteLine("Double Literal: " + doubleLiteral);
        Console.WriteLine("Char Literal: " + charLiteral);
        Console.WriteLine("String Literal: " + stringLiteral);
        Console.WriteLine("Boolean Literal (true): " + trueLiteral);
        Console.WriteLine("Boolean Literal (false): " + falseLiteral);
    }
}

Running this program will produce the following output:


Integer Literal: 42
Double Literal: 3.14
Char Literal: A
String Literal: Hello, World!
Boolean Literal (true): True
Boolean Literal (false): False

This code demonstrates the use of various types of literals in C#. Each literal initializes a variable with a value directly in the code, such as 42 for an integer literal or "Hello, World!" for a string literal. The output shows these literals being printed to the console.

Here are some examples of literals in C#:

1- Numeric Literals:

Numeric literals represent numerical values. Some types of numeric literals are integers, floating-point numbers, or scientific notation. Examples include:

Numeric literals in C# are constant values that represent numerical data. They are used to assign specific numeric values directly to variables or expressions in your code. Numeric literals can take different forms, and they include:

  1. Integer Literals

    These represent whole numbers without a fractional or decimal part. Examples include:

    1. Decimal literals (base 10): In this form, the allowed digits are 0-9. i.e. 123
    2. Octal literals (Base 8): Octal literals, represented in base 8, utilize a set of permissible digits ranging from 0 to 7. To denote an octal number, it is necessary to prefix it with 0, as demonstrated in the following code example:
    3. Hexadecimal literals (base 16): In this form, the allowed digits are 0-9 and characters are a-f. i.e. 0x7F
    4. Binary literals(base 2): In this form, the allowed digits are only 1’s and 0’s. i.e. 0b1101
  2. Floating-Point Literals

    These represent numbers with a fractional or decimal part. Examples include:

    1. Single-precision (float): 3.14f
    2. Double-precision (double): 2.718
    3. Decimal: 123.45m
  3. Exponential Notation

    You can use exponential notation to represent numbers in scientific or engineering form. For example:

    1. 1.23e3 represents 1.23 * 10^3 (1230)
    2. 4.56E-2 represents 4.56 * 10^-2 (0.0456)
    Example:
    
    int integerLiteral = 10;
    float floatingPointLiteral = 3.14f;
    double doubleLiteral = 2.71828;
    decimal decimalLiteral = 123.45m;
    

    In the provided instances, the number 10 represents an integer literal, 3.14f exemplifies a float literal, 2.71828 serves as an example of a double literal, and 123.45m is utilized as a decimal literal.

2- Character and String Literals:

Character literals are individual characters that are wrapped in single quotation marks ('). String literals are used to denote sequences of characters that are enclosed within double quotation marks ("). Examples include:


char characterLiteral = 'A';
string stringLiteral = "Hello, world!";

In the above examples, 'A' is a character literal representing the letter 'A', and "Hello, world!" is a string literal representing the text "Hello, world!".

3- Boolean Literals:

Boolean literals serve as symbolic notations that communicate the two core truth values, specifically denoting "true" and "false", Examples include:


bool trueLiteral = true;
bool falseLiteral = false;

In the above examples, true and false act as boolean literals that stand for the logical values of true and false, respectively.

4- Null Literal:

The null literal represents a null value and is used for variables of reference types. Example:


string nullLiteral = null;

In the given instance, the term null serves as a null literal, indicating that there is no value assigned to the string variable.

5- Verbatim String Literal:

A verbatim string literal is used when you want to include special characters, such as newline or backslash, without escaping them. It is denoted by the @ symbol and this is preceded by the double quotes. Example:


string verbatimLiteral = @"C:\MyFolder\file.txt";

In the given example, @"C:\MyFolder\file.txt" stands for a verbatim string literal which corresponds to the file path "C:\MyFolder\file.txt".

These are just a few examples of literals in C#. Literals are placed in the code to signify unchanging values straightaway, eliminating the necessity for further calculations or actions. They offer an easy and straightforward method to specify values within the code.