In this guide, we will talk about the differences and benefits of “Arrays” and “Lists” in C#.
Arrays in C#
Arrays store data of the same type within fixed memory. The “System.Array” namespace is used for the array. There are indexes in the array to store data. Because of the unchangeable size of the array, memory wastage is an issue. Arrays are static and continuous in nature and the benefit of using an array is that its execution is fast.
The syntax of the array is:
Let’s see an example of an array:
namespace CSharpArrayExample
{
public class Names
{
static void Main(string[] args)
{
string[] names = { "Ali", "Bushra", "Farwa", "Fawad"};
Console.WriteLine("The element at index 1 is:" +names[1]);
Console.WriteLine("The elements of array are:" );
for(int index = 0; index < names.Length; index ++)
{
Console.WriteLine(names[index]);
}
Console.ReadKey();
}
}
}
In the above-described example:
- First, add the required namespace libraries named “System” and “SharpArrayExample”.
- Then, declare a class named “Names” in which we want to store and display names.
- Next, declare the string array inside the main method in which we have stored the names.
- After that, first, we print an element at index 1 which shows how we can access the element of an array at a specified index.
- Then, we print the whole array.
The output is as follows:
List in C#
“List” is present in System.Collection.Generic and is of generic type. “lists” are dynamic in nature and allow one to add, remove, insert, delete, or manipulate the elements. In C# whenever an element is added or removed the list is resized automatically.
The syntax for the list in C# is stated below:
Let’s see the following example for a better understanding:
using System.Collections.Generic;
namespace Names
{
public class Names
{
static void Main(string[] args)
{
List<string> names = new List<string>();
names.Add("Ali");
names.Add("Bushra");
names.Add("Farwa");
names.Add("Fawad");
Console.WriteLine("The element at index 1 is: "+names[1]);
Console.WriteLine("The elements of this list are: ");
for(int index = 0; index < names.Count; index ++)
{
Console.WriteLine(names[index]);
}
Console.ReadKey();
}
}
}
The working of this program is the same as the array example. However, we just store data using a list.
Output
Differences Between “Array” and “List” in C#
Now, let’s have a quick look at the differences between array and list in C#:
Property | Array | List |
Nature | Static | Dynamic |
Memory | Fixed memory, so memory wastage can occur. | No memory wastage occurs |
Execution | Fast | Slow |
Accessing individual element | Fast | Slow |
Add and remove element | Slow | Fast |
Benefits of Array in C#
The benefits of arrays are:
- Arrays can be easily optimized.
- Users can save several elements of a similar data type.
- Execution is fast.
- We can access the element at a particular index.
- It allows searching for elements.
- Better performance.
- Less runtime errors.
Benefits of List in C#
The benefits of using Lists are:
- No fixed memory.
- Can be scaled after declaration.
- Adding and removing elements is fast.
- Allow us to manipulate data.
- We can access elements at a specified index.
We have discussed the benefits and differences between an “array” and a “list” in C#.
Conclusion
Arrays and lists are used to store data. Arrays have fixed types and memory and allow us to search or access elements. Lists are of a dynamic type and do not have a fixed memory and allow us to insert, delete or manipulate data. In this guide, we have seen the differences and advantages of the “array” and a “list” in C#.