开发者

equivalent to list() of php in C#

there some method equal list() of php in C#?

usage list() in PHP:

$array = array('foo','baa'); 
list($foo, $baa)  = $array; 

echo $f开发者_StackOverflow中文版oo; //foo
echo $baa; //baa

equivalent in javascript:

var arr = ['foo','baa']; 
var foo;
var baa; 
[foo, baa] = arr; 

Thanks in advance!


There is no direct language equivalent in C#. While collection initializers can be used to construct the array, there is no way to extract elements directly.

This requires explicitly setting variables, ie:

var theArray = GetArrayFromSomewhere();
var foo = theArray[0]; 
var bar = theArray[1];


This is more similar to the original intention, though some might not like all the ceremony :

string a = null, b = null, c = null;

new ValList(v => a = v,
            v => b = v,
            v => c = v).SetFrom(new string[] { "foo", "bar" });

//here a is "foo", b will be "bar", and c is still null

And the simple helper class:

class ValList
{
    private Action<string>[] _setters;

    public ValList(params Action<string>[] refs)
    {
        _setters = refs;
    }

    internal void SetFrom(string[] values)
    {
        for (int i = 0; i < values.Length && i < _setters.Length; i++)
            _setters[i](values[i]);
    }
}


One way to do the original php (I know nothing about PHP) task in C# would be

        List<string> name_list = new List<string>{"transmogrify","untransmogrify"};
        name_list.ForEach(x => Debug.WriteLine(x));

Which leads to the more general observation, that C# allows you to do a lot while leaving the variables in the array or list. LINQ in particular makes doing many things quite simple. So if you are looking for a way to replicate some PHP code in C# I would think in those terms. Just one parting example, if you had an array of ints you wanted to sum you could do this

       int[] some_ints = {1, 2, 3, 4};
       int sum += some_ints.Sum();


As Reed said , There is no direct language equivalent in C#.

But you can create it like below ↓

class Program
{
    static void Main(string[] args)
    {

        string[] tmp = new string[] { "foo", "baa" };

        string foo, baa;
        tmp.Go(out foo, out baa);

        Console.WriteLine(foo);
        Console.WriteLine(baa);

        Console.ReadKey();
    }
}

public static class PHPList
{
    public static void Go(this string[] soruce, out string p1, out string p2)
    {
        p1 = soruce[0] + "";
        p2 = soruce[1] + "";
    }
}


Just piggy-backing @Yoni's answer into an extension method. This was a cute and silly exercise; however, as has been pointed out in comments and answers, some language features simply do not port from one language to another.

In PHP, list($a, $b) = $c constitutes an assignment, and since no variable declarations are required (the list() is the declaration) it can provide terse and clean assignment syntax.

In C# however, since variable declarations are required prior to usage, you're better off simply assigning the value off the list at that time, as the following example will show.

Speaking of, incoming example:

public static void Into<TSource>(this IEnumerable<TSource> source,
    params Action<TSource>[] actions) 
{
    if (ReferenceEquals(source, null)) {
        throw new ArgumentNullException("source");
    }
    if (ReferenceEquals(actions, null)) {
        throw new ArgumentNullException("actions");
    }
    foreach (var assignment in actions.Zip(source, (action, item) => new {
        Action = action,
        Item = item,
    })) {
        assignment.Action.Invoke(assignment.Item);
    }
}

So you can simply collection.Into(o => a = o, o => ...); for example:

var numbers = new[] { 1, 2, 3, 4, 5 };

int a = 0, b = 0, c = 0, d = 0;
int e = 0, f = 0, g = 0, h = 0, i = 0, j = 0;

numbers.Into(o => a = o,
             o => b = o,
             o => c = o,
             o => d = o);

numbers.Into(o => e = o,
             o => f = o,
             o => g = o,
             o => h = o,
             o => i = o,
             o => j = o);

This will yield:

Console.WriteLine(a); // 1
Console.WriteLine(b); // 2
Console.WriteLine(c); // 3
Console.WriteLine(d); // 4
Console.WriteLine(e); // 1
Console.WriteLine(f); // 2
Console.WriteLine(g); // 3
Console.WriteLine(h); // 4
Console.WriteLine(i); // 5
Console.WriteLine(j); // 0

Perhaps some Expression<> magic can shorten it to:

numbers.Into(o => a,
             o => b,
             ... )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