[C#]初始化数组的三种方式及对比

【[C#]初始化数组的三种方式及对比】

string[] OK = { "Are you OK", "Hello", "Thank you","Thank you very much" };

这种初始化方式可以写在方法里,也可以写在方法外

string[] OK; OK = new string[4]{"Are you OK", "Hello", "Thank you","Thank you very much" };

这种初始化方式只能在方法里赋值,在方法外赋值系统会报错
[C#]初始化数组的三种方式及对比
文章图片



string[] OK = new string[4]; OK[0] = "Are you OK"; OK[1] = "Hello"; OK[2] = "Thank you"; OK[3] = "Thank you very much";

这种初始化方式只能在方法里赋值,在方法外赋值系统会报错
[C#]初始化数组的三种方式及对比
文章图片


    推荐阅读