COMPUTER PROGRAMMING

•ARRAY

# C# PROGRAMMING NOTES

## Lesson 5: Arrays in C#


### 1. What Is an Array?


An **array** is used to store multiple values of the same data type in a single variable.


Instead of creating many variables, we can use one array to store all the values.


**Example: Without an Array**

```csharp

int grade1 = 90;

int grade2 = 85;

int grade3 = 95;

```


**Example: With an Array**

```csharp

int[] grades = {90, 85, 95};

```


**Explanation:**

- `int` – Data type (whole numbers)

- `[]` – Indicates an array

- `grades` – Name of the array

- `{90, 85, 95}` – Values stored inside the array


**Remember:** An array is like a container with several boxes. Each box stores one value.


---


### 2. How to Declare an Array


There are two common ways to create an array.


**Method 1: Declare with Values**

```csharp

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

```

This creates an array containing five integers.


**Method 2: Declare with a Fixed Size**

```csharp

int[] numbers = new int[5];

```

This creates an array with five spaces. Each space initially contains `0`.


**Other Data Types:**

```csharp

string[] names = {"John", "Mark", "Anna"};


double[] prices = {10.5, 20.5, 30.5};


char[] letters = {'A', 'B', 'C'};

```


**Important:** An array stores values of one declared data type.


---


### 3. Understanding Array Indexes


An **index** is the position of a value inside an array.


In C#, array indexes always start at **0**, not 1.


**Example:**

```csharp

string[] fruits = {"Apple", "Banana", "Mango"};

```


| Index | Value |

|---|---|

| 0 | Apple |

| 1 | Banana |

| 2 | Mango |


**Accessing Array Values:**

```csharp

Console.WriteLine(fruits[0]); // Apple

Console.WriteLine(fruits[1]); // Banana

Console.WriteLine(fruits[2]); // Mango

```


**Remember:**

- First element = Index 0

- Second element = Index 1

- Third element = Index 2


If an array has five elements, its valid indexes are `0` to `4`.


---


### 4. How to Change Array Values


We can change an existing value by using its index.


**Example:**

```csharp

int[] scores = {80, 85, 90};


scores[1] = 95;


Console.WriteLine(scores[1]);

```


**Output:**

```text

95

```


**Explanation:**


1. The original value at index 1 is `85`.

2. `scores[1] = 95;` replaces `85` with `95`.

3. The updated array is `{80, 95, 90}`.


---


### 5. Understanding Array Length


The `.Length` property tells us how many elements an array contains.


**Example:**

```csharp

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


Console.WriteLine(numbers.Length);

```


**Output:**

```text

5

```


**Explanation:**


The array contains five values, so its length is `5`.


**Important:** Array length and array index are different.


- Length = 5

- First index = 0

- Last index = 4


**Formula:**


Last Index = Array Length - 1


---


### 6. Using Arrays with a For Loop


A `for` loop allows us to access every value inside an array automatically.


**Example:**

```csharp

using System;


class Program

{

static void Main()

{

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


for (int i = 0; i < numbers.Length; i++)

{

Console.WriteLine(numbers[i]);

}

}

}

```


**Output:**

```text

10

20

30

40

50

```


**Understanding the For Loop:**


```csharp

for (int i = 0; i < numbers.Length; i++)

```


- `int i = 0` – Starts counting at index 0.

- `i < numbers.Length` – Continues while `i` is less than the array length.

- `i++` – Increases `i` by 1 after every loop.

- `numbers[i]` – Accesses the value at the current index.


**How the Loop Works:**


| Loop | Index (i) | Value Printed |

|---|---|---|

| 1 | 0 | 10 |

| 2 | 1 | 20 |

| 3 | 2 | 30 |

| 4 | 3 | 40 |

| 5 | 4 | 50 |


When `i` becomes 5, the condition `5 < 5` is false, so the loop stops.


---


### 7. Getting User Input with Arrays


We can use arrays to store values entered by the user.


**Example: Student Grade Recorder**

```csharp

using System;


class Program

{

static void Main()

{

int[] grades = new int[3];


for (int i = 0; i < grades.Length; i++)

{

Console.Write("Enter grade: ");

grades[i] = int.Parse(Console.ReadLine());

}


Console.WriteLine("Your grades:");


for (int i = 0; i < grades.Length; i++)

{

Console.WriteLine(grades[i]);

}

}

}

```


**Explanation:**


**Step 1: Create the Array**

```csharp

int[] grades = new int[3];

```

Creates an array that can store three grades.


**Step 2: Get User Input**

```csharp

grades[i] = int.Parse(Console.ReadLine());

```

- `Console.ReadLine()` – Reads user input.

- `int.Parse()` – Converts the input into an integer.

- `grades[i]` – Stores the number at the current index.


**Step 3: Display the Grades**

```csharp

Console.WriteLine(grades[i]);

```

Prints each stored grade.


**Note:** `int.Parse()` causes an error if the user enters text that cannot be converted into a whole number.


---


### 8. Using a Foreach Loop


A `foreach` loop is another way to read all the values inside an array.


**Example:**

```csharp

string[] names = {"John", "Anna", "Mark"};


foreach (string name in names)

{

Console.WriteLine(name);

}

```


**Output:**

```text

John

Anna

Mark

```


**Explanation:**


- `foreach` – Repeats for every element.

- `string name` – Holds the current value.

- `in names` – Specifies the array being read.


**For Loop vs. Foreach Loop**


| For Loop | Foreach Loop |

|---|---|

| Uses indexes | Uses values directly |

| Can change elements using indexes | Mainly used for reading values |

| More control | Easier to write |


---


### 9. Common Array Mistakes


**Mistake 1: Wrong Index**

```csharp

int[] numbers = {10, 20, 30};


Console.WriteLine(numbers[3]); // Error

```

The valid indexes are only 0, 1, and 2.


**Mistake 2: Mixing Data Types**

```csharp

int[] numbers = {10, "Hello", 30}; // Error

```

An integer array cannot store strings.


**Mistake 3: Incorrect Loop Condition**

```csharp

for (int i = 0; i <= numbers.Length; i++)

```

This attempts to access an index outside the array.


**Correct:**

```csharp

for (int i = 0; i < numbers.Length; i++)

```


---


### 10. Quick Summary


| Term | Meaning |

|---|---|

| Array | Stores multiple values |

| `[]` | Array symbol |

| Index | Position of an element |

| `array[0]` | First element |

| `.Length` | Total number of elements |

| `for` | Loop using an index |

| `foreach` | Loop through values |

| `new int[5]` | Creates an integer array with 5 elements |


### Important Things to Remember


1. Arrays store multiple values in one variable.

2. Array indexes start at `0`.

3. Arrays have a fixed length after creation.

4. `.Length` returns the number of elements.

5. `for` loops are useful for accessing and modifying array elements.

6. `foreach` loops are useful for reading array values.

7. The last valid index is always `Length - 1`.


---


### 11. Practice Activity


**Activity: Store Five Student Grades**


**Instructions:**


Create a C# program that:

1. Declares an integer array with five elements.

2. Asks the user to enter five grades.

3. Stores each grade inside the array.

4. Displays all five grades using a loop.


**Starter Code:**

```csharp

using System;


class Program

{

static void Main()

{

int[] grades = new int[___];


for (int i = 0; i < grades.Length; i++)

{

Console.Write("

Enter grade: ");