TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Arrays

C# Arrays for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Arrays

Like other programming languages, array in C# is a group of similar types of elements that have contiguous memory location. In C#, array is an object of base type System.Array. In C#, array index starts from 0. We can store only fixed set of elements in C# array.

C# array

Advantages of C# Array

  • Code Optimization (less code)
  • Random Access
  • Easy to traverse data
  • Easy to manipulate data
  • Easy to sort data etc.

Disadvantages of C# Array

  • Fixed size

C# Array Types

There are 3 types of arrays in C# programming:

  1. Single Dimensional Array
  2. Multidimensional Array
  3. Jagged Array

C# Single Dimensional Array

To create single dimensional array, you need to use square brackets [] after the type.

        int[] arr = new int[5];//creating array

You cannot place square brackets after the identifier.

        int arr[] = new int[5];//compile time error

Let's see a simple example of C# array, where we are going to declare, initialize and traverse array.

using System;
public class ArrayExample
{
    public static void Main(string[] args)
    {
        int[] arr = new int[5];//creating array
        arr[0] = 10;//initializing array
        arr[2] = 20;
        arr[4] = 30;

        //traversing array
        for (int i = 0; i < arr.Length; i++)
        {
            Console.WriteLine(arr[i]);
        }
    }
}

Output:

10
0
20
0
30

C# Array Example: Declaration and Initialization at same time

There are 3 ways to initialize array at the time of declaration.

        int[] arr = new int[5]{ 10, 20, 30, 40, 50 };

We can omit the size of array.

        int[] arr = new int[]{ 10, 20, 30, 40, 50 };

We can omit the new operator also.

        int[] arr = { 10, 20, 30, 40, 50 };

Let's see the example of array where we are declaring and initializing array at the same time.

using System;
public class ArrayExample
{
    public static void Main(string[] args)
    {
        int[] arr = { 10, 20, 30, 40, 50 };//Declaration and Initialization of array
 
        //traversing array
        for (int i = 0; i < arr.Length; i++)
        {
            Console.WriteLine(arr[i]);
        }
    }
}

Output:

10
20
30
40
50

C# Array Example: Traversal using foreach loop

We can also traverse the array elements using foreach loop. It returns array element one by one.

using System;
public class ArrayExample
{
    public static void Main(string[] args)
    {
        int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing array
 
        //traversing array
        foreach (int i in arr)
        {
            Console.WriteLine(i);
        }
    }
}

Output:

10
20
30
40
50




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf