0

I need to create a list (array or any type) that would hold values, but these values would vary in size. I'm not sure what data type to use in C#. In PHP it would be something like this;

$a = [1,2];
$a[] = 3;
$a[][] = [4,5,6]; // This would hold any number of values
$a[] = 7;
$a[][] = [8,9];   // This would hold any number of values

The result would be an 'array' that could hold any number of values like this: See how index 3 is also an array that could hold any number of values. The same is for index 5.

> Array (
>     [0] => 1
>     [1] => 2
>     [2] => 3
>     [3] => Array
>         (
>             [0] => Array
>                 (
>                     [0] => 4
>                     [1] => 5
>                     [2] => 6
>                 )
> 
>         )
> 
>     [4] => 7
>     [5] => Array
>         (
>             [0] => Array
>                 (
>                     [0] => 8
>                     [1] => 9
>                 )
> 
>         )
> 
> )

I tried this way, but it doesn't work:

int[,] array2D = new int[,] { { 1, 2 }, { 3 }, { 4, 5, 6 }, { 7, 8 } };
6
  • 1
    Your best bet is most likely a List<object> but you're going to have a hard time doing anything useful with the structure unless all you want to do is serialize it. Commented Oct 13, 2019 at 22:27
  • List<object> is a poor choice to represent data. If you told us what these numbers represent and where they are coming from we may be able to make a better suggestion. Commented Oct 13, 2019 at 22:32
  • Personally this seems to be an XY problem probably not the best way to do things this what I do in php so how do I transfer it to C#. The last example is a jagged array they are initiated with int[][]/ Commented Oct 13, 2019 at 22:48
  • Understanding the difference between multi-dimensional arrays and jagged arrays is important in C#. Just as important as what you are going to next with the jaggies you asked for, most algorithms are based on having some consistency in the data arrangement. Be sure to focus on that first, before you pick the data representation. Commented Oct 13, 2019 at 22:50
  • 1
    It's not about the difference between multi-dimensional and jagged arrays. The OP wants a n-ary tree. I.e., each entry in the array can be either an int or another array of ints. Commented Oct 13, 2019 at 22:54

3 Answers 3

2

Well, since you only have two options: Int or IEnumerable<Int>, you could do something like...

public class IntContainer {
   private Int _intValue;
   private IEnumerable<Int> _intCollection;

   public IntContainer(Int val) {
      this._intValue = val;
   }


   public IntContainer(IEnumerable<Int> val) {
      this._intCollection = val;
   }

   public bool IsSingleValue() => _intCollection == null;
   public bool IsMultiValue() => _intCollection != null;
}

Then have a List<IntContainer> like so:

var myList = new List<IntContainer>{
new IntContainer(new [] { 1, 2})
};

myList.Add(new IntContainer(3));

myList.Add(new IntContainer(new [] { 4,5,6 })); // etc..

Note: the real difference is not between PHP and C#, but between statically typed and dynamically typed languages.

Sign up to request clarification or add additional context in comments.

Comments

0

This comes close to what you want

int[][] x =  { new[]{1, 2 }, new[]{ 3 }, new[]{ 4, 5, 6 }, new[]{ 7, 8 } };

All top level elements will be arrays, so x[1] is not 3 but an array with 1 element.

x[0][0] = 1;
x[1][0] = 3;
//Note: x[1][1] => 'x[1][1]' threw an exception of type 'System.IndexOutOfRangeException'

In your question you have

> Array (
>     [0] => 1
>     [1] => 2
>     [2] => 3

but then you try to declare it{ { 1, 2 }, { 3 },.

You can achieve both with

int[][] x =  { new[]{1, 2 }, new[]{ 3 }, new[]{ 4, 5, 6 }, new[]{ 7, 8 } };

vs

int[][] x =  { new[]{1}, new[]{2}, new[]{ 3 }, new[]{ 4, 5, 6 }, new[]{ 7, 8 } };

Comments

0

As I mentioned doing things like PHP in C# is not the best way of doing it since you are not using c# to it's fullest power. But if you are use to dynamic types there is a way to do it in c# as well. You can create something similar to PHP with dynamic keyword.

 dynamic data = new dynamic[] 
            { 
                new[] { 1, 2 },  
                3, 
                new[] { 4, 5, 6 }, 
                new[] { 7, 8 } 
            };

            Console.WriteLine(data[0][0] + data[1]);

But it would not be the same as PHP because Array is not an actual array it's a map.

For the even more PHP "Array" you could use a dictionary

dynamic a = new Dictionary<dynamic, dynamic>()
            {
                [0] = 1,
                [1] = 2,
                [2] = 3,
                [3] = new Dictionary<dynamic, dynamic>()
                {
                    [0] = new Dictionary<dynamic, dynamic>()
                    {
                        [0] = 4,
                        [1] = 5,
                        [2] = 6,
                    }
                },
                [4] = 7,
                [5] = new Dictionary<dynamic, dynamic>()
                {
                    [0] = new Dictionary<dynamic, dynamic>()
                    {
                        [0] = 8,
                        [1] = 9
                    }

                }
            };

but this will throw exceptions when you try to access an element out of range but you could implement your own dictionary quite ease to solve this problem. BUT I think this is not really C# way of doing it and might want to rethink what you are trying to do.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.