C# 初始化多个空列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14362805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:36:09  来源:igfitidea点击:

initialising multiple empty lists

c#

提问by Adrian Hand

Simple syntactic c# question is this.

简单的 C# 语法问题是这样的。

Given this code:

鉴于此代码:

List<string> Columns = new List<string>();
List<string> Parameters = new List<string>();
List<string> Values = new List<string>();

It can be reduced to:

它可以简化为:

List<string> Columns = new List<string>(), Parameters = new List<string>(), Values = new List<string>();

But can I get it shorter still, since they're all being initialised to an empty list?

但是我可以把它缩短吗,因为它们都被初始化为一个空列表?

Thank you all!

谢谢你们!

采纳答案by Ilya Ivanov

I can recommend to use varkeyword if these variables are not class fields, because types are know from usage. It really makes no sense to flat declaration of three variables.

var如果这些变量不是类字段,我可以推荐使用关键字,因为类型是从用法中知道的。平面声明三个变量确实没有意义。

var Columns = new List<string>();
var Parameters = new List<string>();
var Values = new List<string>();

Yes, you can do things, like declaring multiple local variables in one line and then initializing them in one line. But please, avoiddeclaring multiple variables in one line - it makes code much readable.

是的,你可以做一些事情,比如在一行中声明多个局部变量,然后在一行中初始化它们。但是请避免在一行中声明多个变量 - 它使代码更具可读性。

回答by JLRishe

This is not a great practice, but if you're initializing string lists a lot within a single .cs file, you can add an alias for the class with the file's other usingstatements:

这不是一个很好的做法,但如果您在单个 .cs 文件中初始化字符串列表,您可以使用文件的其他using语句为类添加别名:

using StringList = List<string>;

And then the code to declare and initialize them would be:

然后声明和初始化它们的代码将是:

StringList Columns = new StringList(), 
           Parameters = new StringList(), 
           Values = new StringList();

回答by Tim Medora

Purely as a point of trivia/code golf:

纯粹作为琐事/代码高尔夫的一点:

Func<List<string>> n = () => new List<string>();
List<string> a = n(), b = n(), c = n(), d = n(), e = n(), f = n();

But it would be ridiculous to use this in place of the much clearer constructs available. It mighthave value if the initialization was more complex, and the code was properly named and spaced.

但是用它代替可用的更清晰的结构是荒谬的。如果初始化更复杂,并且代码正确命名和间隔,它可能有价值。

Func<List<string>> setupFoo = () => {
    return new List<string>() { 1, 2, 3 };
};

var option1 = setupFoo();
var option2 = setupFoo();
var option3 = setupFoo();

回答by ken2k

You can't shorten anymore.

你不能再缩短了。

You coulduse some kind of "empty list factory":

可以使用某种“空列表工厂”:

public static List<string> EmptySList()
{
    return EmptyList<string>();
}

public static List<T> EmptyList<T>()
{
    return new List<T>();
}

...

List<string> Columns = EmptySList(), Parameters = EmptySList(), Values = EmptySList();

Honestly, you probably don't want to shorten your code. Just write something readable and maintainable:

老实说,您可能不想缩短代码。只需写一些可读和可维护的东西:

var columns = new List<string>();
var parameters = new List<string>();
var values = new List<string>();