DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 34 of 55
←PreviousPrevNextNext→

C# Arrays - Complete Guide

Introduction to Arrays

Arrays in C# are fixed-size, indexable collections of elements of the same type. They are reference types that derive from System.Array and provide efficient, contiguous storage with O(1) indexed access.

Arrays are zero-indexed (the first element is at index 0). They are useful when the collection size is known up front and when you need fast random access to elements.

Array Declaration and Initialization

Arrays can be declared and initialized in several ways in C#:

Example
using System;

namespace ArrayDeclarationExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Method 1: Declaration then initialization
            int[] numbers1;
            numbers1 = new int[5]; // Array of 5 integers (default values: 0)
            
            // Method 2: Declaration with initialization size
            int[] numbers2 = new int[5];
            
            // Method 3: Declaration with explicit values
            int[] numbers3 = new int[] { 1, 2, 3, 4, 5 };
            
            // Method 4: Implicit-size initializer (common)
            int[] numbers4 = { 1, 2, 3, 4, 5 };
            
            // Method 5: Using var with explicit new[]
            var numbers5 = new int[] { 1, 2, 3, 4, 5 };
            
            // Different element types
            string[] names = { "Alice", "Bob", "Charlie" };
            double[] prices = { 9.99, 19.99, 29.99 };
            bool[] flags = { true, false, true };
            
            // Display array information
            Console.WriteLine($"numbers1 length: {numbers1.Length}");
            Console.WriteLine($"numbers4 length: {numbers4.Length}");
            Console.WriteLine($"names length: {names.Length}");
            
            // Accessing array elements
            Console.WriteLine($"\nFirst element of numbers4: {numbers4[0]}");
            Console.WriteLine($"Second element of names: {names[1]}");
            Console.WriteLine($"Last element of prices: {prices[prices.Length - 1]}");
            
            // Modifying array elements
            numbers4[0] = 10;
            names[1] = "Robert";
            
            Console.WriteLine($"\nModified numbers4[0]: {numbers4[0]}");
            Console.WriteLine($"Modified names[1]: {names[1]}");
            
            // Default values
            Console.WriteLine($"\nDefault values in numbers1:");
            for (int i = 0; i < numbers1.Length; i++)
            {
                Console.WriteLine($"numbers1[{i}] = {numbers1[i]}");
            }
        }
    }
}
Test your knowledge: C# Arrays - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 34 of 55
←PreviousPrevNextNext→