ุงู„ู€ Array ู‡ูˆ ูˆุงุญุฏ ู…ู† ุฃู‡ู… ุงู„ู…ูุงู‡ูŠู… ููŠ ุงู„ุจุฑู…ุฌุฉุŒ ูˆุจู†ุณุชุฎุฏู…ู‡ ุนุดุงู† ู†ุญู„ ู…ุดูƒู„ุฉ ุชุฎุฒูŠู† ุฃูƒุชุฑ ู…ู† ู‚ูŠู…ุฉ ููŠ ู…ุชุบูŠุฑ ูˆุงุญุฏ. ุจุฏู„ ู…ุง ู†ุนุฑู‘ู 100 ู…ุชุบูŠุฑ ุนุดุงู† ู†ุฎุฒู† 100 ุฑู‚ู…ุŒ ู†ู‚ุฏุฑ ู†ุณุชุฎุฏู… Array ูˆุงุญุฏ ูŠุฎุฒู†ู‡ู… ูƒู„ู‡ู….

ุงู„ู€ Array ู‡ูˆ ุนุจุงุฑุฉ ุนู† ู…ุฌู…ูˆุนุฉ ู…ู† ุงู„ุนู†ุงุตุฑ ู…ู† ู†ูุณ ู†ูˆุน ุงู„ุจูŠุงู†ุงุช ุจุชุชุฎุฒู† ุชุญุช ุงุณู… ูˆุงุญุฏ. ููŠ C#ุŒ ุงู„ู€ Arrays ุชุนุชุจุฑ ู…ู† ุงู„ู€Reference TypesุŒ ูˆุฏู‡ ู…ุนู†ุงู‡ ุฅู†ู‡ุง ุจุชุชุนุงู…ู„ ูƒู€ objects ู…ู† ุงู„ู€ class ุงู„ู„ูŠ ุงุณู…ู‡ System.Array ูˆุจูŠุชู… ุชุฎุฒูŠู†ู‡ุง ููŠ ุงู„ู€ Heap.

Array Properties

ุงู„ู€ Arrays ู„ูŠู‡ุง ุดูˆูŠุฉ ุฎุตุงุฆุต ุฃุณุงุณูŠุฉ ุจุชู…ูŠุฒู‡ุง:

  1. ุงู„ู€ Fixed Type (Same Data Type): ูƒู„ ุงู„ุนู†ุงุตุฑ ุงู„ู„ูŠ ุฌูˆู‡ ุงู„ู€ Array ู„ุงุฒู… ุชูƒูˆู† ู…ู† ู†ูุณ ู†ูˆุน ุงู„ุจูŠุงู†ุงุช.
  2. ุงู„ู€ Fixed Size: ุญุฌู… ุงู„ู€ Array ุซุงุจุช ูˆู…ุจูŠุชุบูŠุฑุด ุจุนุฏ ู…ุง ู†ุญุฏุฏู‡. ู„ุงุฒู… ู†ูƒูˆู† ุนุงุฑููŠู† ู…ุณุจู‚ู‹ุง ู‡ู†ุฎุฒู† ูƒุงู… ุนู†ุตุฑ.
  3. ุงู„ู€ Sequential in Memory: ุงู„ุนู†ุงุตุฑ ุจุชุชุฎุฒู† ููŠ ุฃู…ุงูƒู† ู…ุชุชุงู„ูŠุฉ ูˆุฑุง ุจุนุถ ููŠ ุงู„ุฐุงูƒุฑุฉุŒ ูˆุฏู‡ ุจูŠุฎู„ูŠ ุงู„ูˆุตูˆู„ ู„ูŠู‡ุง ุณุฑูŠุน.
  4. ุงู„ู€ Direct Access: ู†ู‚ุฏุฑ ู†ูˆุตู„ ู„ุฃูŠ ุนู†ุตุฑ ุจุดูƒู„ ู…ุจุงุดุฑ ูˆููˆุฑูŠ ุนู† ุทุฑูŠู‚ ุงู„ู€ index ุจุชุงุนู‡.

How to Create and Initialize an Array

