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:
Step 2
Create a console application and give it a name such as InterviewQuestion.
Step 3
Create an integer array as in the following code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InterviewQuestion
- {
- class Program
- {
- static void Main(string[] args)
- {
- //create an integer array
- int[] array = new int[2];
- array[0] = 1;
- }
- }
- }

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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InterviewQuestion
- {
- class Program
- {
- static void Main(string[] args)
- { //create an object type array
- object[] array = new object[2];
- array[0] = 1;
- array[1] = "string";
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InterviewQuestion
- {
- class Program
- {
- static void Main(string[] args)
- {
- object[] array = new object[3];
- array[0] = 1;
- array[1] = "string";
- // create object of class
- customer c = new customer();
- c.ID = 1;//store integer value
- c.Name = "C# corner";//store string value
- array[2] = c;//store complex value
- }
- }
- }
- class customer
- { //creation of properties
- public int ID { get; set; }
- public string Name { get; set; }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InterviewQuestion
- {
- class Program
- {
- static void Main(string[] args)
- {
- object[] array = new object[3];
- array[0] = 1;
- array[1] = "string";
- customer c = new customer();
- c.ID = 1;
- c.Name = "C# corner";
- array[2] = c;
- foreach(object obj in array
- {
- //print values of the items that we have in array
- Console.WriteLine(obj);
- }
- }
- }
- }
- class customer
- {
- public int ID { get; set; }
- public string Name { get; set; }
- }

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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InterviewQuestion
- {
- class Program
- {
- static void Main(string[] args)
- {
- object[] array = new object[3];
- array[0] = 1;
- array[1] = "string";
- customer c = new customer();
- c.ID = 1;
- c.Name = "C# corner";
- array[2] = c;
- foreach(object obj in array)
- {
- Console.WriteLine(obj);
- }
- }
- }
- }
- class customer
- {
- public int ID { get; set; }
- public string Name { get; set; }
- //override the ToString method for meaningfull output
- public override string ToString()
- {
- //get name of the customer by Name property
- return this.Name;
- }
- }

So the complete answer to this question is:

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.

We can add any datatype to an ArrayList collection, using the following code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace InterviewQuestion
- {
- class Program
- {
- static void Main(string[] args)
- {
- //creation of ArrayList
- ArrayList array = new ArrayList();
- //Add method for adding values in ArrayList
- array.Add(1);
- array.Add("string");
- customer c = new customer();
- c.ID = 1;
- c.Name = "C# corner";
- array.Add(c);
- foreach(object obj in array)
- {
- Console.WriteLine(obj);
- }
- }
- }
- }
- class customer
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public override string ToString()
- {
- return this.Name;
- }
- }

Ian IanPosted Jan 21, 2021, 12:21 PM
Shaili, Thank you for this. It is very helpful and beautifully clear
Anil RathodPosted Mar 2, 2019, 12:05 AM
If I have more than one properties then how should i iterate from object array.
Thiruppathi RPosted Apr 4, 2016, 1:17 AM
Nice article....
Sr KarthigaPosted Mar 8, 2016, 11:20 AM
good one
Sr KarthigaPosted Mar 8, 2016, 11:19 AM
nice explanation
Sourabh SomaniPosted Nov 17, 2014, 3:52 AM
:)
Shaili DashoraPosted Nov 17, 2014, 3:45 AM
Thank you to all
Vithal WadjePosted Nov 17, 2014, 3:38 AM
good work keep it up
Dinesh BeniwalPosted Nov 16, 2014, 5:31 AM
Appreciate your hard work Shaili.
Abhishek YadavPosted Nov 16, 2014, 1:49 AM
Nice work Shaili, this article is very informative... :)
Sourabh SomaniPosted Nov 16, 2014, 1:39 AM
Gr8 shaili really very nice article....
Shaili DashoraPosted Nov 16, 2014, 1:04 AM
Thank you very much sir for motivate me,its very big complement for me.
Sam HobbsPosted Nov 15, 2014, 8:36 PM
Thank you, Shaili, I think you will help many people. I wonder if it would be good for a potential employee to say it is best to avoid doing this. This is good if it is necessary. I think that many (I think most) employers should want their employees to avoid doing this and they should be impressed if a candidate says it should be avoided. Do you know differently?