---
title: "Basic of C# Programming with Constants and Literals"  
description: "C# is a powerful and modern programming language developed by Microsoft. It's widely used for developing desktop applications, web services."  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/articles/339098/basic-of-c-sharp-programming-with-constants-and-literals  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Basic of C# Programming with Constants and Literals

C# is a powerful and modern [programming language](https://www.mindstick.com/forum/156801/write-a-program-for-reverse-a-word-in-sentence-in-any-programming-language) developed by Microsoft. It's widely used for developing desktop applications, [web services](https://www.mindstick.com/articles/431/inserting-records-in-table-by-using-web-services), and [enterprise software](https://answers.mindstick.com/qa/30523/what-is-enterprise-software). As a beginner, understanding the basic [building blocks](https://www.mindstick.com/interview/2344/what-are-the-core-building-blocks-of-android) of C# is essential. In this article, we’ll explore **constants** and **literals**, two fundamental concepts in [C# programming](https://www.mindstick.com/forum/145552/c-sharp-introduction).

## What are Constants in C#?

A **constant** is a variable whose value cannot be changed once it is assigned. Constants are used to define fixed values that remain the same throughout the execution of a program.

## Syntax

```cs
const dataType constantName = value;
```

## Example

```cs
const double Pi = 3.14159;
const int DaysInWeek = 7;
```

In the example above:

- `Pi` is a constant with a value of 3.14159.
- `DaysInWeek` will always hold the value 7 and cannot be reassigned.

### Key Points:

Constants must be initialized at the time of declaration.

1. They are implicitly `static`.
2. The value of a constant is evaluated at compile time.

## Why Use Constants?

Using constants improves **code readability** and **maintainability**. For example, instead of using the number `7` everywhere in your code, using `DaysInWeek` makes your intent clearer.

## What are Literals in C#?

A **literal** is a fixed value that you assign to a variable or constant directly in your code. C# supports different types of literals depending on the data type.

### Types of Literals:

| Literal Type | Example | Description |
| --- | --- | --- |
| **Integer Literal** | `100`, `-42`, `0` | Represents whole numbers. Default type is `int`. |
| **Long Literal** | `100L`, `9999999999L` | Whole numbers suffixed with `L` for `long`. |
| **Unsigned Literal** | `100U`, `4294967295U` | Unsigned integers with `U` suffix (`uint`). |
| **ULong Literal** | `100000UL` | Unsigned long with `UL` suffix. |
| **Float Literal** | `3.14f`, `0.5F` | Decimal numbers with `f` or `F` suffix for `float`. |
| **Double Literal** | `3.14159`, `2.5D` | Decimal numbers (default type is `double`). Use `D` suffix optionally. |
| **Decimal Literal** | `100.25m`, `9999.99M` | High-precision decimal values (e.g., money). Use `m` or `M` suffix. |
| **Character Literal** | `'A'`, `'1'`, `'@'` | A single Unicode character enclosed in single quotes. |
| **String Literal** | `"Hello"`, `"123"`, `""` | Sequence of characters enclosed in double quotes. |
| **Verbatim String** | `@"C:\Path\To\File"` | Ignores escape sequences (starts with `@`). |
| **Boolean Literal** | `true`, `false` | Logical values representing truth. |
| **Null Literal** | `null` | Represents the absence of a value (for reference types). |
| **Hex Literal** | `0x1A3F` | Integer literal in hexadecimal format (prefix `0x`). |
| **Binary Literal** | `0b1010` | Integer literal in [binary format](https://answers.mindstick.com/qa/51767/how-to-convert-a-number-into-binary-format) (prefix `0b`). |
| **Underscore Separator** | `1_000_000` | Improves readability of numeric literals (ignored by compiler). |
| **Escape Sequences in Char/String** | `'\n'`, `"\t"`, `"\\n"` | [Special character](https://www.mindstick.com/forum/119/special-character-remove-from-url-or-any-string) codes like newline, tab, backslash, etc. |

> ## Tip:
>
> - Literals are fixed values written directly into the [source code](https://www.mindstick.com/interview/300/what-is-the-source-code-for-display-the-picture-in-button-click-event).
> - The compiler uses them to assign values to variables or constants.
> - Verbatim strings (`@""`) are especially useful when [working with file paths](https://www.mindstick.com/forum/161589/how-can-you-use-path-combine-and-path-getextension-when-working-with-file-paths) or [regular expressions](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table).

#### 1. Integer Literals

Used to represent whole numbers.

```cs
int age = 25;
```

#### 2. Floating-Point Literals

Used to represent decimal numbers.

```cs
float temperature = 36.6f;
double pi = 3.14159;
```

> Note: A `float` literal must end with `f`, while `double` can be used directly or with `d`.

#### 3. Character Literals

Represent a single character enclosed in single quotes.

```cs
char grade = 'A';
```

#### 4. String Literals

A sequence of characters enclosed in double quotes.

```cs
string message = "Welcome to C# programming!";
```

#### 5. Boolean Literals

Only two values: `true` or `false`.

```cs
bool isCompleted = true;
```

#### 6. Null Literal

Represents the absence of a value.

```cs
string name = null;
```

## Example: Using Constants and Literals

```cs
using System;

class Program
{
    const int MaxScore = 100;

    static void Main()
    {
        string studentName = "Alice";
        int studentScore = 85;

        Console.WriteLine("Student: " + studentName);
        Console.WriteLine("Score: " + studentScore + " out of " + MaxScore);
    }
}
```

## Output:

```plaintext
Student: Alice
Score: 85 out of 100
```

---

Original Source: https://www.mindstick.com/articles/339098/basic-of-c-sharp-programming-with-constants-and-literals

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