ููŠู‡ ุทุฑู‚ ู…ุฎุชู„ูุฉ ุนุดุงู† ู†ู†ุดุฆ ูˆู†ุฎุตุต ู‚ูŠู… ู„ู„ู€ Array:

1. Declaration and Allocation Separately

// Step 1: Declare a reference for an array of integers.
// At this point, Arr is just a reference pointing to null.
// Zero bytes have been allocated in the Heap for the array itself.
int[] Arr;
 
// Step 2: Allocate memory in the Heap for 5 integers.
// The array is initialized with the default value for its type (0 for int).
Arr = new int[5]; 

2. Declaration and Initialization Together

// The most explicit way
int[] Arr1 = new int[5] { 3, 4, 5, 6, 7 };
 
// The compiler can infer the size from the number of elements
int[] Arr2 = new int[] { 3, 4, 5, 6, 7 };
 
// The simplest and most common syntax
int[] Arr3 = { 3, 4, 5, 6, 7 };

3. Implicitly Typed Arrays (var)

ู„ู…ุง ุจู†ุณุชุฎุฏู… varุŒ ุงู„ู€ compiler ุจูŠุณุชู†ุชุฌ ู†ูˆุน ุงู„ู€ Array ู…ู† ุงู„ุนู†ุงุตุฑ ุงู„ู„ูŠ ุจู†ู…ุฑุฑู‡ุง.

// The compiler knows this is an int[] array
var numbers = new[] { 10, 20, 30, 40, 50 };

Accessing and Changing Array Elements

ุจู†ุณุชุฎุฏู… ุงู„ู€ index ุนุดุงู† ู†ูˆุตู„ ู„ุนู†ุงุตุฑ ุงู„ู€ Array. ุงู„ู€ index ุฏุงูŠู…ู‹ุง ุจูŠุจุฏุฃ ู…ู† 0.

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
 
// Access the first element
Console.WriteLine(cars[0]);
// Outputs: Volvo

ูˆุจุฑุถูˆ ุจู†ุณุชุฎุฏู… ุงู„ู€ index ุนุดุงู† ู†ุบูŠุฑ ู‚ูŠู…ุฉ ุฃูŠ ุนู†ุตุฑ:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
 
// The value at index 0 is now changed
Console.WriteLine(cars[0]);
// Outputs: Opel

Accessing an Element Out of Bounds

ู„ูˆ ุญุงูˆู„ุช ุชูˆุตู„ ู„ู€ index ู…ุด ู…ูˆุฌูˆุฏ ููŠ ุงู„ู€ Array (ุฃูƒุจุฑ ู…ู† Length - 1)ุŒ ุงู„ุจุฑู†ุงู…ุฌ ู…ุด ู‡ูŠุทู„ุน error ูˆู‚ุช ุงู„ู€ compilationุŒ ู„ูƒู† ู‡ูŠุญุตู„ runtime error ูˆู‡ูŠุธู‡ุฑ Exception ู…ู† ู†ูˆุน IndexOutOfRangeException.

string[] countries = new string[3];
countries[3] = "India"; // This will cause an IndexOutOfRangeException at runtime

Memory Representation of Arrays

ู„ู…ุง ุจุชู†ุดุฆ ArrayุŒ ุงู„ุนู†ุงุตุฑ ุจุชุงุนุชู‡ ุจุชุชุฎุฒู† ููŠ ุงู„ุฐุงูƒุฑุฉ ูˆุฑุง ุจุนุถ. ู…ุซู„ู‹ุง ู„ูˆ ุนู†ุฏู†ุง int[] A = new int[5]; ูˆุงูุชุฑุถู†ุง ุฅู† ูƒู„ int ุจูŠุงุฎุฏ 4 bytesุŒ ู‡ูŠูƒูˆู† ุดูƒู„ู‡ู… ููŠ ุงู„ุฐุงูƒุฑุฉ ูƒุงู„ุชุงู„ูŠ:

graph LR
    subgraph Heap Memory
        direction LR
        A["A[0]"] --- B["A[1]"] --- C["A[2]"] --- D["A[3]"] --- E["A[4]"]
    end
    
    subgraph Stack Memory
        ref[Array Reference 'A'] --> A
    end

