|
| 1 | +--- |
| 2 | +Title: 'Lists' |
| 3 | +Description: 'A List in C# is a dynamic data structure that stores multiple objects of a specified type.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Code Foundations' |
| 7 | + - 'Developer Tools' |
| 8 | +Tags: |
| 9 | + - 'Data Structures' |
| 10 | + - 'Lists' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-c-sharp' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +A **`List`** in [C#](https://www.codecademy.com/resources/docs/c-sharp) is a dynamic [data structure](https://www.codecademy.com/resources/docs/general/data-structures) that stores multiple objects of a specified type. These objects can be accessed by their zero-based index. Unlike [arrays](https://www.codecademy.com/resources/docs/c-sharp/arrays). Lists can grow to accommodate a very large number of elements (up to about 2 billion), limited primarily by available system memory and the maximum value of an integer index." |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +> **Note:** Before creating a list, include the `System.Collections.Generic` namespace. |
| 21 | +
|
| 22 | +There are two common ways to create a `List` in C#: |
| 23 | + |
| 24 | +```pseudo |
| 25 | +// Creating a List without any elements |
| 26 | +List<T> myList = new List<T>(); |
| 27 | +``` |
| 28 | + |
| 29 | +Or alternatively: |
| 30 | + |
| 31 | +```pseudo |
| 32 | +// Creating a List with three elements |
| 33 | +List<T> myList = new List<T> { element1, element2, element3 }; |
| 34 | +``` |
| 35 | + |
| 36 | +**Parameters:** |
| 37 | + |
| 38 | +- `T`: Represents any data type. |
| 39 | +- `element`: Any object or variable of type `T`. |
| 40 | + |
| 41 | +## Example: Creating and Accessing a List |
| 42 | + |
| 43 | +In this example, a list is created, numbers are added, and elements are accessed by their index: |
| 44 | + |
| 45 | +```cs |
| 46 | +using System; |
| 47 | +using System.Collections.Generic; |
| 48 | + |
| 49 | +class Program |
| 50 | +{ |
| 51 | + static void Main() |
| 52 | + { |
| 53 | + // Create a list of integers |
| 54 | + List<int> numbers = new List<int>(); |
| 55 | + |
| 56 | + // Add elements to the list |
| 57 | + numbers.Add(10); |
| 58 | + numbers.Add(20); |
| 59 | + numbers.Add(30); |
| 60 | + |
| 61 | + // Access elements by index |
| 62 | + Console.WriteLine(numbers[0]); |
| 63 | + Console.WriteLine(numbers[1]); |
| 64 | + Console.WriteLine(numbers[2]); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +The output of this code is: |
| 70 | + |
| 71 | +```shell |
| 72 | +10 |
| 73 | +20 |
| 74 | +30 |
| 75 | +``` |
| 76 | + |
| 77 | +## Codebyte Example: Printing elements in a List |
| 78 | + |
| 79 | +This example creates a new `List` that stores three numbers. Since each element has an index, we can print each element in the `numbers` list by its index: |
| 80 | + |
| 81 | +```codebyte/csharp |
| 82 | +using System; |
| 83 | +using System.Collections.Generic; |
| 84 | +
|
| 85 | +public class Test |
| 86 | +{ |
| 87 | + public static void Main() |
| 88 | + { |
| 89 | + List<int> numbers = new List<int> { 2, 5, 10 }; |
| 90 | +
|
| 91 | + Console.WriteLine(numbers[0]); |
| 92 | + Console.WriteLine(numbers[1]); |
| 93 | + Console.WriteLine(numbers[2]); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
0 commit comments