Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Arrays vs. Lists: When to Use Each | Working with Lists and Arrays
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Lists & Collections

bookArrays vs. Lists: When to Use Each

When working with collections of data in C#, you often need to decide between using an array or a list. Both have their strengths and ideal use cases. The following table compares arrays and lists based on mutability, performance, and typical scenarios where each is preferred:

FeatureArrayList
SizeFixed after creationDynamic, can grow or shrink
MutabilityElements mutable, size fixedElements and size mutable
PerformanceSlightly faster for fixed sizeSlightly slower, overhead for resizing
SyntaxSimple, uses []Uses generic syntax List<T>
Use CasesKnown size, performance criticalUnknown or changing size, flexibility
ConversionCan convert to/from lists easilyCan convert to/from arrays easily
Methods AvailableLimited (Length, basic ops)Rich set (Add, Remove, etc.)

Arrays are best when you know the number of elements ahead of time and need maximum performance. Lists are more flexible and provide many helpful methods for managing elements.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void PrintNumbersArray(int[] numbers) { Console.WriteLine("Array contents:"); foreach (int num in numbers) { Console.Write(num + " "); } Console.WriteLine(); } public static void PrintNumbersList(List<int> numbers) { Console.WriteLine("List contents:"); foreach (int num in numbers) { Console.Write(num + " "); } Console.WriteLine(); } public static void Main(string[] args) { int[] numbersArray = { 1, 2, 3, 4, 5 }; List<int> numbersList = new List<int> { 10, 20, 30, 40, 50 }; PrintNumbersArray(numbersArray); PrintNumbersList(numbersList); } } }

To write methods that work with both arrays and lists, you can use interfaces like IEnumerable<T> or ICollection<T>. These interfaces allow your method to accept any collection that implements them, including arrays and lists. For example, a method that accepts IEnumerable<int> can take either an int[] or a List<int>, making your code more flexible and reusable.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Convert List to Array List<string> fruitsList = new List<string> { "apple", "banana", "cherry" }; string[] fruitsArray = fruitsList.ToArray(); // Convert Array to List int[] numbersArray = { 1, 2, 3 }; List<int> numbersList = new List<int>(numbersArray); Console.WriteLine("Fruits Array:"); foreach (string fruit in fruitsArray) { Console.Write(fruit + " "); } Console.WriteLine(); Console.WriteLine("Numbers List:"); foreach (int number in numbersList) { Console.Write(number + " "); } Console.WriteLine(); } } }

1. When is it preferable to use an array over a list?

2. How can you convert a List to an array?

3. Fill in the blanks to write a method that accepts a List<int> as a parameter.

question mark

When is it preferable to use an array over a list?

Select the correct answer

question mark

How can you convert a List to an array?

Select the correct answer

question-icon

Fill in the blanks to write a method that accepts a List<int> as a parameter.

<> numbers) {     foreach (int num in numbers)     {         Console.WriteLine(num);     } }
1
2
3
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you give examples of when to use arrays versus lists in real-world applications?

What are the main performance differences between arrays and lists in C#?

How do I convert between arrays and lists in C#?

bookArrays vs. Lists: When to Use Each

Svep för att visa menyn

When working with collections of data in C#, you often need to decide between using an array or a list. Both have their strengths and ideal use cases. The following table compares arrays and lists based on mutability, performance, and typical scenarios where each is preferred:

FeatureArrayList
SizeFixed after creationDynamic, can grow or shrink
MutabilityElements mutable, size fixedElements and size mutable
PerformanceSlightly faster for fixed sizeSlightly slower, overhead for resizing
SyntaxSimple, uses []Uses generic syntax List<T>
Use CasesKnown size, performance criticalUnknown or changing size, flexibility
ConversionCan convert to/from lists easilyCan convert to/from arrays easily
Methods AvailableLimited (Length, basic ops)Rich set (Add, Remove, etc.)

Arrays are best when you know the number of elements ahead of time and need maximum performance. Lists are more flexible and provide many helpful methods for managing elements.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void PrintNumbersArray(int[] numbers) { Console.WriteLine("Array contents:"); foreach (int num in numbers) { Console.Write(num + " "); } Console.WriteLine(); } public static void PrintNumbersList(List<int> numbers) { Console.WriteLine("List contents:"); foreach (int num in numbers) { Console.Write(num + " "); } Console.WriteLine(); } public static void Main(string[] args) { int[] numbersArray = { 1, 2, 3, 4, 5 }; List<int> numbersList = new List<int> { 10, 20, 30, 40, 50 }; PrintNumbersArray(numbersArray); PrintNumbersList(numbersList); } } }

To write methods that work with both arrays and lists, you can use interfaces like IEnumerable<T> or ICollection<T>. These interfaces allow your method to accept any collection that implements them, including arrays and lists. For example, a method that accepts IEnumerable<int> can take either an int[] or a List<int>, making your code more flexible and reusable.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Convert List to Array List<string> fruitsList = new List<string> { "apple", "banana", "cherry" }; string[] fruitsArray = fruitsList.ToArray(); // Convert Array to List int[] numbersArray = { 1, 2, 3 }; List<int> numbersList = new List<int>(numbersArray); Console.WriteLine("Fruits Array:"); foreach (string fruit in fruitsArray) { Console.Write(fruit + " "); } Console.WriteLine(); Console.WriteLine("Numbers List:"); foreach (int number in numbersList) { Console.Write(number + " "); } Console.WriteLine(); } } }

1. When is it preferable to use an array over a list?

2. How can you convert a List to an array?

3. Fill in the blanks to write a method that accepts a List<int> as a parameter.

question mark

When is it preferable to use an array over a list?

Select the correct answer

question mark

How can you convert a List to an array?

Select the correct answer

question-icon

Fill in the blanks to write a method that accepts a List<int> as a parameter.

<> numbers) {     foreach (int num in numbers)     {         Console.WriteLine(num);     } }
1
2
3
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 6
some-alt