How to declare variables and constants in C#?

The way to declare variables and constants in C# is as follows:

Declare variables.

// 声明一个整型变量
int num;

// 声明并初始化一个字符串变量
string name = "Alice";

// 声明一个浮点型变量并赋值
float salary;
salary = 5000.50f;

Declare a constant:

// 声明一个整型常量
const int MAX_NUM = 100;

// 声明一个字符串常量
const string COMPANY_NAME = "ABC Company";

It is important to note that once a constant is assigned a value, it cannot be changed, whereas the value of a variable can be modified within the program.

Leave a Reply 0

Your email address will not be published. Required fields are marked *