简体   繁体   English

在C#中列出列表的变量?

[英]Variable to list assignment in C#?

In PHP you can write 在PHP中,您可以编写

$arr = array(1,2);
list($a, $b) = $arr;

Which is basically the equivalent of 基本上相当于

$a = $arr[0];
$b = $arr[1];

Is there an equivalent in C#? C#中有等效的东西吗?


Just bugs me because so often I write things like 烦我,因为我经常写这样的东西

var split = action.Split('.');
string controllerName = split[0];
string actionName = split[1];

And split is just a throw-away variable that I can never think of a decent name for. split只是一个扔掉的变量,我永远也不会想到一个像样的名字。 "chunks", "bits", "pieces", "parts",... all meaningless jibberish. “块”,“位”,“件”,“零件”,……毫无意义。

You could write your own method, like: 您可以编写自己的方法,例如:

int[] arr = new[] { 1, 2 };

int a, b;
Populate(arr, out a, out b);

static void Populate<T>(T[] arr, out T t1, out T t2)
{
    t1 = arr[0];
    t2 = arr[1];
}

I wouldn't recommend it, though...You'd have to be careful about having the right number of parameters, and I don't think there's a way to do an arbitrary size - C# has the concept of "params array" in the signature, but I don't think you can do it with "out" parameters. 我不建议这样做,但是...您必须小心使用正确数量的参数,而且我认为没有一种方法可以实现任意大小-C#具有“参数数组”的概念在签名中,但我认为您无法使用“ out”参数来实现。

Other than this? 除了这个?

a = arr[0];
b = arr[1];

No. 没有。

不幸的是,这是不可能的。

To expand on Joe's solution , the input doesn't have to be an array. 为了扩展Joe的解决方案 ,输入不必是数组。 It can be an IEnumerable<T> , allowing you to pass any source of data. 它可以是IEnumerable<T> ,允许您传递任何数据源。 And once you do that, it starts looking like it should be an extension method. 一旦完成,它开始看起来应该是一种扩展方法。 Furthermore, rather than always assuming that there will be as many items in the collection as there are input parameters, sometimes it's convenient to allow mismatches in numbers. 此外,与其总是假设集合中的项目和输入参数一样多,有时允许数字不匹配也很方便。

public static void AssignTo<T>(this IEnumerable<T> source, out T dest1, out T dest2)
{
    using (var e = source.GetEnumerator())
    {
        dest1 = e.MoveNext() ? e.Current : default(T);
        dest2 = e.MoveNext() ? e.Current : default(T);
    }
}

Then this code: 然后这段代码:

string x, y;
"x".Split(',').AssignTo(out x, out y);
Console.WriteLine(x + ", " + y);
"x,y".Split(',').AssignTo(out x, out y);
Console.WriteLine(x + ", " + y);
"x,y,z".Split(',').AssignTo(out x, out y);
Console.WriteLine(x + ", " + y);

will output: 将输出:

x,
x, y
x, y

Why would you ever want to allow the wrong size list passed in? 为什么您要允许传入错误的尺寸列表? Let's say you're parsing query strings. 假设您正在解析查询字符串。 In Python you would want to say key, value = query.split('=') but that won't work because key is a valid query and you could get key=value=value too, both of which would cause an exception. 在Python中,您可能想说出key, value = query.split('=')但是那是行不通的,因为key是有效的查询,您也可能会得到key=value=value ,两者都会导致异常。 Ordinarily you'd have to write 通常你必须写

string[] kv = query.Split('=');
string key = kv[0];
string value = kv.Length > 1 ? kv[1] : null;

but instead you can just write 但是你可以写

string key, value;
query.Split('=').AssignTo(out key, out value);

If you require the exact number of arguments though, just throw an exception instead of assigning null: 如果您需要确切数量的参数,则抛出异常而不是分配null:

public static void AssignToExact<T>(this IEnumerable<T> source, out T dest1, out T dest2)
{
    using (var e = source.GetEnumerator())
    {
        if (e.MoveNext()) dest1 = e.Current;
        else throw new ArgumentException("Only 0 of 2 arguments given", "source");
        if (e.MoveNext()) dest2 = e.Current;
        else throw new ArgumentException("Only 1 of 2 arguments given", "source");
        if (e.MoveNext()) throw new ArgumentException("More than 2 arguments given", "source");
    }
}

没有与您的第一个示例等效的内容。

等价于您在第二个示例中编写的内容,

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

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