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.
'C# > C# Tutorial' 카테고리의 다른 글
C# Operators | C# Tutorial for Beginners (0) | 2023.02.14 |
---|---|
C# Type Casting | C# Tutorial for Beginners (0) | 2023.02.14 |
C# Constants | C# Tutorial for Beginners (0) | 2023.02.14 |
C# Data Types | C# Tutorial for Beginners (0) | 2023.02.14 |
C# Basic Syntax | C# Tutorial for Beginners (0) | 2023.02.14 |