Curriculum
Arrays and Collections are essential programming concepts used to store and manage multiple values efficiently within software applications. Arrays and Collections help developers organize data, process large datasets, and build scalable applications. Every .NET developer must understand Arrays and Collections because they are widely used in C#, ASP.NET Core, Entity Framework, MVC applications, Web APIs, and enterprise software development.
In real-world applications, developers often need to store multiple records such as student information, product details, customer data, employee records, and transaction histories. Arrays and Collections provide an efficient way to handle such data.
Arrays and Collections are data structures used to store multiple values in a single variable.
Instead of creating separate variables for each value, Arrays and Collections allow developers to manage related data together.
Example without an Array:
string student1 = "Rahul";
string student2 = "Amit";
string student3 = "Neha";
string student4 = "Priya";
Example using an Array:
string[] students = {"Rahul", "Amit", "Neha", "Priya"};
The second approach is cleaner, more scalable, and easier to maintain.
Arrays and Collections help developers:
Most modern software applications depend heavily on Arrays and Collections.
An Array is a collection of elements of the same data type stored in contiguous memory locations.
Each element in an array is identified by an index.
Example:
int[] numbers = {10, 20, 30, 40, 50};
Array elements can be accessed using index numbers.
Example:
Console.WriteLine(numbers[0]);
Output:
10
The first element always starts at index 0.
Arrays have the following characteristics:
These characteristics make arrays suitable for many programming scenarios.
Syntax:
dataType[] arrayName;
Example:
int[] marks;
This declaration creates an integer array reference.
Example:
int[] marks = {80, 85, 90, 95};
The array contains four elements.
Example:
string[] cities = {"Jaipur", "Delhi", "Mumbai"};
Console.WriteLine(cities[1]);
Output:
Delhi
Index positions:
| Index | Value |
|---|---|
| 0 | Jaipur |
| 1 | Delhi |
| 2 | Mumbai |
Example:
string[] cities = {"Jaipur", "Delhi", "Mumbai"};
cities[1] = "Pune";
Updated Array:
Jaipur
Pune
Mumbai
Array values can be changed after creation.
The Length property returns the total number of elements.
Example:
int[] numbers = {10, 20, 30, 40};
Console.WriteLine(numbers.Length);
Output:
4
Arrays are often used with loops.
Example:
int[] marks = {80, 85, 90, 95};
for(int i = 0; i < marks.Length; i++)
{
Console.WriteLine(marks[i]);
}
Output:
80
85
90
95
The foreach loop provides a simpler way to iterate through arrays.
Example:
string[] courses = {"C#", ".NET", "SQL"};
foreach(string course in courses)
{
Console.WriteLine(course);
}
Output:
C#
.NET
SQL
The foreach loop is commonly used in modern C# development.
Although arrays are useful, they have some limitations:
To overcome these limitations, Collections are used.
Collections are advanced data structures that store and manage groups of objects dynamically.
Unlike arrays, Collections can grow and shrink as needed.
Collections are part of the .NET Framework and provide powerful features for data management.
Collections provide:
Collections are preferred in most modern applications.
A List stores multiple values dynamically.
Example:
List<string> students = new List<string>();
students.Add("Rahul");
students.Add("Amit");
students.Add("Neha");
Display values:
foreach(string student in students)
{
Console.WriteLine(student);
}
Output:
Rahul
Amit
Neha
List is one of the most commonly used collections in C#.
A Dictionary stores data in key-value pairs.
Example:
Dictionary<int, string> students =
new Dictionary<int, string>();
students.Add(1, "Rahul");
students.Add(2, "Amit");
Access value:
Console.WriteLine(students[1]);
Output:
Rahul
Dictionary collections are widely used in enterprise applications.
A Queue follows the First In First Out (FIFO) principle.
Example:
Queue<string> customers =
new Queue<string>();
customers.Enqueue("Rahul");
customers.Enqueue("Amit");
Remove item:
customers.Dequeue();
The first inserted item is removed first.
A Stack follows the Last In First Out (LIFO) principle.
Example:
Stack<string> books =
new Stack<string>();
books.Push("Book1");
books.Push("Book2");
Remove item:
books.Pop();
The most recently added item is removed first.
| Feature | Array | Collection |
|---|---|---|
| Size | Fixed | Dynamic |
| Performance | Fast | Flexible |
| Memory Usage | Efficient | Slightly Higher |
| Data Management | Basic | Advanced |
| Insert/Delete | Difficult | Easy |
Collections provide more flexibility for modern application development.
Store student records:
List<Student> students = new List<Student>();
Store product information:
List<Product> products = new List<Product>();
Store transaction history:
List<Transaction> transactions =
new List<Transaction>();
Store patient records:
List<Patient> patients =
new List<Patient>();
Arrays and Collections are used in almost every enterprise application.
Arrays and Collections are heavily used in:
Understanding Arrays and Collections is essential for becoming a professional .NET developer.
Arrays and Collections are data structures used to store and manage multiple values.
Arrays have fixed sizes, while Collections can grow and shrink dynamically.
Use Arrays when the number of elements is known and fixed.
Use Collections when the amount of data may change during execution.
A List is a dynamic collection that can store multiple values and automatically adjust its size.
Arrays and Collections are used extensively for data management, database operations, APIs, and enterprise application development.
WhatsApp us