ุงู„ู€ Array ู†ูุณู‡ (ุงู„ุนู†ุงุตุฑ) ู…ูˆุฌูˆุฏ ููŠ ุงู„ู€ HeapุŒ ูˆุงู„ู…ุชุบูŠุฑ A ุงู„ู„ูŠ ููŠ ุงู„ู€ Stack ู‡ูˆ ู…ุฌุฑุฏ reference ุจูŠุดุงูˆุฑ ุนู„ู‰ ุฃูˆู„ ุนู†ุตุฑ ููŠ ุงู„ู€ Array.

Looping Through an Array

ุนุดุงู† ู†ู„ู ุนู„ู‰ ูƒู„ ุนู†ุงุตุฑ ุงู„ู€ ArrayุŒ ุจู†ุณุชุฎุฏู… ุงู„ู€ loops.

int[] arr = new int[3]; // Array is initialized with {0, 0, 0}
 
// Use a 'for' loop to read values from the user and fill the array
for(int i = 0; i < arr.Length; i++)
{
	arr[i] = int.Parse(Console.ReadLine());
}
 
// Use a 'foreach' loop to print the values
foreach (int element in arr)
{
    Console.WriteLine(element);
}

For Loop vs. Foreach Loop

ููŠู‡ ูุฑู‚ ู…ู‡ู… ุจูŠู† ุงู„ุงุชู†ูŠู† ู„ู…ุง ุจู†ุชุนุงู…ู„ ู…ุน Arrays:

Featurefor Loopforeach Loop
Purposeุชู‚ุฏุฑ ุชู‚ุฑุง ูˆุชุนุฏู„ ู‚ูŠู… ุงู„ุนู†ุงุตุฑ.ู„ู„ู‚ุฑุงุกุฉ ูู‚ุท (Read-only).
Loop Variableู…ุชุบูŠุฑ ุงู„ู€ loop ุจูŠูƒูˆู† int ูˆุจูŠู…ุซู„ ุงู„ู€ index.ู…ุชุบูŠุฑ ุงู„ู€ loop ุจูŠูƒูˆู† ู…ู† ู†ูุณ ู†ูˆุน ุนู†ุงุตุฑ ุงู„ู€ Array ูˆุจูŠู…ุซู„ ุงู„ู‚ูŠู…ุฉ ู†ูุณู‡ุง.
Usageู…ู†ุงุณุจ ู„ูˆ ู…ุญุชุงุฌ ุงู„ู€ index ุฃูˆ ู„ูˆ ู‡ุชุนุฏู„ ู‚ูŠู….ู…ู†ุงุณุจ ู„ูˆ ุนุงูŠุฒ ุชู‚ุฑุง ุงู„ู‚ูŠู… ุจุณุŒ ูˆุดูƒู„ู‡ ุฃุจุณุท.

Array as a Reference Type

ู„ุฅู† ุงู„ู€ Array ู‡ูˆ reference typeุŒ ู„ู…ุง ุจุชุนู…ู„ ุนู…ู„ูŠุฉ assignment ุจูŠู† ุงุชู†ูŠู† ArraysุŒ ุฅู†ุช ู…ุจุชู†ุณุฎุด ุงู„ู‚ูŠู…ุŒ ุฅู†ุช ุจุชุฎู„ูŠ ุงู„ุงุชู†ูŠู† references ูŠุดุงูˆุฑูˆุง ุนู„ู‰ ู†ูุณ ุงู„ู€ object ููŠ ุงู„ุฐุงูƒุฑุฉ.

int[] Arr01 = { 1, 7, 5, 3, 8, 6, 4, 2 };
int[] Arr02 = { 7, 8, 9 };
 
Console.WriteLine($"Arr01 HashCode: {Arr01.GetHashCode()}"); // e.g., 27252167
Console.WriteLine($"Arr02 HashCode: {Arr02.GetHashCode()}"); // e.g., 59941933
 
// Assignment: Arr02 now points to the same object as Arr01
Arr02 = Arr01;
 
