C# Variables

HTML
CSS
Bootstrap
JavaScript
C#
SQL
Salesforce Admin
Exercise
Study Material

Variables

Introduction to Variables in C#:

Variables in C# are containers for storing data values. They have a type that defines what kind of data they can hold and a name that identifies them within a program. C# is a statically typed language, meaning that you must declare the type of each variable before you can use it. Here are some commonly used data types in C#:

1. int: Represents integer numbers.
2. float: Represents single-precision floating-point numbers.
3. double: Represents double-precision floating-point numbers.
4. char: Represents a single Unicode character.
5. bool: Represents Boolean values (true or false).
6. string: Represents a sequence of characters.

Declaring Variables:

Variables in C# are declared using the following syntax:

<type> <name>;

Here, <type> is the data type of the variable, and <name> is the identifier for the variable.

Variable Initialization:

Variables can also be declared and initialized in a single step:

<type> <name> = <value>;

Examples:

Integer Variable:

int age;

int quantity = 10;

age = 25;

Console.WriteLine(“Quantity: ” + quantity);

Console.WriteLine(“Age: ” + age);

Floating-Point Variable:

float temperature;

float pi = 3.14f;

temperature = 98.6f;

Console.WriteLine(“Pi: ” + pi);

Console.WriteLine(“Temperature: ” + temperature);

Character Variable:

char grade;

char symbol = ‘@’;

grade = ‘A’;

Console.WriteLine(“Symbol: ” + symbol);

Console.WriteLine(“Grade: ” + grade);

Boolean Variable:

bool isRaining;

bool isActive = false;

isRaining = true;

Console.WriteLine(“Is Active? ” + isActive);

Console.WriteLine(“Is it raining? ” + isRaining);

String Variable:

string name;

string message = “Hello, world!”;

name = “John”;

Console.WriteLine(“Message: ” + message);

Console.WriteLine(“Name: ” + name);

Course Video