为什么列表声明在 C# 中不起作用
Why does List declaration is not working in c#
我正在尝试像下面的代码一样声明列表。
public List<string> t1,t2,t3 = new List<string>();
当我尝试向 List 添加内容时,它在 运行 时给我错误 was null 但不给我编译时错误。
public List<string> t1 = new List<string>();
public List<string> t2 = new List<string>();
public List<string> t3 = new List<string>();
虽然个人声明工作正常。
请有人解释一下,提前谢谢你。
您可以在一行中声明多个变量,如果它们属于同一类型:
// Declare and initialize three local variables with same type.
// ... The type int is used for all three variables.
//
int i = 5, y = 10, x = 100;
Console.WriteLine("{0} {1} {2}", i, y, x);
我正在尝试像下面的代码一样声明列表。
public List<string> t1,t2,t3 = new List<string>();
当我尝试向 List 添加内容时,它在 运行 时给我错误 was null 但不给我编译时错误。
public List<string> t1 = new List<string>();
public List<string> t2 = new List<string>();
public List<string> t3 = new List<string>();
虽然个人声明工作正常。
请有人解释一下,提前谢谢你。
您可以在一行中声明多个变量,如果它们属于同一类型:
// Declare and initialize three local variables with same type.
// ... The type int is used for all three variables.
//
int i = 5, y = 10, x = 100;
Console.WriteLine("{0} {1} {2}", i, y, x);