// Now both references point to the same object in memory
Console.WriteLine("--- After Assignment: Arr02 = Arr01 ---");
Console.WriteLine($"Arr01 HashCode: {Arr01.GetHashCode()}"); // 27252167
Console.WriteLine($"Arr02 HashCode: {Arr02.GetHashCode()}"); // 27252167 (Now identical)
 
// The original object {7, 8, 9} is now unreachable and will be collected by the GC.

Cloning an Array

ู„ูˆ ุนุงูŠุฒ ุชุนู…ู„ ู†ุณุฎุฉ ุฌุฏูŠุฏุฉ ู…ู† ุงู„ู€ Array ุจู‚ูŠู…ู‡ุงุŒ ุจุชุณุชุฎุฏู… Clone() ุฃูˆ Array.Copy().

// Clone() creates a new object with a new identity but the same state (values)
// This performs a Shallow Copy.
Arr02 = (int[])Arr01.Clone();
 
// We must use explicit casting (int[]) because Clone() returns a generic 'object'.
// Derived = Base is not valid, but a cast makes it valid.
 
Console.WriteLine("--- After Clone ---");
Console.WriteLine($"Arr01 HashCode: {Arr01.GetHashCode()}"); // 27252167
Console.WriteLine($"Arr02 HashCode: {Arr02.GetHashCode()}"); // e.g., 2606490 (A new, different hash code)

Array Class Methods and Properties

ุงู„ู€ Array class ุจูŠูˆูุฑ ู…ุฌู…ูˆุนุฉ ู…ู† ุงู„ุฃุฏูˆุงุช ุงู„ุฌุงู‡ุฒุฉ ู„ู„ุชุนุงู…ู„ ู…ุน ุงู„ู€ Arrays.

  • ุงู„ู€ Length: ู‡ูŠ Property ุจุชุฑุฌุน ุงู„ุนุฏุฏ ุงู„ุฅุฌู…ุงู„ูŠ ู„ู„ุนู†ุงุตุฑ.
  • ุงู„ู€ Rank: ู‡ูŠ Property ุจุชุฑุฌุน ุนุฏุฏ ุงู„ุฃุจุนุงุฏ (1 ู„ู„ู€ 1D array).
  • ุงู„ู€ Sort(): ู‡ูŠ Method ุจุชุฑุชุจ ุงู„ุนู†ุงุตุฑ ุชุตุงุนุฏูŠู‹ุง.
  • ุงู„ู€ Clear(): ู‡ูŠ Method ุนุดุงู† ุชู…ุณุญ ูƒู„ ุงู„ุนู†ุงุตุฑ.
  • ุงู„ู€ Reverse(): ู‡ูŠ Method ุจุชุนูƒุณ ุชุฑุชูŠุจ ุงู„ุนู†ุงุตุฑ.
  • ุงู„ู€ Copy(): ู‡ูŠ Method ุจุชู†ุณุฎ ุนู†ุงุตุฑ ู…ู† array ู„ู„ุชุงู†ูŠุฉ.
  • ุงู„ู€ Clone(): ู‡ูŠ Method ุจุชุนู…ู„ ู†ุณุฎุฉ ุฌุฏูŠุฏุฉ ู…ู† ุงู„ู€ Array.
  • ุงู„ู€ Min(), Max(), Sum(): ู‡ูŠ Methods ู…ู† System.Linq ุจุชุญุณุจ ุฃู‚ู„ ู‚ูŠู…ุฉุŒ ุฃูƒุจุฑ ู‚ูŠู…ุฉุŒ ูˆุงู„ู…ุฌู…ูˆุน.

Array.Copy Method

ุงู„ู€Array.Copy ุจุชู†ุณุฎ ุฌุฒุก ู…ู† array ุฃูˆ ูƒู„ู‡ุง ู„ู€ array ุชุงู†ูŠุฉ.

ุจู†ุณุชุฎุฏู…ู‡ุง ุนุดุงู† ู†ุนู…ู„ Deep Copy

Basic Syntax

