ุงูู Array ูู ูุงุญุฏ ู
ู ุฃูู
ุงูู
ูุงููู
ูู ุงูุจุฑู
ุฌุฉุ ูุจูุณุชุฎุฏู
ู ุนุดุงู ูุญู ู
ุดููุฉ ุชุฎุฒูู ุฃูุชุฑ ู
ู ููู
ุฉ ูู ู
ุชุบูุฑ ูุงุญุฏ. ุจุฏู ู
ุง ูุนุฑูู 100 ู
ุชุบูุฑ ุนุดุงู ูุฎุฒู 100 ุฑูู
ุ ููุฏุฑ ูุณุชุฎุฏู
Array ูุงุญุฏ ูุฎุฒููู
ูููู
.
ุงูู Array ูู ุนุจุงุฑุฉ ุนู ู
ุฌู
ูุนุฉ ู
ู ุงูุนูุงุตุฑ ู
ู ููุณ ููุน ุงูุจูุงูุงุช ุจุชุชุฎุฒู ุชุญุช ุงุณู
ูุงุญุฏ. ูู C#ุ ุงูู Arrays ุชุนุชุจุฑ ู
ู ุงููReference Typesุ ูุฏู ู
ุนูุงู ุฅููุง ุจุชุชุนุงู
ู ูู objects ู
ู ุงูู class ุงููู ุงุณู
ู System.Array ูุจูุชู
ุชุฎุฒูููุง ูู ุงูู Heap.
Array Properties
ุงูู Arrays ูููุง ุดููุฉ ุฎุตุงุฆุต ุฃุณุงุณูุฉ ุจุชู
ูุฒูุง:
- ุงูู Fixed Type (Same Data Type): ูู ุงูุนูุงุตุฑ ุงููู ุฌูู ุงูู
Arrayูุงุฒู ุชููู ู ู ููุณ ููุน ุงูุจูุงูุงุช. - ุงูู Fixed Size: ุญุฌู
ุงูู
Arrayุซุงุจุช ูู ุจูุชุบูุฑุด ุจุนุฏ ู ุง ูุญุฏุฏู. ูุงุฒู ูููู ุนุงุฑููู ู ุณุจููุง ููุฎุฒู ูุงู ุนูุตุฑ. - ุงูู Sequential in Memory: ุงูุนูุงุตุฑ ุจุชุชุฎุฒู ูู ุฃู ุงูู ู ุชุชุงููุฉ ูุฑุง ุจุนุถ ูู ุงูุฐุงูุฑุฉุ ูุฏู ุจูุฎูู ุงููุตูู ูููุง ุณุฑูุน.
- ุงูู 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: OpelAccessing 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 runtimeMemory 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:
| Feature | for Loop | foreach 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: 6Looping
ุจูุณุชุฎุฏู
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: 4Advantages and Disadvantages of Arrays
Advantages
- ุงูู Efficiency: ุชุฎุฒูู ุงูุนูุงุตุฑ ูุฑุง ุจุนุถ ูู ุงูุฐุงูุฑุฉ ุจูุฎูู ุงููุตูู ูููุง ุณุฑูุน ุฌุฏูุง.
- ุงูู Strongly Typed: ุงูู
Arraysูู C# ูููุฉ ุงูููุนุ ูุนูู ู ุจุชุณู ุญุด ุจุชุฎุฒูู ุฃููุงุน ู ุฎุชููุฉ ูู ููุณ ุงููarray. ุฏู ุจูู ูุน ุฃุฎุทุงุก ุงููruntimeูุจูุญุณู ุงูุฃุฏุงุก (ูุฃูู ุจูุชุฌูุจ ุนู ููุงุช ุงููboxing/unboxing). - ุงูู Foundation: ุจุชุณุชุฎุฏู
ูุจูุงุก ููุงูู ุจูุงูุงุช ุชุงููุฉ ุฃุนูุฏ ุฒู
Lists,Stacks,Queues.
Disadvantages
- ุงูู Fixed Size: ุฃูุจุฑ ุนูุจ ูู ุฅู ุญุฌู
ูุง ุซุงุจุช. ูู ุญุฌุฒุช ุญุฌู
ูุจูุฑ ุฒูุงุฏุฉ ุนู ุงููุฒูู
ุ ุจุชูุฏุฑ ุงูุฐุงูุฑุฉ. ููู ุญุฌุฒุช ุญุฌู
ููููุ ูุชุญุชุงุฌ ุชุนู
ู
arrayุฌุฏูุฏุฉ ูุชูุณุฎ ุงูุนูุงุตุฑ ุงููุฏูู ุฉุ ูุฏู ู ููู. - ุงูู Manipulation: ุตุนุจ ุชุถูู ุฃู ุชุญุฐู ุนูุตุฑ ู
ู ูุต ุงูู
arrayู ู ุบูุฑ ู ุง ุชุนู ู ุนู ููุงุช ููู ููุณุฎ ูุชูุฑ.
ุจุณุจุจ ุงูุนููุจ ุฏูุ C# ุจุชูุฏู
ุญููู ุชุงููุฉ ุฒู ุงูู Collections (ู
ุซู List<T>) ุงููู ุจุชููุฑ ู
ุฑููุฉ ุฃูุจุฑ ูู ุงูุชุนุงู
ู ู
ุน ุญุฌู
ุงูุจูุงูุงุช.