C#/C# Tutorial

C# Variables | C# Tutorial for Beginners

DragonTory 2023. 2. 14. 18:02
반응형

 C# Variables 

In C#, a variable is a named storage location that holds a value. 

Before you can use a variable in C#, you need to declare it with a data type.

To declare a variable, you use the following syntax:

data_type variable_name;

Here's an example of declaring a variable:

1
2
int age;
 
cs

In this example, we're declaring a variable called age of type int.

To assign a value to a variable, you use the assignment operator (=). 

Here's an example:

1
2
3
4
int age;
age = 30;
 
 
cs

In this example, we're declaring a variable called age of type int and assigning the value 30 to it in a single statement.

You can change the value of a variable at any time by assigning a new value to it. 

Here's an example:

1
2
3
int age;
age = 30;
age = 35;
cs

In this example, we're changing the value of the age variable from 30 to 35.

It's important to choose meaningful variable names to make your code more readable and easier to understand. 

You can also use comments to explain the purpose of the variables in your code.

반응형