Just the reinforcement @Yoni answers the extension method. It was a sweet and silly exercise; however, as noted in the comments and answers, some language functions simply cannot be transferred from one language to another.
In PHP, list($a, $b) = $c represents the assignment, and since no variable declarations are required ( list() is the declaration), it can provide a concise and clean assignment syntax.
However, in C #, since variable declarations are required before use, you better just assign a value from the list at this time, as shown in the following example.
Speaking of the following 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 just collection.Into(o => a = o, o => ...); eg:
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 give:
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 the magic of Expression<> can shorten it to:
numbers.Into(o => a, o => b, ... )