In this tutorial, we will learn about variables in C# language. We will cover the basics of declaring and using variables, including naming conventions.
Variables in C# are used to store data values that can be manipulated during program execution. They have a data type that determines the kind of data they can hold, such as integers, characters, or floating-point numbers.
When naming variables in C#, follow these conventions:
The syntax to declare variables in C# is:
data_type variable_name;
num
.using System;
class Program
{
static void Main()
{
// Declare and initialize an integer variable
int num = 10;
// Print the value of the variable
Console.WriteLine("The value of num is: " + num);
}
}
The value of num is: 10
letter
.using System;
class Program
{
static void Main()
{
// Declare and initialize a character variable
char letter = 'A';
// Print the value of the variable
Console.WriteLine("The value of letter is: " + letter);
}
}
The value of letter is: A
pi
.using System;
class Program
{
static void Main()
{
// Declare and initialize a floating-point variable
float pi = 3.14f;
// Print the value of the variable
Console.WriteLine("The value of pi is: " + pi);
}
}
The value of pi is: 3.14