public static void Copy(Array sourceArray, Array destinationArray, int length);
  • ุงู„ู€ sourceArray: ุงู„ู€ Array ุงู„ู„ูŠ ู‡ู†ู†ุณุฎ ู…ู†ู‡ุง.
  • ุงู„ู€ destinationArray: ุงู„ู€ Array ุงู„ู„ูŠ ู‡ู†ู†ุณุฎ ููŠู‡ุง.
  • ุงู„ู€ length: ุนุฏุฏ ุงู„ุนู†ุงุตุฑ ุงู„ู„ูŠ ู‡ุชุชู†ุณุฎ.

Overload with Indices

public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);

ุงู„ู†ุณุฎุฉ ุฏูŠ ุจุชุณู…ุญู„ูƒ ุชุญุฏุฏ ู‡ุชุจุฏุฃ ุชู†ุณุฎ ู…ู†ูŠู† (sourceIndex) ูˆู‡ุชุญุท ุงู„ู†ุณุฎุฉ ููŠู† (destinationIndex).

Important Considerations

  • ุงู„ู€ Type Compatibility: ุงู„ู†ูˆุนูŠู† ู„ุงุฒู… ูŠูƒูˆู†ูˆุง ู…ุชูˆุงูู‚ูŠู†.
  • ุงู„ู€ Bounds Checking: ุงู„ู€ destinationArray ู„ุงุฒู… ูŠูƒูˆู† ุญุฌู…ู‡ุง ูƒุงููŠ.
  • ุงู„ู€ Shallow Copy: ุงู„ู€ Copy() ุจุชุนู…ู„ Shallow CopyุŒ ูŠุนู†ูŠ ู„ูˆ ุงู„ุนู†ุงุตุฑ reference typesุŒ ู‡ูŠ ู‡ุชู†ุณุฎ ุงู„ู€ references ุจุณ ู…ุด ุงู„ู€ objects ู†ูุณู‡ุง. ุดูˆู Cs Shallow and deep copy.

Types of Arrays in C#

1. Single-Dimensional Array

ุฏู‡ ุงู„ู†ูˆุน ุงู„ู„ูŠ ุงุชูƒู„ู…ู†ุง ุนู†ู‡ ู„ุญุฏ ุฏู„ูˆู‚ุชูŠุŒ ุนุจุงุฑุฉ ุนู† ุตู ูˆุงุญุฏ ู…ู† ุงู„ุจูŠุงู†ุงุช.

2. Multi-Dimensional Array (Rectangular)

ุฏู‡ array ู„ูŠู‡ ุฃูƒุชุฑ ู…ู† ุจุนุฏุŒ ุฃุดู‡ุฑู‡ู… ุงู„ู€ 2D Array ุงู„ู„ูŠ ุจูŠูƒูˆู† ุดุจู‡ ุฌุฏูˆู„ ู„ูŠู‡ ุตููˆู ูˆุฃุนู…ุฏุฉ (rows and columns).

Creation and Access

// dataType[,] arrayName = new dataType[rows, columns];
 
// A 2x3 array (2 rows, 3 columns)
int[,] matrix = { {1, 2, 3}, {4, 5, 6} };
 
// Accessing an element at row 1, column 2
Console.WriteLine(matrix[1, 2]); // Outputs: 6

Looping

ุจู†ุณุชุฎุฏู… nested loops ุนุดุงู† ู†ู„ู ุนู„ู‰ ูƒู„ ุงู„ุนู†ุงุตุฑ.

for (int i = 0; i < matrix.GetLength(0); i++) // GetLength(0) gets the number of rows
{
    for (int j = 0; j < matrix.GetLength(1); j++) // GetLength(1) gets the number of columns
    {
        Console.Write(matrix[i, j] + "\t");
    }
    Console.WriteLine();
}

3. Jagged Array

ุฏู‡ ู†ูˆุน ุฎุงุต ู…ู† ุงู„ู€ 2D ArraysุŒ ุจูŠุณู…ูˆู‡ โ€œarray of arraysโ€. ู‡ู†ุงุŒ ูƒู„ ุตู ู‡ูˆ ุนุจุงุฑุฉ ุนู† 1D array ู…ุณุชู‚ู„ุŒ ูˆู…ู…ูƒู† ูŠูƒูˆู† ู„ูŠู‡ ุทูˆู„ ู…ุฎุชู„ู ุนู† ุจุงู‚ูŠ ุงู„ุตููˆู.

