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;
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);
}
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
5.. Delete Operation
Arrays are fixed in size, so you cannot directly delete an element. However, you can simulate deletion by creating a new array or shifting elements.
Example
int[] numbers = { 10, 20, 30, 40 };
int indexToRemove = 1; // Remove the element at index 1 (20)
// Create a new array
int[] newNumbers = new int[numbers.Length - 1];
// Copy elements except the one to be removed
int j = 0;
for (int i = 0; i < numbers.Length; i++)
{
if (i != indexToRemove)
{
newNumbers[j] = numbers[i];
j++;
}
}
// Print the updated array
foreach (var num in newNumbers)
{
Console.WriteLine(num);
}
// Output: 10, 30, 40
using System;
class Program
{
static void Main()
{
// 1. Create
int[] numbers = { 10, 20, 30, 40 };
Console.WriteLine("Array after creation:");
PrintArray(numbers);
// 2. Read
Console.WriteLine("\nReading element at index 2:");
Console.WriteLine(numbers[2]); // Output: 30
// 3. Update
Console.WriteLine("\nUpdating element at index 1:");
numbers[1] = 25;
PrintArray(numbers);
// 4. Delete
Console.WriteLine("\nDeleting element at index 3:");
numbers = RemoveElement(numbers, 3);
PrintArray(numbers);
}
static int[] RemoveElement(int[] array, int indexToRemove)
{
int[] newArray = new int[array.Length - 1];
int j = 0;
for (int i = 0; i < array.Length; i++)
{
if (i != indexToRemove)
{
newArray[j] = array[i];
j++;
}
}
return newArray;
}
static void PrintArray(int[] array)
{
foreach (var num in array)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
}
Array after creation:
10 20 30 40
Reading element at index 2:
30
Updating element at index 1:
10 25 30 40
Deleting element at index 3:
10 25 30
Limitations of Arrays for CRUD Operations
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;
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); }
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
5.. Delete Operation
Arrays are fixed in size, so you cannot directly delete an element. However, you can simulate deletion by creating a new array or shifting elements.
Example
int[] numbers = { 10, 20, 30, 40 }; int indexToRemove = 1; // Remove the element at index 1 (20) // Create a new array int[] newNumbers = new int[numbers.Length - 1]; // Copy elements except the one to be removed int j = 0; for (int i = 0; i < numbers.Length; i++) { if (i != indexToRemove) { newNumbers[j] = numbers[i]; j++; } } // Print the updated array foreach (var num in newNumbers) { Console.WriteLine(num); } // Output: 10, 30, 40
using System; class Program { static void Main() { // 1. Create int[] numbers = { 10, 20, 30, 40 }; Console.WriteLine("Array after creation:"); PrintArray(numbers); // 2. Read Console.WriteLine("\nReading element at index 2:"); Console.WriteLine(numbers[2]); // Output: 30 // 3. Update Console.WriteLine("\nUpdating element at index 1:"); numbers[1] = 25; PrintArray(numbers); // 4. Delete Console.WriteLine("\nDeleting element at index 3:"); numbers = RemoveElement(numbers, 3); PrintArray(numbers); } static int[] RemoveElement(int[] array, int indexToRemove) { int[] newArray = new int[array.Length - 1]; int j = 0; for (int i = 0; i < array.Length; i++) { if (i != indexToRemove) { newArray[j] = array[i]; j++; } } return newArray; } static void PrintArray(int[] array) { foreach (var num in array) { Console.Write(num + " "); } Console.WriteLine(); } }
Array after creation: 10 20 30 40 Reading element at index 2: 30 Updating element at index 1: 10 25 30 40 Deleting element at index 3: 10 25 30
Limitations of Arrays for CRUD Operations
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved