C# C# Fundamentals

C# Variable Declaration and Initialization

This example demonstrates variable declaration and initialization in C# using common data types such as int, float, double, decimal, char, string, and bool. It also demonstrates three ways to display variable values: concatenation, string interpolation, and String.Format.

Beginner 25 Minutes Monopoly IT Solutions Updated 2026-09-01
.NET 10 class training banner
5.0
5.0
4.6

10 AM Class

Overview

What you'll cover

Learning objectives

  • Understand variable declaration and initialization in C#
  • Identify common C# data types
  • Initialize variables with appropriate literal suffixes
  • Display variable values using concatenation
  • Display variable values using string interpolation
  • Display variable values using String.Format
  • Understand positional format placeholders such as {0}, {1}, and {2}

Prerequisites

  • C# Basics
  • Variables
  • Data Types
  • Console.WriteLine()
  • Basic String Handling

Related topics

  • C#
  • Variables
  • Variable Declaration
  • Variable Initialization
  • Data Types
  • String Concatenation
  • String Interpolation
  • String.Format
  • Console.WriteLine
  • C# Fundamentals
Theory

Concept breakdown

Variable Declaration and Initialization

A variable stores a value of a particular data type. In C#, a variable can be declared with its data type and initialized with a value in the same statement, as demonstrated in the example.

int

The int variable a is initialized with the integer value 100.

float

The float variable b is initialized with 12.23f. The f suffix indicates a float literal.

double

The double variable c is initialized with 23.45.

decimal

The decimal variable d is initialized with 34.56m. The m suffix indicates a decimal literal.

char

The char variable e is initialized with the character 'A'.

string

The string variable f is initialized with the text Hello, World!.

bool

The bool variable g is initialized with the Boolean value true.

String Concatenation

The example uses the + operator to combine text with variable values, such as "a value is :" + a.

String Interpolation

The example uses the $ prefix and expressions inside braces, such as $"a value is : {a}".

String.Format

The example uses placeholders such as {0} and passes the corresponding variables as arguments to Console.WriteLine().

Syntax
Hands-on example

C# Variable Declaration and Initialization Example

Console Output
> "a value is :100"
> "b value is :12.23"
> "c value is :23.45"
> "d value is :34.56"
> "e value is :A"
> "f value is :Hello, World!"
> "g value is :True"
> ""
> "a value is : 100"
> "b value is : 12.23"
> "c value is : 23.45"
> "d value is : 34.56"
> "e value is : A"
> "f value is : Hello, World!"
> "g value is : True"
> ""
> "a value is : 100"
> "b value is : 12.23"
> "c value is : 23.45"
> "d value is : 34.56"
> "e value is : A"
> "f value is : Hello, World!"
> "g value is : True"
> ""
> "a value is : 100"
> "b value is : 12.23"
> "c value is : 23.45"
> "d value is : 34.56"
> "e value is : A"
> "f value is : Hello, World!"
> "g value is : True"
Walkthrough

Step by step

  1. 1

    Import Namespaces

    The program imports System, System.Collections.Generic, and System.Text.

  2. 2

    Declare the Namespace

    The Class3 class is placed inside the ConApp1 namespace.

  3. 3

    Declare and Initialize Variables

    Seven variables are declared and initialized using int, float, double, decimal, char, string, and bool.

  4. 4

    Display Using Concatenation

    The + operator combines each descriptive string with its corresponding variable value.

  5. 5

    Display Using String Interpolation

    The $ prefix enables expressions such as {a} to be embedded directly inside the string.

  6. 6

    Display Using String.Format

    Numbered placeholders such as {0} are used and the corresponding variable is supplied as an argument.

  7. 7

    Display Multiple Values with String.Format

    The final Console.WriteLine uses placeholders from {0} through {6} and supplies all seven variables in the same order.

Summary

Key takeaways

Key points

  • Variables are declared with a data type and variable name.
  • Variables can be initialized at the time of declaration.
  • The example uses int, float, double, decimal, char, string, and bool.
  • The float literal uses the f suffix.
  • The decimal literal uses the m suffix.
  • String concatenation uses the + operator.
  • String interpolation uses the $ prefix and braces.
  • String.Format-style formatting uses numbered placeholders.
  • The final statement maps {0} to a, {1} to b, through {6} to g.

Advantages

  • Clear and simple variable declarations.
  • Strong typing makes the intended data type explicit.
  • String interpolation provides readable formatting.
  • String.Format supports numbered placeholders and multiple values.
  • The example demonstrates several common ways to display data.

Disadvantages

  • Concatenation can become harder to read when many values are combined.
  • String.Format with many numbered placeholders can be less readable because values must be matched by position.
  • Using many variables without meaningful names can reduce code readability.
Real-world use

Displaying Employee Information

An application may declare variables such as employee ID, salary, department, employee name, and active status, then display those values using concatenation, interpolation, or formatted strings.

Interview prep

Frequently asked questions

Variable declaration specifies the data type and name of a variable.

Variable initialization assigns an initial value to a variable.

int, float, double, decimal, char, string, and bool.

The f suffix identifies the numeric literal as a float literal.

The m suffix identifies the numeric literal as a decimal literal.

String concatenation combines strings and values, using the + operator in this example.

String interpolation allows expressions to be embedded inside a string using the $ prefix and braces.

It uses numbered placeholders such as {0} and supplies the corresponding variables as arguments.

The bool variable g is initialized with true.

Test yourself

Quick MCQ practice

1. Which data type is used for variable a?

2. Which suffix is used for the float literal 12.23?

3. Which suffix is used for the decimal literal 34.56?

4. Which variable stores the character A?

5. Which variable stores Hello, World!?

6. Which variable is of type bool?

7. Which operator is used for concatenation in the example?

8. Which syntax represents string interpolation?

Apply it

Practice on your own

Coding exercise

C# Variable Declaration and Output Formatting

Declare variables using int, float, double, decimal, char, string, and bool. Initialize them with suitable values and display them using concatenation, string interpolation, and String.Format.

Assignment

  1. Declare variables using int, float, double, decimal, char, string, and bool.
  2. Initialize each variable with a suitable value.
  3. Display all values using string concatenation.
  4. Display all values using string interpolation.
  5. Display all values using String.Format-style placeholders.
  6. Display multiple variables in a single formatted Console.WriteLine statement.

Tags

#csharp#variables#variable declaration#variable initialization#data types#string concatenation#string interpolation#string format#console writeline#beginner

getintouch

Book for Live Demo!