What is my error in List syntax? - C #

1

I want to create the following structure but I think I have a problem in the list syntax

List<ArrayList> vertices = new List<ArrayList> { { 'alfa',false},{ 'Beta',false}};

By the way, I had problems trying to make an array in this way too

string[,] vertices3 = new string[2,2] { {'alfa','nv' }, { 'alfa', 'nv' }};

But for some reason, if I can do the matrix with numbers

int[,] aristas2 = new int[2, 10] { {0,76000,0,20000,0,0,0,0,0,0 },
                                        { 76000,0,0,0,240000,0,0,0,360000,0 } };
    
asked by Shiki 03.12.2017 в 08:57
source

2 answers

0

The problem of the string array was the single quotes I apologize, so long with database I get used to its syntax for example instead of being 'alpha' it should have been "alpha" the single quotes indicated a single character , I had forgotten it ... I still want to know what the syntax with List and arraylist would be like because I could not solve it, and when using list and arraylist I could use different data types in an array such as {{"asd", false}, {"asd", false}}

    
answered by 03.12.2017 в 09:42
0

You are missing the new ArrayList for each element of List :

List<ArrayList> vertices = 
    new List<ArrayList>
    { 
        new ArrayList { "alfa", false },
        new ArrayList { "Beta", false }
    };

Although I am sure that what you are testing is purely for academic purposes, I mention that the use of ArrayList is no longer favored. It is always better to use List<T> instead. In this case, you would replace ArrayList with List<object> .

Or better yet, you would use a tuple, which is more suitable for the data you have.

Example using System.Tuple :

List<Tuple<string, bool>> vertices = 
    new List<Tuple<string, bool>>
    {
        Tuple.Create("alfa", false),
        Tuple.Create("Beta", false)
    };

If you have C # 7 + and you include the libraries for the new System.ValueTuple :

List<(string, bool)> vertices = 
    new List<(string, bool)>
    {
        ("alfa", false),
        ("Beta", false)
    };
    
answered by 03.12.2017 в 14:08