0

I need an array of integer arrays and string in C#

[
    ["Item 1",[1,2,3,4,5,6]],
    ["Item 1",[1,2,3,4,5,6]],
    ["Item 1",[1,2,3,4,5,6]],
    ["Item 1",[1,2,3,4,5,6]]
]

I can not think of any correct possible approach to this problem. I came to this snippet below.

string[,] items = new string[10, 2];
for(int 1 = 0; i<10; i++){
    items[i, 0] = "Item " + i;
    items[i, 1] = new int[10];
}

How can I get the array I need?

0

3 Answers 3

1

use a dictionary.

Dictionary<string, int[]> myDictionary = new Dictionary<string, int[]>();

// Add Item:
myDictionary.Add("Item 1", new int[]{1,2,3,4,5,6});

Be aware that the key (string) must be unique if you use a dictionary.

or use tuples:

Tuple<string, int[]>[] myTuples = new Tuple<string, int[]>[5];
myTuples[0] = new Tuple<string, int[]>("Item 1", new int[] { 1, 2, 3, 4, 5, 6 });
// Access string with: myTuples[0].Item1
// Access int[]  with: myTuples[0].Item2

Or since C#7.0 you can use named tuples:

(string MyString, int[] MyIntArray)[] myTuples = new (string MyString, int[] MyIntArray)[5];
myTuples[0] = ("Item1", new int[]{1,2,3,4,5,6});

// Access string with: myTuples[0].MyString
// Access int[]  with: myTuples[0].MyIntArray
Sign up to request clarification or add additional context in comments.

1 Comment

Could also define your own class with string and int[] properties.
0

You can create an array of tuples:

(string,int[])[] items = new (string,int[])[10];
for(int i = 0; i < items.Length; i++)
 {
 items[i] = ("Item" + i,new int[10]);
 }

You can access the values by .Item1 and .Item2:

  Console.WriteLine(items[4].Item1); // will output "Item 4"
  Console.WriteLine(items[4].Item2[4]); // will output the 4th value of the 4th array

To increase readability even more, you could create your own class and make an array of this class:

class Item
 {
 public string Name {get;set;}
 public int[] Values {get;set;}
 }

1 Comment

You could also name the parts of the tuple var items = new (string Name, int[] Values)[10];
0

You can create an class like this

public class Test
{
    public string Name { get; set; }
    public int[] Array { get; set; }
}
List<Test> testList = new List<Test>();
for (int i = 0; i < 4; i++)
{
    Test test = new Test
    {
        Name = "Item 1",
        Array = new int[6]
    };
    for (int j = 0; j < 6; j++)
    {
        test.Array[j] = j + 1;
    }
    testList.Add(test);
}
foreach (var item in testList)
{
    Console.Write(item.Name);
    foreach (var arr in item.Array)
    {
        Console.Write("\t" + arr);
    }
    Console.WriteLine();
}

Result

Item 1  1       2       3       4       5       6
Item 1  1       2       3       4       5       6
Item 1  1       2       3       4       5       6
Item 1  1       2       3       4       5       6

1 Comment

Why not using a struct instead of the class?

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.