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
How to use the GetValue() method of array class in C#?
The GetValue() method of the Array class in C# retrieves the value at a specified position in a multi-dimensional or single-dimensional array. This method is particularly useful when working with arrays created using Array.CreateInstance(), as it provides a generic way to access array elements regardless of the array's underlying type.
Syntax
Following is the syntax for using GetValue() with different array dimensions −
// For single-dimensional array object value = array.GetValue(index); // For multi-dimensional array object value = array.GetValue(index1, index2); object value = array.GetValue(index1, index2, index3);
Parameters
index − A 32-bit integer representing the position of the element to retrieve.
index1, index2, etc. − Integer indices for each dimension of a multi-dimensional array.
Return Value
The method returns an object containing the value at the specified position. You may need to cast it to the appropriate type for further processing.
Using GetValue() with Multi-Dimensional Arrays
The following example demonstrates creating a 2D array and retrieving values using GetValue() −
using System;
class Program {
static void Main(string[] args) {
Array arr = Array.CreateInstance(typeof(String), 3, 6);
// Setting values in the 2D array
arr.SetValue("One", 0, 0);
arr.SetValue("Two", 0, 1);
arr.SetValue("Three", 0, 2);
arr.SetValue("Four", 0, 3);
arr.SetValue("Five", 1, 4);
arr.SetValue("Six", 1, 5);
arr.SetValue("Seven", 1, 2);
arr.SetValue("Eight", 1, 3);
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);
Console.WriteLine("Array contents:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
object value = arr.GetValue(i, j);
if (value != null) {
Console.WriteLine($"Position [{i},{j}]: {value}");
}
}
}
}
}
The output of the above code is −
Array contents: Position [0,0]: One Position [0,1]: Two Position [0,2]: Three Position [0,3]: Four Position [1,2]: Seven Position [1,3]: Eight Position [1,4]: Five Position [1,5]: Six
Using GetValue() with Single-Dimensional Arrays
Here's an example showing GetValue() with a single-dimensional array −
using System;
class Program {
static void Main(string[] args) {
Array numbers = Array.CreateInstance(typeof(int), 5);
// Setting values
numbers.SetValue(10, 0);
numbers.SetValue(20, 1);
numbers.SetValue(30, 2);
numbers.SetValue(40, 3);
numbers.SetValue(50, 4);
Console.WriteLine("Single-dimensional array:");
for (int i = 0; i < numbers.Length; i++) {
int value = (int)numbers.GetValue(i);
Console.WriteLine($"Index {i}: {value}");
}
}
}
The output of the above code is −
Single-dimensional array: Index 0: 10 Index 1: 20 Index 2: 30 Index 3: 40 Index 4: 50
Comparison with Direct Array Access
| GetValue() Method | Direct Array Access |
|---|---|
| Works with arrays created using Array.CreateInstance() | Works with standard array declarations |
| Returns object type (requires casting) | Returns the actual element type |
| arr.GetValue(i, j) | arr[i, j] |
| More flexible for generic array operations | More direct and type-safe |
Conclusion
The GetValue() method provides a flexible way to retrieve elements from arrays, especially when working with dynamically created arrays using Array.CreateInstance(). It returns an object that needs to be cast to the appropriate type, making it useful for generic array operations where the exact type may not be known at compile time.
