1

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?

8
  • 1
    You seem to have missed the whole point of arrays... Commented Dec 14, 2016 at 2:49
  • 1
    I have no idea why you'd want to do this. Commented Dec 14, 2016 at 2:49
  • I think this is almost the same as your question, Google is helpful Commented Dec 14, 2016 at 2:56
  • You can use a dictionary : Dictionary<string, int> 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()); Commented Dec 14, 2016 at 2:58
  • 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 . Commented Dec 14, 2016 at 4:53

4 Answers 4

1

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

var variable1 = arrayOfSize3[0];
var variable2 = arrayOfSize3[1];
var variable3 = arrayOfSize3[2];
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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];
    }
}

Comments

0

If your array is a member of a class, you could use properties in order to provide a named interface.

    class Foo
    {
        readonly int[] myArray = new int[2];

        int NamedZero
        {
            get => myArray[0];
            set => myArray[0] = value;
        }

        int NamedOne
        {
            get => myArray[1];
            set => myArray[1] = value;
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.