TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Object Array

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

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. 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.

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.

StringBuilder

Finally: WriteArray uses the foreach-loop to iterate over the object array. It tests for null to avoid NullReferenceExceptions.

ForeachNullReferenceException

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.

String Literal

Note: The null literal is a special-cased reference that is compatible with all reference types.

Null

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.

Type

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.

DataRowDataTable

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.

Excel

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf