Store Different Types in an Array in C#

Question: Can we store different types in an array in C#? If so then provide a practical example.

Ans: Yes, if we create an object array.

Before going further, let's understand this concept with an example.

Step 1

Open Visual Studio then go to "File" -> "New" -> "Project..." as in the following:

create new project

Step 2

Create a console application and give it a name such as InterviewQuestion.

console application

Step 3

Create an integer array as in the following code.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace InterviewQuestion    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {       
  12.            //create an integer array    
  13.             int[] array = new int[2];    
  14.             array[0] = 1;    
  15.              
  16.         }    
  17.     }    
  18. }   
When we try to store a string value in an integer array it will give the compile time error "cannot implicitly convert type 'string' to 'int'".

error

Step 4

We know that an array is strongly typed. Saying that an integer array is strongly type means that we can only store an integer type in the array.

If we want to store different data types in the array, then we can create an array of type object so if I convert to this in type object then now we are able to store type integer. I am able to store type string, by the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace InterviewQuestion    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {   //create an object type array    
  12.             object[] array = new object[2];    
  13.             array[0] = 1;    
  14.             array[1] = "string";    
  15.         }    
  16.     }    
  17. }  
Because the object type is the base type for all the types in .NET, every type in .Net is directly or indirectly inherited from an object type. So we can add any type of object to this array as an inherited type, including complex type.

Step 5

Now for example if a class "customer" includes the two properties ID and Name and we increase the size of the array by 3 then create an instance of the customer class and by using the properties store values, I can even store the complex type as in the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace InterviewQuestion    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             object[] array = new object[3];    
  13.             array[0] = 1;    
  14.             array[1] = "string";    
  15.     
  16.          // create object of class    
  17.             customer c = new customer();    
  18.             c.ID = 1;//store integer value    
  19.             c.Name = "C# corner";//store string value    
  20.             array[2] = c;//store complex value    
  21.         }    
  22.     }    
  23. }    
  24. class customer    
  25. {   //creation of properties    
  26.     public int ID { getset; }    
  27.     public string Name { getset; }    
  28. }   
So in this array we can store different data types and we can easily retrieve them as well by using a foreach loop.

And print the values of the items that we have within the array. So we have one integer value, one string value and object of the customer class, like the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace InterviewQuestion    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             object[] array = new object[3];    
  13.             array[0] = 1;    
  14.             array[1] = "string";    
  15.     
  16.             customer c = new customer();    
  17.             c.ID = 1;    
  18.             c.Name = "C# corner";    
  19.             array[2] = c;    
  20.     
  21.             foreach(object obj in array    
  22.             {     
  23.                 //print values of the items that we have in array    
  24.                 Console.WriteLine(obj);    
  25.             }    
  26.         }    
  27.     }    
  28. }    
  29. class customer    
  30. {    
  31.     public int ID { getset; }    
  32.     public string Name { getset; }    
  33. }   
Now run the application and see the output like this.

output

Step 6

It gives the value 1, string and object of the class give the name of the type, that is the name of the class is customer. But it does not make sense to the end user so if we want to get meaningful output then we must do an override of the ToString method of the customer class. And when the ToString method is invoked we want to print the name of the customer, using the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace InterviewQuestion    
  7. {    
  8.     class Program    
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             object[] array = new object[3];    
  13.             array[0] = 1;    
  14.             array[1] = "string";    
  15.     
  16.             customer c = new customer();    
  17.             c.ID = 1;    
  18.             c.Name = "C# corner";    
  19.             array[2] = c;    
  20.     
  21.             foreach(object obj in array)    
  22.             {    
  23.                 Console.WriteLine(obj);    
  24.             }    
  25.         }    
  26.     }    
  27. }    
  28. class customer    
  29. {    
  30.     public int ID { getset; }    
  31.     public string Name { getset; }    
  32.     //override the ToString method for meaningfull output    
  33.     public override string ToString()    
  34.     {    
  35.       //get name of the customer by Name property    
  36.         return this.Name;    
  37.     }    
  38. }   
Now see, in the output we get the name of the customer.

see the output

So the complete answer to this question is:

answer

An alternative is to use an object array. We can use an ArrayList class that is present in the System.Collections namespace,. Using it we can store different types because the ArrayList class operates on the object type.

Step 7

Now import the System.Collections namespace and use an ArrayList and create an ArrayList then use the Add method. Add the value of type object, ArrayList operates on the object type.

arraylist store object value

We can add any datatype to an ArrayList collection, using the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Collections;    
  6.     
  7. namespace InterviewQuestion    
  8. {    
  9.     class Program    
  10.     {    
  11.         static void Main(string[] args)    
  12.         {      
  13.           //creation of ArrayList    
  14.             ArrayList array = new ArrayList();    
  15.           //Add method for adding values in ArrayList     
  16.             array.Add(1);    
  17.             array.Add("string");    
  18.     
  19.             customer c = new customer();    
  20.             c.ID = 1;    
  21.             c.Name = "C# corner";    
  22.             array.Add(c);    
  23.     
  24.             foreach(object obj in array)    
  25.             {    
  26.                 Console.WriteLine(obj);    
  27.             }    
  28.         }    
  29.     }    
  30. }    
  31. class customer    
  32. {    
  33.     public int ID { getset; }    
  34.     public string Name { getset; }    
  35.     
  36.     public override string ToString()    
  37.     {    
  38.         return this.Name;    
  39.     }    
  40. }  
Now see the output, we get the same output like this:

program output

 

Up Next
    Ebook Download
    View all
    Learn
    View all