0

Say I have an array of fixed size N, is there a way to map the elements to a list of N variable names?

I was thinking of something like:

variable1, variable2, variable3 = arrayOfSize3;

EDIT:

A few people have remarked that this would be useless and suggested that I am doing something wrong. Maybe I am, but this is a pretty common feature in dynamic languages so I was hoping C# had something elegant as an alternative.

If it helps, I can write what I need it for. I have parsed an HTML table and have an array of strings representing a row of the table. I made a class to represent the row with variables representing the data, but to store the data I have to manually set the names to each of the array elements. I know there are other ways to do this, but I was wondering more out of curiosity than anything else, what is the right way to map variables like this?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Krikor Ailanjian
  • 1,842
  • 12
  • 17
  • 1
    You seem to have missed the whole point of arrays... – John3136 Dec 14 '16 at 02:49
  • 1
    I have no idea why you'd want to do this. – itsme86 Dec 14 '16 at 02:49
  • I think [this](http://stackoverflow.com/questions/20857773/create-dynamic-variable-name) is almost the same as your question, Google is helpful – shakram02 Dec 14 '16 at 02:56
  • You can use a dictionary : Dictionary dict = arrayOfSize3.Select((x, i) => new { name = "variable" + i.ToString(), value = x }) .GroupBy(x => x.name, y => y.value) .ToDictionary(x => x.Key, y => y.FirstOrDefault()); – jdweng Dec 14 '16 at 02:58
  • The difficulty is that variableX could be value types making them hard to pass around when there is a variable number of them. – Daniel Dec 14 '16 at 03:12
  • 1
    C# 7, still in preview, supports deconstruction matching with Tuple, you can do var (variable1, variable2, variable3) = tupleOfSize3 . It actually works with any thing with deconstructor method, maybe array will have this extension. Please see [What's new in C# 7.0](https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/) . – frogcoder Dec 14 '16 at 04:53
  • @frogcoder If you respond something like "there is no simple way to do this now, but C# 7 has X feature" I will accept it as the answer since your response was the closest and it may be useful to someone in the future. – Krikor Ailanjian Dec 20 '16 at 02:42
  • C# 7 does not appear to support deconstructing. Nor does List. – Alex Dresko Mar 11 '17 at 14:26

3 Answers3

1

Their is no such assignment, but you can do like this:

var variable1 = arrayOfSize3[0];
var variable2 = arrayOfSize3[1];
var variable3 = arrayOfSize3[2];
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

Not production ready yet, but you can do this in C# 7 preview which comes with Visual Studio 15 preview. Using deconstruction matching, the following code works with Tuple

var (variable1, variable2, variable3) = tupleOfSize3;

This feature actually works with anything with a deconstructor method like this

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

Maybe array will have this extension. Please see What's new in C# 7.0

frogcoder
  • 963
  • 1
  • 7
  • 17
0

Tuples are a set of a fixed number values, each with its own name and type. Arrays, on the other hand, are a set of a variable number of anonymous values, all of the same type. These are two very distinct forms of aggregate types, and they really serve mostly disjoint use cases.

Having said that, it is possible to deconstruct an array (in C# 7 preview which comes with Visual Studio 15 preview)! You can do it by adding your own Deconstruct method as an extension method:

class C
{
    public static void Main()
    {
        int[] x = new int[2];
        var (a, b) = x;
    }
}

static class ArrayUtilities
{
    public static void Deconstruct(this int[] data, out int a, out int b)
    {
        a = data[0];
        b = data[1];
    }
}
Neal Gafter
  • 3,293
  • 20
  • 16