How to declare and initialize a variable in C#?

In C#, you can declare and initialize a variable using the following syntax:

// 声明并初始化一个整型变量
int num = 10;

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

// 声明并初始化一个布尔变量
bool isSunny = true;

// 声明并初始化一个数组变量
int[] numbers = new int[] { 1, 2, 3, 4, 5 };

// 声明并初始化一个对象变量
Person person = new Person { Name = "Alice", Age = 30 };

In the example above, the type of the variable is specified at declaration and then initialized using the equals sign. If the variable is a complex type (such as an array or object), you can use the new keyword to create an instance and initialize it.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds