Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign values of array to separate variables in one line

Tags:

Can I assign each value in an array to separate variables in one line in C#? Here's an example in Ruby code of what I want:

irb(main):001:0> str1, str2 = ["hey", "now"] => ["hey", "now"] irb(main):002:0> str1 => "hey" irb(main):003:0> str2 => "now" 

I'm not sure if what I'm wanting is possible in C#.

Edit: for those suggesting I just assign the strings "hey" and "now" to variables, that's not what I want. Imagine the following:

irb(main):004:0> val1, val2 = get_two_values() => ["hey", "now"] irb(main):005:0> val1 => "hey" irb(main):006:0> val2 => "now" 

Now the fact that the method get_two_values returned strings "hey" and "now" is arbitrary. In fact it could return any two values, they don't even have to be strings.

like image 299
Sarah Vessels Avatar asked Sep 14 '09 15:09

Sarah Vessels


People also ask

How do you set multiple values in an array?

Use the spread syntax to push multiple values to an array, e.g. arr = [... arr, 'b', 'c', 'd']; . The spread syntax can be used to unpack the values of the original array followed by one or more additional values. Copied!

How do you assign an array value to a variable?

Assigning values to an element in an array is similar to assigning values to scalar variables. Simply reference an individual element of an array using the array name and the index inside parentheses, then use the assignment operator (=) followed by a value.

Can we assign array to var in C#?

This is not possible in C#.

How do you assign a value to a list to a variable in Python?

Using for in The for loop can help us iterate through the elements of the given list while assigning them to the variables declared in a given sequence. We have to mention the index position of values which will get assigned to the variables.


2 Answers

This is not possible in C#.

The closest thing I can think of is to use initialization in the same line with indexs

strArr = new string[]{"foo","bar"}; string str1 = strArr[0], str2 = strArr[1]; 
like image 98
JaredPar Avatar answered Oct 20 '22 14:10

JaredPar


Update: In C#7 you can easily assign multiple variables at once using tuples. In order to assign array elements to variables, you'd need to write an appropriate Deconstruct() extension methods:

Another way to consume tuples is to deconstruct them. A deconstructing declaration is a syntax for splitting a tuple (or other value) into its parts and assigning those parts individually to fresh variables:

(string first, string middle, string last) = LookupName(id1); // deconstructing declaration WriteLine($"found {first} {last}."); 

In a deconstructing declaration you can use var for the individual variables declared:

(var first, var middle, var last) = LookupName(id1); // var inside 

Or even put a single var outside of the parentheses as an abbreviation:

var (first, middle, last) = LookupName(id1); // var outside 

You can also deconstruct into existing variables with a deconstructing assignment:

(first, middle, last) = LookupName(id2); // deconstructing assignment 

Deconstruction is not just for tuples. Any type can be deconstructed, as long as it has an (instance or extension) deconstructor method of the form:

public void Deconstruct(out T1 x1, ..., out Tn xn) { ... } 

The out parameters constitute the values that result from the deconstruction.

(Why does it use out parameters instead of returning a tuple? That is so that you can have multiple overloads for different numbers of values).

class Point {     public int X { get; }     public int Y { get; }      public Point(int x, int y) { X = x; Y = y; }     public void Deconstruct(out int x, out int y) { x = X; y = Y; } }  (var myX, var myY) = GetPoint(); // calls Deconstruct(out myX, out myY); 

It will be a common pattern to have constructors and deconstructors be “symmetric” in this way. https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/


Old answer:

In fact, you can achieve similar functionality in C# by using extension methods like this (note: I haven't include checking if arguments are valid):

public static void Match<T>(this IList<T> collection, Action<T,T> block) {     block(collection[0], collection[1]); } public static void Match<T>(this IList<T> collection, Action<T,T,T> block) {     block(collection[0], collection[1], collection[2]); } //... 

And you can use them like this:

new[] { "hey", "now" }.Match((str1, str2) => {     Console.WriteLine(str1);     Console.WriteLine(str2); }); 

In case a return value from a function is needed, the following overload would work:

public static R Match<T,R>(this IList<T> collection, Func<T, T, R> block) {     return block(collection[0], collection[1]); }  private string NewMethod1() {        return new[] { "hey", "now" }.Match((str1, str2) =>     {         return str1 + str2;     }); } 

In this way:

  • You avoid having to repeat array name like in solution proposed by JaredPar and others; the list of "variables" is easy to read.

  • You avoid having to explicitly declare variables types like in Daniel Earwicker's solution.

The disadvantage is that you end up with additional code block, but I think it's worth it. You can use code snippets in order to avoid typing braces etc. manually.

I know it's a 7 years old question, but not so long time ago I needed such a solution - easy giving names to array elements passed into the method (no, using classes/structs instead of arrays wasn't practical, because for same arrays I could need different element names in different methods) and unfortunately I ended up with code like this:

var A = points[0]; var A2 = points[1]; var B = points[2]; var C2 = points[3]; var C = points[4]; 

Now I could write (in fact, I've refactored one of those methods right now!):

points.Match((A, A2, B, C2, C) => {...}); 

My solution is similar to pattern matching in F# and I was inspired by this answer: https://stackoverflow.com/a/2321922/6659843

like image 23
user1414213562 Avatar answered Oct 20 '22 15:10

user1414213562