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
The real-world use case for this is providing a convenient way to return multiple values from a function. So it is a Ruby function that returns a fixed number of values in the array, and the caller wants them in two separate variables. This is where the feature makes most sense:
first_name, last_name = get_info() // always returns an array of length 2
To express this in C# you would mark the two parameters with out in the method definition, and return void:
public static void GetInfo(out string firstName, out string lastName)
{
// assign to firstName and lastName, instead of trying to return them.
}
And so to call it:
string firstName, lastName;
SomeClass.GetInfo(out firstName, out lastName);
It's not so nice. Hopefully some future version of C# will allow this:
var firstName, lastName = SomeClass.GetInfo();
To enable this, the GetInfo method would return a Tuple. This would be a non-breaking change to the language as the current legal uses of var are very restrictive so there is no valid use yet for the above "multiple declaration" syntax.