Variables and Data Types

Variables and Data Types

In C#, variables are used to store data of different types, such as numbers, text, and Boolean values. Each variable has a name, a data type, and a value.

Here’s an example of declaring and initializing a variable in C#:

int num = 10;

In this example, “num” is the name of the variable, “int” is the data type (which stands for integer), and “10” is the value assigned to the variable.

C# supports several different data types, including:

Integer types (such as int, short, and long)

Floating-point types (such as float and double)

Character types (such as char)

Boolean type (bool)

String type (string)

Here’s an example of declaring and initializing variables of different data types in C#:

int age = 30;

double height = 1.75;

char gender = ‘M’;

bool isMarried = false;

string name = “John”;

In this example, “age” is an integer variable, “height” is a double variable, “gender” is a character variable, “isMarried” is a Boolean variable, and “name” is a string variable.

It’s important to choose the right data type for your variables based on the type of data you want to store. Using the correct data type can help ensure that your code is efficient and avoids errors.

Variables in C# are also strongly typed, which means that the data type of a variable is checked at compile time. If you try to assign a value of the wrong data type to a variable, the compiler will raise an error.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Blocks
Variable Operations

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?