
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - CreateInstance() Method
The C# Array CreateInstance() method is used to create new instance of an array with a specified type, dimensions, and lengths for each dimension.
The array that is created may have one or multiple dimensions, depending on the length that is given.
This method is especially useful when the type or dimensions of the array are not known at compile time.
Exception
There are the following exceptions of CreateInstance() methods −
- ArgumentNullException: Thrown if the elementType is null.
- ArgumentOutOfRangeException: Thrown if any of the dimensions are less than zero
- ArgumentException: Thrown if the elementType is invalid
Syntax
Following is the syntax of the C# Array CreateInstance() method −
public static Array CreateInstance (Type elementType, int length);
Parameters
This method accepts the following parameters −
- elementType: Specifies the type of the array's elements.
- length: Specifies the size of array in each dimension.
Return value
This method returns an instance of the array class.
Example 1: Create a One-Dimensional Array
This is the basic example of theCreateInstance()method to create an instance of one-dimensional array −
using System; class Program { static void Main() { // use CreateInstance method Array array = Array.CreateInstance(typeof(int), 5); for (int i = 0; i < array.Length; i++) { array.SetValue(i * 10, i); } foreach (var item in array) { Console.Write(item + " "); } } }
Output
Following is the output −
0 10 20 30 40
Example 2: Create Multi-Dimensional Array
Let us see another example of the CreateInstance() method to create an instance of multi-dimensional array −
using System; class Program { static void Main() { Array array = Array.CreateInstance(typeof(int), 3, 2); // Fill the array with values for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { array.SetValue((i + 1) * (j + 1), i, j); } } // Print the array for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { Console.Write(array.GetValue(i, j) + " "); } Console.WriteLine(); } } }
Output
Following is the output −
1 2 2 4 3 6
Example 3: Create an Array of String
In this example of the CreateInstance() method. We create an instance of one-dimensional string array −
using System; class Program { static void Main() { // Create a one-dimensional array of strings Array stringArray = Array.CreateInstance(typeof(string), 4); stringArray.SetValue("Apple", 0); stringArray.SetValue("Banana", 1); stringArray.SetValue("Cherry", 2); stringArray.SetValue("Date", 3); foreach (var item in stringArray) { Console.Write(item + " "); } } }
Output
Following is the output −
Apple Banana Cherry Date
Example 4: Handling Invalid Dimension
Following is another example of theCreateInstance()method. Here, we are attempting to create an array with a negative dimension −
using System; class Program { static void Main() { try { // Attempt to create an array with a negative dimension Array array = Array.CreateInstance(typeof(int), -5); } catch (Exception ex) { Console.WriteLine($"Exception: {ex.Message}"); } } }
Output
Following is the output −
Exception: MonoArgumentException:NULL