C# Object Array

Object keyword

Object arrays are versatile. They store elements of different types in a single collection. An object reference can point to any derived type instance. This makes code more complex. It also decreases runtime performance.

Example

Note

We see an example program that uses an object[] array to store different types of data in each element location. This is not an example of ideal and maintainable C# code, but is designed to demonstrate how the object array can store all types of elements, both reference types such as string and value types such as int. The type information is then retained and can be used later.

This C# program uses object arrays. Object arrays store many types of elements.

Program that uses object[] array [C#]

using System;
using System.Text;

class Program
{
    static void Main()
    {
	//
	// Allocate an object array.
	//
	object[] array1 = new object[5];
	//
	// - Put an empty object in the object array.
	// - Put various object types in the array.
	// - Put string literal in the array.
	// - Put an integer constant in the array.
	// - Put the null literal in the array.
	//
	array1[0] = new object();
	array1[1] = new StringBuilder("Initialized");
	array1[2] = "String literal";
	array1[3] = 3;
	array1[4] = null;
	//
	// Use the object array reference as a parameter.
	//
	WriteArray(array1);
    }

    static void WriteArray(object[] array)
    {
	//
	// Loop through the objects in the array.
	//
	foreach (object element in array)
	{
	    if (element != null) // Avoid NullReferenceException
	    {
		Console.WriteLine(element.ToString());
		Console.WriteLine(element.GetType());
		Console.WriteLine("---");
	    }
	}
    }
}

Output

System.Object
System.Object
---
Initialized
System.Text.StringBuilder
---
String literal
System.String
---
3
System.Int32
---
New keyword (constructor invocation)

New object[] array. The program contains an array creation expression that allocates an object array on the managed heap that has five elements of storage. These elements are always initialized to null on the managed heap because of the way the CLR is designed to avoid uninitialized memory.

Assignments. The program next assigns the five offsets of the array storage location to various actual objects or references. The "new object()" is basically an empty object that can't be used for much. The StringBuilder is an object instance used to concatenate strings quickly. The string literal is actually a reference to the character data stored in the string literal between the quotes. The integer is a value type, but will be stored in a "box" so that it can be classified as an object logically. The null literal is a special-cased reference that is compatible with all reference types.

StringBuilder Secrets String Literal Null Tips

Boxing objects into the array. Due to performance concerns, all numbers in the C# language such as "int" variables are stored directly in the evaluation stack in most situations. This does improve performance, but the CLR must use boxing to represent this value types as actual objects with type pointers. When you use an integer variable in a method body, it does not have a type pointer. However, the object representation of the integer does have a type pointer.

Foreach loop construct

Looping over object array. The WriteArray method shown near the bottom of the example uses the foreach-loop to iterate over the object[] array. It tests each element for null to avoid the NullReferenceException. Finally, it prints some information about the objects stored in the array. You can see that the precise type information was retained and is still available to use.

NullReferenceException and Null Parameter

Usages

Note

Let's note some practical usages of object arrays you may encounter in C# programming. The System.Data namespace in the .NET Framework contains some types that use object arrays, such as the DataRow type and the DataTable type. These are very commonly used and the object arrays they use represent "elements" with varying data types in a database table.

DataRow Examples

Additionally, the Microsoft.Office.Interop.Excel namespace, which you can use to manipulate Microsoft Excel and create spreadsheets in C# code, often uses object[] arrays for the same general purpose.

Excel Interop: Microsoft.Office.Interop.Excel

Summary

.NET Framework information

We examined object arrays. We saw how they can be used to store different types of objects together, while retaining the original type information. Object arrays are most often used in methods that must interoperate with other technologies, such as databases or programs such as Excel. Also, object arrays can be used with threading methods, which essentially are a way to interact with the operating system's threading abilities.

Object Type
.NET