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)
- {
-
- int[] array = new int[2];
- array[0] = 1;
-
- }
- }
- }
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'".
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)
- {
- object[] array = new object[2];
- array[0] = 1;
- array[1] = "string";
- }
- }
- }
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.
- 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;
- }
- }
- }
- class customer
- {
- public int ID { get; set; }
- public string Name { get; set; }
- }
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.
- 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; }
- }
Now run the application and see the output like this.
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; }
-
- public override string ToString()
- {
-
- return this.Name;
- }
- }
Now see, in the output we get the name of the customer.
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)
- {
-
- ArrayList array = new 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;
- }
- }
Now see the output, we get the same output like this: