Array in C#

  • How to create a const array in C#:

You can create a const array in C# using the readonly keyword. Here is an example:

readonly int[] myArray = { 1, 2, 3 };

This will create a const array myArray with values 1, 2, and 3.

  • How to create an empty array in C#:

You can create an empty array in C# by specifying the length of the array as 0. Here is an example:

int[] myArray = new int[0];

This will create an empty array myArray.

  • Add values to an array in C#:

You can add values to an array in C# by assigning values to array elements. Here is an example:

int[] myArray = new int[3];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

This will create an array myArray with values 1, 2, and 3.

Alternatively, you can create an array with values using array initialization syntax. Here is an example:

int[] myArray = { 1, 2, 3 };

This will create an array myArray with values 1, 2, and 3.

  • Check if a value is in an array in C#:

You can use the Array.IndexOf method to check if a value is in an array in C#. Here is an example:

int[] myArray = { 1, 2, 3 };
int value = 2;
if (Array.IndexOf(myArray, value) != -1)
{
    Console.WriteLine("Value " + value + " is in the array");
}
else
{
    Console.WriteLine("Value " + value + " is not in the array");
}

This will output "Value 2 is in the array" because the value 2 is in the myArray array. If the value is not in the array, it will output "Value x is not in the array", where x is the value you are searching for.

  1. Declare and Initialize Array in C#:

    Declare and initialize an array in C#.

    int[] numbers = new int[] { 1, 2, 3, 4, 5 };
    
  2. C# Array Length Property:

    Use the Length property to get the number of elements in an array.

    int[] numbers = { 1, 2, 3, 4, 5 };
    int length = numbers.Length;
    
  3. Multidimensional Arrays in C#:

    Declare and initialize a multidimensional array in C#.

    int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };
    
  4. Accessing Elements in a C# Array:

    Access elements of an array using index.

    int[] numbers = { 1, 2, 3, 4, 5 };
    int firstElement = numbers[0];
    
  5. Array Initialization in C#:

    Initialize an array with a specific size.

    int[] numbers = new int[5];
    
  6. Sorting Arrays in C#:

    Sort elements of an array in ascending order.

    int[] numbers = { 5, 3, 1, 4, 2 };
    Array.Sort(numbers);
    
  7. Iterating Through an Array in C#:

    Use loops to iterate through elements of an array.

    int[] numbers = { 1, 2, 3, 4, 5 };
    foreach (int number in numbers) {
        Console.WriteLine(number);
    }
    
  8. Copying Arrays in C#:

    Copy elements from one array to another.

    int[] sourceArray = { 1, 2, 3 };
    int[] destinationArray = new int[sourceArray.Length];
    Array.Copy(sourceArray, destinationArray, sourceArray.Length);
    
  9. Searching in Arrays in C#:

    Search for an element in an array.

    int[] numbers = { 1, 2, 3, 4, 5 };
    int searchNumber = 3;
    int index = Array.IndexOf(numbers, searchNumber);
    
  10. Jagged Arrays in C#:

    Declare and initialize a jagged array (array of arrays).

    int[][] jaggedArray = new int[2][];
    jaggedArray[0] = new int[] { 1, 2, 3 };
    jaggedArray[1] = new int[] { 4, 5, 6, 7 };
    
  11. Dynamic Arrays in C#:

    Use List<T> for dynamic arrays.

    List<int> dynamicArray = new List<int>();
    dynamicArray.Add(1);
    dynamicArray.Add(2);
    
  12. Array vs List in C#:

    Understand the differences between arrays and lists in terms of flexibility and functionality.

    // Array
    int[] array = { 1, 2, 3 };
    
    // List
    List<int> list = new List<int> { 1, 2, 3 };