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

发布于 2024-10-08 04:16:43 字数 370 浏览 3 评论 0原文

我正在编写我的测试代码,我不想写:

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

我很想写

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

但是{“一”,“二”,“三”}不是“IEnumerable字符串集合”。如何使用 IEnumerable 字符串集合在一行中初始化它?

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". How can I initialise this in one line using the IEnumerable string Collection"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

み零 2024-10-15 04:16:43
var list = new List<string> { "One", "Two", "Three" };

本质上语法是:

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

由编译器翻译为

List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
list.Add("Three");
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");
司马昭之心 2024-10-15 04:16:43

将代码更改为

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

List<string> nameslist = new List<string>(new[] {"one", "two", "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"});
夜唯美灬不弃 2024-10-15 04:16:43

只要去掉括号即可:

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

Just lose the parenthesis:

var nameslist = new List<string> { "one", "two", "three" };
唠甜嗑 2024-10-15 04:16:43

为那些想要使用 POCO 初始化列表的人发布此答案,因为这是搜索中弹出的第一件事,但所有答案仅适用于字符串类型列表。

您可以通过两种方式执行此操作,一种是通过 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"} };

或者通过参数化构造函数

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")};


        

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.

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.

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

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")};


        
小猫一只 2024-10-15 04:16:43

这是一种方法。

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

这是另一种方式。

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

list2.Add(1);

list2.Add(2);

字符串也是如此。

例如:

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

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" };
美男兮 2024-10-15 04:16:43
List<string> nameslist = new List<string> {"one", "two", "three"} ?
List<string> nameslist = new List<string> {"one", "two", "three"} ?
赠我空喜 2024-10-15 04:16:43

去掉括号:

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

Remove the parentheses:

List<string> nameslist = new List<string> {"one", "two", "three"};
没有心的人 2024-10-15 04:16:43

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

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

It depends which version of C# you're using, from version 3.0 onwards you can use...

List<string> nameslist = new List<string> { "one", "two", "three" };
梦里°也失望 2024-10-15 04:16:43

我认为这适用于 int、long 和 string 值。

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


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

I think this will work for int, long and string values.

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


var animals = new List<string>() { "bird", "dog" };
无力看清 2024-10-15 04:16:43

C# 12 (.NET 8+) 的方式是:

List<string> list = ["One", "Two", "Three"];

虽然不使用 var ,但它需要知道类型,因为新的集合文字可用于任何类型的集合。

来源:https://learn。 microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#collection-expressions

C# 12 (.NET 8+) way is:

List<string> list = ["One", "Two", "Three"];

though don't use var with it, it needs to know the type since the new collection literals can be used for any kind of collection.

Source: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#collection-expressions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文