As we know array is a collection of similar objects.
If we create array of integer, we can pass or assign only the integer values. If we try to pass or assign any other data type value we will get a compile time error.
So, how can we store values of different data types in a single array?
We can create array of object.
As we know all types i.e. value types and complex types directly or indirectly inherits from System.Object namespace. So, we can pass any type of data to an object type.
DEMO
- using System;
- namespace ObjectArrays
- {
- class Student
- {
- public int Id{get;set;}
- public string Name{get;set;}
- public override string ToString()
- {
- return this.Name;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //create an array of type object
- object[] ObjectType = new object[3];
- //in the first position, we are assigning an integer value
- ObjectType[0] = 1;
- //in the second position, we are assigning a string value.
- ObjectType[1] = "Hello";
- Student s = new Student();
- s.Id = 1;
- s.Name = "Sam";
- //in the third position we are assigning a complex value
- ObjectType[2] = s;
- foreach(object objects in ObjectType)
- {
- Console.WriteLine(objects);
- }
- }
- }
- }

I hope you like it and find this helpful.
Thank you for reading.

Madhu KumarPosted Apr 30, 2015, 7:38 AM
also we can use arraylist to store values of different data types.
Harpreet SinghPosted Apr 25, 2015, 12:57 AM
Nanhe Siddique thank you
Harpreet SinghPosted Apr 25, 2015, 12:57 AM
Santhakumar Munuswamy thank you
Nanhe SiddiquePosted Apr 24, 2015, 12:15 AM
usefull
Santhakumar MunuswamyPosted Apr 23, 2015, 2:32 PM
Nice