简体   繁体   English

在C#中是否有类似于PHPs list()的语言结构?

[英]Is there a language construct similar to PHPs list() in C#?

PHP has a language construct list() which provides multiple variables assignment in one statement. PHP有一个语言构造list() ,它在一个语句中提供多个变量赋值。

$a = 0;
$b = 0;
list($a, $b) = array(2, 3);
// Now $a is equal to 2 and $b is equal to 3.

Is there a similar thing in C#? C#中有类似的东西吗?

If not, is there any workaround which may help to avoid code like the following, without having to deal with reflection ? 如果没有,是否有任何解决方法可以帮助避免像下面这样的代码, 而不必处理反射

public class Vehicle
{
    private string modelName;
    private int maximumSpeed;
    private int weight;
    private bool isDiesel;
    // ... Dozens of other fields.

    public Vehicle()
    {
    }

    public Vehicle(
        string modelName,
        int maximumSpeed,
        int weight,
        bool isDiesel
        // ... Dozens of other arguments, one argument per field.
        )
    {
        // Follows the part of the code I want to make shorter.
        this.modelName = modelName;
        this.maximumSpeed = maximumSpeed;
        this.weight= weight;
        this.isDiesel= isDiesel;
        /// etc.
    }
}

No, I'm afraid there isn't any good way to do that, and code like your example gets written often. 不,我担心没有任何好的方法可以做到这一点,像你的例子这样的代码经常被写入。 It sucks. 太糟糕了。 My condolences. 节哀顺变。

If you're willing to sacrifice encapsulation for concision, you can use object initializer syntax instead of a constructor for this case: 如果您愿意为了简洁而牺牲封装,那么在这种情况下您可以使用对象初始化器语法而不是构造函数:

public class Vehicle
{
    public string modelName;
    public int maximumSpeed;
    public int weight;
    public bool isDiesel;
    // ... Dozens of other fields.
}

var v = new Vehicle {
    modelName = "foo",
    maximumSpeed = 5,
    // ...
};

I think you're looking for object and collection initializers . 我认为你正在寻找对象和集合初始化器

var person = new Person()
{
    Firstname = "Kris",
    Lastname = "van der Mast"
}

for example where Firstname and Lastname are both properties of the class Person. 例如,Firstname和Lastname都是Person类的属性。

public class Person
{
    public string Firstname {get;set;}
    public string Lastname {get;set;}
}

"Multiple variable initialization " or "Multiple variable assignment " ? “多变量初始化 ”或“多变量赋值 ”?

For initialization 用于初始化

$a = 0; 
$b = 0; 
list($a, $b) = array(2, 3); 

would be: 将会:

 int a=2, b=3;

For assignment, there's no shortcut. 对于作业,没有捷径。 It has to be two statement, but if you like, you can put the two statements on one line: 它必须是两个声明,但如果您愿意,可以将两个声明放在一行:

 a=2; b=3;

Yes - you can eliminate all the code in the constructor with object initializers (new for C# 3.0). 是的 - 您可以使用对象初始值设定项(C#3.0的新增功能)消除构造函数中的所有代码。 Here is a pretty good explanation: 这是一个很好的解释:

http://weblogs.asp.net/dwahlin/archive/2007/09/09/c-3-0-features-object-initializers.aspx http://weblogs.asp.net/dwahlin/archive/2007/09/09/c-3-0-features-object-initializers.aspx

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

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