What is an Array of Objects in C#
An array is the collection of the data elements stored, and each element can be accessed directly from the indexed number. For example, seven integer values can be declared with an array without having to declare separately. You can store multiple variables in a single variable by using an array of objects and you can store all predefined and user-defined data type values in the object array.
You can specify the object type to the array if you want to store the data of multiple types. They are versatile and can hold multiple variables of multiple types.
The following is the syntax of the object array in C#:
For example, you can create an object array with three elements of different types as follows:
You can access the elements of the array using their indexes, as follows:
string myString = (string)myArray[1]; // Retrieves the second element ("welcome") and casts it to a string.
CustomObject myObject = (CustomObject)myArray[2]; // Retrieves the third element (a custom object) and casts it to the appropriate type.
Example 1: Storing Different Data Types in an Object Array
The following code illustrates storing different types in an object array in C#:
class Program {
static void Main() {
object[] myArray = new object[3];
myArray[0] = "Linuxhint"; // storing a string
myArray[1] = 10; // storing an integer
myArray[2] = 3.14; // storing a double
// Accessing the values in the array
Console.WriteLine(myArray[0]); // Output: Linuxhint
Console.WriteLine(myArray[1]); // Output: 10
Console.WriteLine(myArray[2]); // Output: 3.14
}
}
Example 2: Using an Object Array to Store Objects of a Class
In C#, an object array can also be used to store objects of a class. Here is an example:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
Person person1 = new Person() { Name = "Zainab", Age = 25 };
Person person2 = new Person() { Name = "Awais", Age = 27 };
object[] peopleArray = new object[2];
peopleArray[0] = person1;
peopleArray[1] = person2;
// Accessing the objects from the array
Person retrievedPerson1 = (Person)peopleArray[0];
Person retrievedPerson2 = (Person)peopleArray[1];
Console.WriteLine("First person: " + retrievedPerson1.Name + " - " + retrievedPerson1.Age);
Console.WriteLine("Second person: " + retrievedPerson2.Name + " - " + retrievedPerson2.Age);
Console.ReadLine();
}
}
The above code defines a Person class with two properties, a Name, and Age. It then creates two instances of the Person class and stores them in an object array. You can get the objects from the array by casting them back to the Person type. Finally, the retrieved objects’ properties are printed to the console.
Bottom Line
The object array in C# can be used to store different data elements in each element location. The object array keeps the component of the many data types in a single location. Object array makes the code more complex and decreases the run time performance of the code. In this guide, we have learned the initialization and the use of the array of objects in the C# with the example codes.