C# Arrays

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:15

C# Arrays:

Arrays are collections of elements of the same type.

Example:

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

Single Dimensional Array:

Single-dimensional arrays are linear arrays with a single row/column of elements.

Example:

int[] scores = new int[5];

Multidimensional Array:

Multidimensional arrays have multiple dimensions, like 2D arrays (matrices).

Example:

int[,] matrix = new int[3, 3];

Jagged Array:

Jagged arrays are arrays of arrays, each sub-array can have a different length.

Example:

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2 };
jaggedArray[1] = new int[] { 3, 4, 5 };
jaggedArray[2] = new int[] { 6 };

C# Passing Array to Function:

You can pass an array to a function to perform operations on it.

Example:

static void PrintArray(int[] arr)
{
    foreach (int num in arr)
    {
        Console.WriteLine(num);
    }
}

C# ‘params’:

‘params’ keyword allows passing a variable number of arguments to a method.

Example:

static int Sum(params int[] numbers)
{
    int result = 0;
    foreach (int num in numbers)
    {
        result += num;
    }
    return result;
}

C# Array class:

The ‘Array’ class provides methods for working with arrays, like ‘Sort’ and ‘IndexOf’.

Example:

int[] numbers = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
Array.Sort(numbers);

C# Command Line Arguments:

Command line arguments are values passed when running a program from the command line.

Example:

static void Main(string[] args)
{
    foreach (string arg in args)
    {
        Console.WriteLine(arg);
    }
}
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this