简体   繁体   English

如何在声明它的同一行中初始化 C# 列表。 (IEnumerable 字符串集合示例)

[英]How can I initialize a C# List in the same line I declare it. (IEnumerable string Collection Example)

I am writing my testcode and I do not want wo write:我正在编写我的测试代码,但我不想写:

List<string> nameslist = new List<string>();
nameslist.Add("one");
nameslist.Add("two");
nameslist.Add("three");

I would love to write我很想写

List<string> nameslist = new List<string>({"one", "two", "three"});

However {"one", "two", "three"} is not an "IEnumerable string Collection".但是 {"one", "two", "three"} 不是“IEnumerable string Collection”。 How can I initialise this in one line using the IEnumerable string Collection"?如何使用 IEnumerable 字符串集合在一行中初始化它?

var list = new List<string> { "One", "Two", "Three" };

Essentially the syntax is:本质上的语法是:

new List<Type> { Instance1, Instance2, Instance3 };

Which is translated by the compiler as编译器将其翻译为

List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
list.Add("Three");

Change the code to将代码更改为

List<string> nameslist = new List<string> {"one", "two", "three"};

or

List<string> nameslist = new List<string>(new[] {"one", "two", "three"});

只是失去括号:

var nameslist = new List<string> { "one", "two", "three" };
List<string> nameslist = new List<string> {"one", "two", "three"} ?

去掉括号:

List<string> nameslist = new List<string> {"one", "two", "three"};

这取决于您使用的是哪个版本的 C#,从 3.0 版开始,您可以使用...

List<string> nameslist = new List<string> { "one", "two", "three" };

I think this will work for int, long and string values.我认为这适用于 int、long 和 string 值。

List<int> list = new List<int>(new int[]{ 2, 3, 7 });


var animals = new List<string>() { "bird", "dog" };

This is one way.这是一种方式。

List<int> list = new List<int>{ 1, 2, 3, 4, 5 };

This is another way.这是另一种方式。

List<int> list2 = new List<int>();

list2.Add(1);

list2.Add(2);

Same goes with strings.字符串也是如此。

Eg:例如:

List<string> list3 = new List<string> { "Hello", "World" };

Posting this answer for folks wanting to initialize list with POCOs and also coz this is the first thing that pops up in search but all answers only for list of type string.为想要使用 POCO 初始化列表的人发布此答案,因为这是搜索中弹出的第一件事,但所有答案仅适用于字符串类型的列表。

You can do this two ways one is directly setting the property by setter assignment or much cleaner by creating a constructor that takes in params and sets the properties.您可以通过两种方式做到这一点,一种是通过 setter 赋值直接设置属性,或者通过创建一个接受参数并设置属性的构造函数来更清晰。

class MObject {        
        public int Code { get; set; }
        public string Org { get; set; }
    }

List<MObject> theList = new List<MObject> { new MObject{ PASCode = 111, Org="Oracle" }, new MObject{ PASCode = 444, Org="MS"} };

OR by parameterized constructor OR 通过参数化构造函数

class MObject {
        public MObject(int code, string org)
        {
            Code = code;
            Org = org;
        }

        public int Code { get; set; }
        public string Org { get; set; }
    }

List<MObject> theList = new List<MObject> {new MObject( 111, "Oracle" ), new MObject(222,"SAP")};


        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM