C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# One-dimensional Array

In C#, an array is a collection of elements of the same data type. A one-dimensional array is a simple list of elements. Here's a tutorial on how to work with one-dimensional arrays in C#:

  • Declare and initialize an array:

To declare and initialize a one-dimensional array, use the following syntax:

dataType[] arrayName = new dataType[arraySize];

Here, dataType is the type of elements in the array (e.g., int, float, string), arrayName is the name of the array, and arraySize is the number of elements in the array.

For example, to declare and initialize an integer array with 5 elements:

int[] numbers = new int[5];

You can also initialize an array with values:

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

Or, you can use the simplified syntax:

int[] numbers = { 1, 2, 3, 4, 5 };
  • Access array elements:

You can access array elements using their indices (0-based). To get or set the value of an element, use the following syntax:

arrayName[index] = value;

For example, to set the first element of the numbers array to 10:

numbers[0] = 10;

To get the value of the first element:

int firstNumber = numbers[0];
  • Loop through array elements:

You can use a for loop, a foreach loop, or LINQ to iterate through array elements.

For example, using a for loop:

for (int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);
}

Using a foreach loop:

foreach (int number in numbers)
{
    Console.WriteLine(number);
}
  • Array methods and properties:

Arrays have a Length property, which returns the number of elements in the array:

int length = numbers.Length;

You can use the Array class to perform various operations on arrays, such as sorting, searching, or reversing:

Array.Sort(numbers); // Sorts the array in ascending order
Array.Reverse(numbers); // Reverses the order of elements in the array
int index = Array.IndexOf(numbers, 3); // Finds the index of the first occurrence of the value 3 in the array

That's the basic tutorial on one-dimensional arrays in C#. Arrays are very useful for storing and manipulating collections of data, and they are an essential part of any programming language.

  1. C# One-Dimensional Array Example:

    // Declare and initialize a one-dimensional array of integers
    int[] numbers = { 1, 2, 3, 4, 5 };
    
  2. How to Declare and Initialize a One-Dimensional Array in C#:

    // Declare and initialize a one-dimensional array of strings
    string[] fruits = new string[] { "Apple", "Orange", "Banana" };
    
  3. Accessing Elements in a C# One-Dimensional Array:

    // Accessing elements by index
    string firstFruit = fruits[0];
    
  4. Iterating Through a One-Dimensional Array in C#:

    // Iterate through the array using a foreach loop
    foreach (int number in numbers)
    {
        Console.WriteLine(number);
    }
    
  5. Length Property of One-Dimensional Arrays in C#:

    // Get the length of the array
    int arrayLength = numbers.Length;
    
  6. Sorting a One-Dimensional Array in C#:

    // Sort the array in ascending order
    Array.Sort(numbers);
    
  7. Searching for an Element in a One-Dimensional Array in C#:

    // Search for an element (e.g., number 3) in the array
    int index = Array.IndexOf(numbers, 3);
    
  8. Modifying Elements in a One-Dimensional Array in C#:

    // Modify an element at a specific index
    numbers[1] = 10;
    
  9. Passing One-Dimensional Arrays as Parameters in C#:

    // Method that takes a one-dimensional array as a parameter
    static void PrintArray(int[] arr)
    {
        foreach (int num in arr)
        {
            Console.WriteLine(num);
        }
    }
    
    // Call the method with the 'numbers' array
    PrintArray(numbers);
    
  10. Common Operations with One-Dimensional Arrays in C#:

    // Example of common operations
    int[] newArray = new int[5];
    
    // Copy elements from 'numbers' to 'newArray'
    Array.Copy(numbers, newArray, numbers.Length);
    
    // Fill the array with a specific value
    Array.Fill(newArray, 99);
    
    // Reverse the order of elements in the array
    Array.Reverse(newArray);