Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are mixed arrays in C#?
Mixed arrays in C# refer to arrays that can store elements of different data types. They were historically used as a combination of multi-dimensional arrays and jagged arrays, but this terminology is largely obsolete in modern C#. Instead, we use object[] arrays or collections to achieve similar functionality.
Note − The traditional "mixed arrays" concept became obsolete after .NET 4.0, as modern C# provides better alternatives like generic collections and tuples.
Syntax
Following is the syntax for creating an array that can hold mixed data types −
object[] arrayName = new object[] { value1, value2, value3 };
Following is the syntax for using Tuple as an alternative −
Tuple<type1, type2> tupleName = new Tuple<type1, type2>(value1, value2);
Using Object Arrays for Mixed Data Types
An object[] array can store elements of any type since all types in C# inherit from the object class −
Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
object[] mixedArray = new object[] {89, 45, "jacob", 9.8};
Console.WriteLine("Mixed Array Elements:");
for(int i = 0; i < mixedArray.Length; i++) {
Console.WriteLine($"Element {i}: {mixedArray[i]} (Type: {mixedArray[i].GetType().Name})");
}
}
}
The output of the above code is −
Mixed Array Elements: Element 0: 89 (Type: Int32) Element 1: 45 (Type: Int32) Element 2: jacob (Type: String) Element 3: 9.8 (Type: Double)
Complex Mixed Arrays with Collections
You can also include collections and other complex objects in mixed arrays −
Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var complexArray = new object[] {
87,
33,
"tim",
6.7,
new List<string>() {"football", "tennis", "squash", "cricket"}
};
Console.WriteLine("Complex Mixed Array:");
foreach(var item in complexArray) {
if(item is List<string> list) {
Console.WriteLine($"List: [{string.Join(", ", list)}]");
} else {
Console.WriteLine($"Value: {item} (Type: {item.GetType().Name})");
}
}
}
}
The output of the above code is −
Complex Mixed Array: Value: 87 (Type: Int32) Value: 33 (Type: Int32) Value: tim (Type: String) Value: 6.7 (Type: Double) List: [football, tennis, squash, cricket]
Modern Alternatives: Using Tuples
For structured mixed data, tuples provide a better alternative than mixed arrays −
Example
using System;
class Program {
static void Main() {
Tuple<int, string> tuple = new Tuple<int, string>(60, "John");
Console.WriteLine($"ID: {tuple.Item1}");
Console.WriteLine($"Name: {tuple.Item2}");
// Using ValueTuple (C# 7.0+) - more concise syntax
var person = (Id: 99, Name: "Jack");
Console.WriteLine($"Person ID: {person.Id}, Name: {person.Name}");
}
}
The output of the above code is −
ID: 60 Name: John Person ID: 99, Name: Jack
Comparison of Approaches
| Approach | Best Use Case | Type Safety |
|---|---|---|
| object[] Arrays | Dynamic collections with unknown types | Runtime type checking required |
| Tuples | Fixed structure with known types | Compile-time type safety |
| Generic Collections | Homogeneous data with flexibility | Strong compile-time type safety |
Conclusion
While mixed arrays using object[] allow storing different data types in a single array, modern C# favors type-safe alternatives like tuples and generic collections. Use object[] only when you truly need dynamic typing, and prefer tuples or strongly-typed collections for better performance and maintainability.
