Equivalent to list () php in C #

is there some kind of method equal to php list () in c #?

using list () in PHP:

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

equivalent in javascript:

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

Thanks in advance!

+6
source share
5 answers

There is no direct language equivalent in C #. Although collection initializers can be used to build an array, there is no way to directly retrieve elements.

This requires explicitly setting variables, for example:

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

This is more like the original intention, although some may not like the whole 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 a 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]); } } 
+3
source

One way to do the original php task (I don't know anything about PHP) in C # would be

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

This leads to a more general view that C # allows you to do a lot by leaving variables in an array or list. LINQ in particular makes many things pretty simple. Therefore, if you are looking for a way to replicate PHP code in C #, I would think in these terms. Just one divorce example, if you have an array of ints that you would like to summarize, you could do this

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

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

But you can create it as 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] + ""; } } 
0
source

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, ... ) 
0
source

Source: https://habr.com/ru/post/895209/


All Articles