为什么List声明在c#中不起作用[重复]

问题描述 投票:-2回答:1

这个问题在这里已有答案:

我试图像下面的代码一样声明List。

public List<string> t1,t2,t3 = new List<string>();

当我尝试向List添加内容时,它会在运行时给出错误为null但不会给出编译时错误。

public List<string> t1 = new List<string>();
public List<string> t2 = new List<string>();
public List<string> t3 = new List<string>();

虽然个人宣言工作完美。

请有人解释一下,并提前感谢你。

c# list variable-declaration
1个回答
1
投票

如果它们属于同一类型,您可以在一行中声明多个变量:

        // 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);
© www.soinside.com 2019 - 2024. All rights reserved.