// Syntax: dataType[][] arrayName = new dataType[rows][];
 
// Create a jagged array with 3 rows
int[][] jaggedArray = new int[3][];
 
// Initialize each row with a different size
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
 
// Accessing an element
Console.WriteLine(jaggedArray[1][0]); // Outputs: 4

Advantages and Disadvantages of Arrays

Advantages

  1. ุงู„ู€ Efficiency: ุชุฎุฒูŠู† ุงู„ุนู†ุงุตุฑ ูˆุฑุง ุจุนุถ ููŠ ุงู„ุฐุงูƒุฑุฉ ุจูŠุฎู„ูŠ ุงู„ูˆุตูˆู„ ู„ูŠู‡ุง ุณุฑูŠุน ุฌุฏู‹ุง.
  2. ุงู„ู€ Strongly Typed: ุงู„ู€ Arrays ููŠ C# ู‚ูˆูŠุฉ ุงู„ู†ูˆุนุŒ ูŠุนู†ูŠ ู…ุจุชุณู…ุญุด ุจุชุฎุฒูŠู† ุฃู†ูˆุงุน ู…ุฎุชู„ูุฉ ููŠ ู†ูุณ ุงู„ู€ array. ุฏู‡ ุจูŠู…ู†ุน ุฃุฎุทุงุก ุงู„ู€ runtime ูˆุจูŠุญุณู† ุงู„ุฃุฏุงุก (ู„ุฃู†ู‡ ุจูŠุชุฌู†ุจ ุนู…ู„ูŠุงุช ุงู„ู€ boxing/unboxing).
  3. ุงู„ู€ Foundation: ุจุชุณุชุฎุฏู… ู„ุจู†ุงุก ู‡ูŠุงูƒู„ ุจูŠุงู†ุงุช ุชุงู†ูŠุฉ ุฃุนู‚ุฏ ุฒูŠ Lists, Stacks, Queues.

Disadvantages

  1. ุงู„ู€ Fixed Size: ุฃูƒุจุฑ ุนูŠุจ ู‡ูˆ ุฅู† ุญุฌู…ู‡ุง ุซุงุจุช. ู„ูˆ ุญุฌุฒุช ุญุฌู… ูƒุจูŠุฑ ุฒูŠุงุฏุฉ ุนู† ุงู„ู„ุฒูˆู…ุŒ ุจุชู‡ุฏุฑ ุงู„ุฐุงูƒุฑุฉ. ูˆู„ูˆ ุญุฌุฒุช ุญุฌู… ู‚ู„ูŠู„ุŒ ู‡ุชุญุชุงุฌ ุชุนู…ู„ array ุฌุฏูŠุฏุฉ ูˆุชู†ุณุฎ ุงู„ุนู†ุงุตุฑ ุงู„ู‚ุฏูŠู…ุฉุŒ ูˆุฏู‡ ู…ูƒู„ู.
  2. ุงู„ู€ Manipulation: ุตุนุจ ุชุถูŠู ุฃูˆ ุชุญุฐู ุนู†ุตุฑ ู…ู† ู†ุต ุงู„ู€ array ู…ู† ุบูŠุฑ ู…ุง ุชุนู…ู„ ุนู…ู„ูŠุงุช ู†ู‚ู„ ูˆู†ุณุฎ ูƒุชูŠุฑ.

ุจุณุจุจ ุงู„ุนูŠูˆุจ ุฏูŠุŒ C# ุจุชู‚ุฏู… ุญู„ูˆู„ ุชุงู†ูŠุฉ ุฒูŠ ุงู„ู€ Collections (ู…ุซู„ List<T>) ุงู„ู„ูŠ ุจุชูˆูุฑ ู…ุฑูˆู†ุฉ ุฃูƒุจุฑ ููŠ ุงู„ุชุนุงู…ู„ ู…ุน ุญุฌู… ุงู„ุจูŠุงู†ุงุช.