Arrays help us store many items together in one place. This makes it easy to keep and use a list of things like numbers or names.
When you want to keep a list of student scores in a test.
When you need to store daily temperatures for a week.
When you want to save multiple names of friends in one variable.
When you want to process a collection of items one by one.
When you want to pass many values to a method at once.
Syntax
C Sharp (C#)
class Program
{
staticvoid Main()
{
// Declare an array of integersint[] numbers = newint[5];
// Declare and initialize an array
string[] names = { "Alice", "Bob", "Charlie" };
}
}
An array holds items of the same type together.
You must specify the type and size when creating an array.
Examples
This creates an empty array with zero length.
C Sharp (C#)
int[] emptyArray = newint[0];
// An array with no elements
This array holds just one number.
C Sharp (C#)
int[] singleElementArray = { 42 };
// An array with one element
This array holds three fruit names.
C Sharp (C#)
string[] fruits = { "Apple", "Banana", "Cherry" };
// An array with multiple elements
Sample Program
This program shows how to create an array, print its items, change one item, and print again.
C Sharp (C#)
using System;
class Program
{
staticvoid Main()
{
// Create an array to hold 3 colors
string[] colors = { "Red", "Green", "Blue" };
Console.WriteLine("Colors before change:");
foreach (string color in colors)
{
Console.WriteLine(color);
}
// Change the second color
colors[1] = "Yellow";
Console.WriteLine("\nColors after change:");
foreach (string color in colors)
{
Console.WriteLine(color);
}
}
}
OutputSuccess
Important Notes
Accessing an array element by index is very fast (constant time).
Arrays use a fixed amount of memory based on their size.
Common mistake: Trying to access an index outside the array size causes an error.
Use arrays when you know the number of items in advance and want fast access.
Summary
Arrays store many items of the same type together.
They make it easy to keep and use lists of data.
Arrays have a fixed size and fast access by position.
Practice
(1/5)
1. Why do we need arrays in C# programming?
easy
A. To avoid using variables
B. To store only one item at a time
C. To store multiple items of the same type together
D. To make the program run slower
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many items of the same type in one place, making data management easier.
Step 2: Compare options
To store multiple items of the same type together correctly states the main reason for arrays. Other options are incorrect or unrelated.
Final Answer:
To store multiple items of the same type together -> Option C
Quick Check:
Arrays store many items = D [OK]
Hint: Arrays hold many same-type items together [OK]
Common Mistakes:
Thinking arrays store different types together
Believing arrays hold only one item
Confusing arrays with variables
2. Which of the following is the correct way to declare an array of integers in C#?
easy
A. int[] numbers = new int[5];
B. int numbers = new int[5];
C. array numbers = int[5];
D. int numbers[] = new int;
Solution
Step 1: Recall C# array declaration syntax
In C#, arrays are declared with the type followed by square brackets, then the variable name and size.
Step 2: Check each option
int[] numbers = new int[5]; matches correct syntax. Options B, C, and D have syntax errors or wrong order.
Final Answer:
int[] numbers = new int[5]; -> Option A
Quick Check:
Correct array syntax = A [OK]
Hint: Use type[] name = new type[size]; for arrays [OK]