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 are values assigned to arrays in C#?
Declaring an array does not initialize the array in memory. When the array variable is initialized, you can assign values to the array using several different approaches in C#.
Array is a reference type, so you need to use the new keyword to create an instance of the array. There are multiple ways to assign values to arrays, each with its own syntax and use cases.
Syntax
Following is the syntax for declaring and initializing arrays −
// Declaration with size
datatype[] arrayName = new datatype[size];
// Assignment using index
arrayName[index] = value;
// Declaration with initialization
datatype[] arrayName = new datatype[size] {value1, value2, ...};
// Simplified initialization
datatype[] arrayName = {value1, value2, ...};
Using Index-Based Assignment
After declaring an array with a specific size, you can assign values to individual elements using their index positions −
using System;
class Program {
public static void Main() {
double[] price = new double[5];
price[0] = 3245.50;
price[1] = 1234.50;
price[2] = 8765.50;
price[3] = 5784.50;
price[4] = 6576.50;
Console.WriteLine("Array values using index assignment:");
for (int i = 0; i < price.Length; i++) {
Console.WriteLine($"price[{i}] = {price[i]}");
}
}
}
The output of the above code is −
Array values using index assignment: price[0] = 3245.5 price[1] = 1234.5 price[2] = 8765.5 price[3] = 5784.5 price[4] = 6576.5
Using Array Initialization at Declaration
You can assign values to the array at the time of declaration using the array initializer syntax −
using System;
class Program {
public static void Main() {
double[] price = new double[5] {3245.50, 1234.50, 8765.50, 5784.50, 6576.50};
Console.WriteLine("Array values using initialization:");
foreach (double p in price) {
Console.WriteLine(p);
}
}
}
The output of the above code is −
Array values using initialization: 3245.5 1234.5 8765.5 5784.5 6576.5
Using Simplified Array Initialization
C# provides a shorthand syntax where you can omit the size and new keyword when initializing arrays −
using System;
class Program {
public static void Main() {
string[] cities = {"New York", "London", "Paris", "Tokyo"};
int[] numbers = {10, 20, 30, 40, 50};
Console.WriteLine("Cities:");
for (int i = 0; i < cities.Length; i++) {
Console.WriteLine($"{i + 1}. {cities[i]}");
}
Console.WriteLine("\nNumbers:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Cities: 1. New York 2. London 3. Paris 4. Tokyo Numbers: 10 20 30 40 50
Comparison of Array Assignment Methods
| Method | Syntax | Use Case |
|---|---|---|
| Index Assignment | arr[0] = value; |
When values are determined dynamically or conditionally |
| Initialization with Size | new int[3] {1, 2, 3} |
When you know both size and values at compile time |
| Simplified Initialization | {1, 2, 3} |
Most concise when values are known at declaration |
Conclusion
C# provides multiple ways to assign values to arrays: index-based assignment for dynamic values, initialization with size for explicit control, and simplified initialization for concise code. Choose the method that best fits your specific use case and coding style.
