1

I have trouble with the value that arraylist returns.

I created a two-dimensional Arraylist that includes string arrays, when I try to get actual value of string arrays, I get System.String[] as output instead of actual value of the arrays.

Why do I get System.String() as outuput?

Here is my code :

static void Main(string[] args)
{
    string[] employee_1 = { "Employee1" };
    string[] employee_2 = { "Employee2" };

    ArrayList main_array = new ArrayList();

    main_array.Add(employee_1);
    main_array.Add(employee_2);

    for (int i= 0; i < 2; i++) 
    {
        Console.WriteLine(main_array[i]);                
    }
    Console.ReadKey();
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Ahmet Furkan
  • 84
  • 11
  • You should have a look at the [AddRange](https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist.addrange?view=netframework-4.7.2) method. – Jeroen Heier Sep 30 '18 at 05:51
  • 5
    This seems like an XY problem... Why are you using an `ArrayList` instead of a generic array or list, also this is not a multidimensional or a 2 dimensional array – TheGeneral Sep 30 '18 at 05:55
  • https://stackoverflow.com/questions/5063156/why-isnt-arraylist-marked-obsolete – Cheng Chen Sep 30 '18 at 09:05

5 Answers5

2

That is because when retrieving the items from an ArrayList what you are getting is a reference to an object instead of the actual type. Thus when printing it is calls the ToString of object (which prints the type's name) and not the string you want. In addition when printing a collection (like you are doing in your WriteLine command) you need to specify how to so do because it's default implementation is also as object's. You can use string.Join to print all items in the nested array.

To correct this first cast to string[] ( as string[]) and then print, or better still is to work with the list object instead: List<string[]>. To read more see:

So:

var mainCollection = new List<string[]> { new string[] { "Employee1" },
                                          new string[] { "Employee2" }};
for (int i = 0; i < mainCollection.Count; i++) 
{
    Console.WriteLine(string.Join(", ", mainCollection[i]));
}
Console.ReadKey();

As a side note do not loop to 2 but instead by the number of items in the collection. See: What is a magic number, and why is it bad?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • And last, make sure that these `string[]` are not better of as custom classes with proper properties.. – Gilad Green Sep 30 '18 at 05:54
  • This time I get an error of "Anonymous Types" with "Employee1" and "Employee2". How can I correct it ? – Ahmet Furkan Sep 30 '18 at 06:07
  • @AhmetFurkan - you are correct. I have a mistake in the initializing of the list. See correction – Gilad Green Sep 30 '18 at 06:11
  • Thank you @Gilad Green for your solution. I will be dealing with what you recommended to me. – Ahmet Furkan Sep 30 '18 at 10:55
  • @AhmetFurkan - You are welcome :) If at the end you do use this answer for your solution please consider marking this one as the one that solves the question for future readers use. – Gilad Green Sep 30 '18 at 13:55
  • At last I could figure out and can correct the code in your way. Thank you again. By the way I correct the marking of answer. Have a good coding. – Ahmet Furkan Sep 30 '18 at 19:01
0

This is the default behaviour of ToString() function for an array.

To print the employees names, you need to iterate the array:

for (int i= 0; i < 2; i++) 
{
    foreach(string employee in main_array[i]) {
        Console.WriteLine(employee);
    }
}
Renan Araújo
  • 3,533
  • 11
  • 39
  • 49
0

The problem is here, ArrayList always take input as an object. So, when you add string array at an ArrayList, it take an object instead of string array.

So, you should convert this object to string array and print it. like below:

        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine(string.Join(", ", main_array[i] as string[]));
        }

or may use below(both are same):

        for (int i = 0; i < 2; i++)
        {
            foreach (string employee in main_array[i] as string[])
            {
                Console.WriteLine(employee);
            }
        }
Rousonur Jaman
  • 1,183
  • 14
  • 19
0

Because you have string[] type elements in outer ArrayList. The best examples to enumerate are in answers above. If you realy need string content of string to be wtitten you should use

Console.WriteLine(((string[])main_array[i])[0]);
Serg Shevchenko
  • 662
  • 1
  • 5
  • 21
-1

main_array is a two position ArrayList, each position contains a string[] this is, a string array.

If you use main_array[0], this is a reference to employee_1 which is a string array.

You should be able to reference the array strings by using main_array[0][0]

Try this:

foreach(var strArray in main_array)
{
    // strArray is a string array
    foreach(var stir in strArray)
    {
        // star is a string
        Console.WriteLine(str);
    }
}
Prix
  • 19,417
  • 15
  • 73
  • 132
Mauricio Atanache
  • 2,424
  • 1
  • 13
  • 18
  • There is a problem with second foreach loop. I get an error of "GetEnumerator". Can you take a look again ? – Ahmet Furkan Sep 30 '18 at 06:16
  • @AhmetFurkan that's because `strArray` doesn't know what type of data is inside `main_array`, so you have to explicit do `string[]` instead of `var` on the first `foreach`, also there is a typo on his 2nd `foreach`, should have been `str` instead of `stir` – Prix Sep 30 '18 at 06:22
  • Thank you @Prix, After I modified that you said, ıt worked for me. – Ahmet Furkan Sep 30 '18 at 06:36
  • 1
    @AhmetFurkan nice it works for you but you should still follow @GiladGreen 's advice and use `List` instead of `ArrayList`, you can read more about why on the 2 links he posted on his answer. – Prix Sep 30 '18 at 06:56
  • @Prix All right, I will. I just wanted reason of this issue. I guess, figure out what is going on with that. Thank you again for your comments. – Ahmet Furkan Sep 30 '18 at 10:50
  • I guess I won’t try to help people anymore using mi phone. :), anyway i’m glad you took my idea as your new code base. – Mauricio Atanache Oct 01 '18 at 02:55