Forem

Cover image for CRUD operations on Arrays
LetsUpdateSkills
LetsUpdateSkills

Posted on

CRUD operations on Arrays

Arrays in C# are fixed in size and do not have built-in methods for dynamic operations (like Add or Remove). However, you can perform CRUD operations by working with array elements.

1. Create Operation

You can initialize arrays with specific values or create an empty array and populate it later.

Example

// Creating an array with predefined values
int[] numbers = { 10, 20, 30 };

// Creating an empty array and assigning values later
int[] grades = new int[3];
grades[0] = 85;
grades[1] = 90;
grades[2] = 78;

Enter fullscreen mode Exit fullscreen mode

2. Read Operation

Access specific elements or iterate through the array.

Example

int[] numbers = { 10, 20, 30 };

// Accessing elements by index
Console.WriteLine(numbers[0]); // Output: 10

// Iterating through the array
foreach (var num in numbers)
{
    Console.WriteLine(num);
}

Enter fullscreen mode Exit fullscreen mode

3. Update Operation

Modify existing elements by accessing them via their index.

int[] numbers = { 10, 20, 30 };

// Updating the value at index 1
numbers[1] = 25;

foreach (var num in numbers)
{
    Console.WriteLine(num);
}
// Output: 10, 25, 30

Enter fullscreen mode Exit fullscreen mode

Please click here to see complete tutorial

Top comments (0)