C# Variables: Assignment & I/O Basics

In C# language, variables can be defined using the following methods:

// 声明变量并指定类型
int age;
string name;

// 声明并初始化变量
int age = 20;
string name = "John";

// 多个变量声明
int age, height;
string name, address;

Variables can be assigned values using the equal sign (=), for example:

int age = 20;
string name = "John";

age = 25; // 修改age的值为25
name = "Mike"; // 修改name的值为"Mike"

You can use the ReadLine and WriteLine methods provided by the Console class for input and output. For example:

// 输入
Console.WriteLine("请输入您的姓名:");
string name = Console.ReadLine();
Console.WriteLine("您的姓名是:" + name);

// 输出
int age = 20;
Console.WriteLine("年龄:" + age);

In the code above, Console.ReadLine() is used to receive input from the user, while Console.WriteLine() is used to output strings to the console.

bannerAds