C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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. We see an example program that uses an object array to store different types of data in each element location. An object array can store all types of elements, both reference types such as string and value types such as int.
Tip: For each object element, the type information is retained and can be used later. It is not lost.
C# program that uses object array 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 ---
The program contains an array creation expression that allocates an object array on the managed heap. Elements are always initialized to null on the managed heap because of the way the CLR is designed to avoid uninitialized memory.
Next, the program 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.
Tip: The StringBuilder is an object instance used to concatenate strings quickly. More details on this type are available.
Finally: WriteArray uses the foreach-loop to iterate over the object array. It tests for null to avoid NullReferenceExceptions.
Discussion. 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 logically classified as an object.
Note: The null literal is a special-cased reference that is compatible with all reference types.
Numbers such as ints are often stored directly in the evaluation stack. This improves performance, but the CLR must use boxing to represent these value types as actual objects with type pointers.
Tip: When you use an integer variable in a method body, it does not have a type pointer. But its object representation does.
Usage. The System.Data namespace contains some types that use object arrays, such as DataRow and DataTable. These are commonly used. The object arrays they use represent "elements" with varying data types in a database table.
Also, we note the Microsoft.Office.Interop.Excel namespace, which you can use to manipulate Microsoft Excel and create spreadsheets in C# code. It often uses object arrays for the same general purpose.
Summary. We examined object arrays. They are used to store different types of objects together, while retaining the original type information. We use them in methods that must interoperate with other technologies, such as databases or Excel.