
- 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 - SetValue() Method
The C# Array SetValue() method is used to set a value at a specific index in an array. It provides the flexibility to set values in both single-dimensional and multi-dimensional arrays.
The value we assign or set to the array should be compatible with the array type. Otherwise, we will get an InvalidCastException.
Syntax
Following is the syntax of the C# Array SetValue() method −
public void SetValue(object value, params int[] indices);
Parameters
This method accepts the following parameters −
- value: The value to assign to the specified array element.
- indices: An array of integers specifying the position of the element to set. A 1D array provides a single index. For 2D array provides two indices.
Return value
This method does not return any value.
Example 1: Set Value in one-Dimensional Array
Let us create a basic example of the SetValue() method. Here, we demonstrate how to set and get a specific value in a one-dimensional −
using System; public class SamplesArray { public static void Main() { String[] myArr1 = new String[5]; // Sets the element at index 3. myArr1.SetValue( "three", 3 ); Console.WriteLine( "[3]: {0}", myArr1.GetValue( 3 ) ); } }
Output
Following is the output −
[3]: three
Example 2: Set Value in Two-Dimensional Array
Let us see another example of the SetValue() method to set and get the value in two dimensional array −
using System; public class SamplesArray { public static void Main() { // Creates and initializes a two-dimensional array. String[,] arr = new String[5,5]; // Sets the element at index 1,3. arr.SetValue( "one three", 1, 3 ); Console.WriteLine( "[1,3]: {0}", arr.GetValue( 1, 3 ) ); } }
Output
Following is the output −
[1,3]: one three
Example 3: Set The Value in Three Dimensional Array
This is another, example of the SetValue() method. Here, we set and get the value in three dimensional array −
using System; public class SamplesArray { public static void Main() { // Creates and initializes a three-dimensional array. String[,,] myArr3 = new String[5,5,5]; // Sets the element at index 1,2,3. myArr3.SetValue( "one two three", 1, 2, 3 ); Console.WriteLine( "[1,2,3]: {0}", myArr3.GetValue( 1, 2, 3 ) ); } }
Output
Following is the output −
[1,2,3]: one two three
Example 4: Using SetValue with Type Conversion
Here, in this example, we use the SetValue() method with type conversion. The SetValue method will attempt type conversion when necessary, but a runtime exception will occur if the conversion is invalid −
using System; class Program { static void Main() { // Create an object array object[] items = new object[3]; // Set values of different types items.SetValue(42, 0); items.SetValue("Hello", 1); items.SetValue(DateTime.Now, 2); foreach (var item in items) { Console.WriteLine(item); } } }
Output
Following is the output −
42 Hello 1/4/2025 10:52:06 AM