0
0
C Sharp (C#)programming~5 mins

Variable declaration and initialization in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Variables store information you want to use later. Declaring and initializing them sets up a name and a value to remember.

When you want to save a number to use in calculations.
When you need to keep text like a name or message.
When you want to store a true/false answer.
When you want to change a value step-by-step in your program.
When you want to remember user input or results.
Syntax
C Sharp (C#)
type variableName = value;

type is the kind of data (like int, string, bool).

You can declare a variable without a value, but it's best to initialize it right away.

Examples
Declares an integer variable named age and sets it to 25.
C Sharp (C#)
int age = 25;
Declares a string variable named name and sets it to "Alice".
C Sharp (C#)
string name = "Alice";
Declares a boolean variable named isSunny and sets it to true.
C Sharp (C#)
bool isSunny = true;
Declares a double variable named price and sets it to 19.99.
C Sharp (C#)
double price = 19.99;
Sample Program

This program declares three variables with different types and prints their values.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        int age = 30;
        string city = "Paris";
        bool isRaining = false;

        Console.WriteLine($"Age: {age}");
        Console.WriteLine($"City: {city}");
        Console.WriteLine($"Is it raining? {isRaining}");
    }
}
OutputSuccess
Important Notes

Variable names should be clear and meaningful.

Use camelCase for variable names in C# (start with lowercase letter).

Once declared, you can change the variable's value later if needed.

Summary

Variables hold data with a name and a type.

Declaration sets the variable's type and name.

Initialization gives the variable its first value.