Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 5299212

Browse files
Merge branch 'main' into poll
2 parents 0bb6c71 + 9b1798b commit 5299212

38 files changed

Lines changed: 3348 additions & 213 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
```
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
Title: 'begin()'
3+
Description: 'Returns an iterator pointing to the first element of an array container'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Arrays'
9+
- 'Functions'
10+
- 'Iterators'
11+
- 'STL'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`begin()`** function is a member function used with C++ array containers and the standard `std::begin()` function that returns an iterator pointing to the first element of an array or array-like container. It provides a standardized way to access the beginning of containers, enabling iteration through elements and compatibility with range-based `for` [loops](https://www.codecademy.com/resources/docs/cpp/loops) and STL algorithms.
18+
19+
## Syntax
20+
21+
For `std::array` containers:
22+
23+
```pseudo
24+
arrayname.begin()
25+
```
26+
27+
For regular arrays using `std::begin()`:
28+
29+
```pseudo
30+
std::begin(arrayname)
31+
```
32+
33+
**Parameters:**
34+
35+
No parameters are required.
36+
37+
**Returns:**
38+
39+
Returns an iterator (random-access for `std::array`) pointing to the first element.
40+
41+
## Example 1: Basic Iterator Usage with `begin()`
42+
43+
This example demonstrates the basic usage of `begin()` with `std::array` to iterate through elements:
44+
45+
```cpp
46+
#include <iostream>
47+
#include <array>
48+
49+
int main() {
50+
// Create an array with 5 elements
51+
std::array<int, 5> numbers = {10, 20, 30, 40, 50};
52+
53+
// Use begin() to get iterator to first element
54+
auto it = numbers.begin();
55+
56+
std::cout << "First element: " << *it << std::endl;
57+
58+
// Iterate through all elements using begin() and end()
59+
for (auto iter = numbers.begin(); iter != numbers.end(); ++iter) {
60+
std::cout << *iter << " ";
61+
}
62+
std::cout << std::endl;
63+
64+
return 0;
65+
}
66+
```
67+
68+
The output of this code is:
69+
70+
```shell
71+
First element: 10
72+
10 20 30 40 50
73+
```
74+
75+
## Example 2: Range-Based `for` Loop With `begin()`
76+
77+
This example shows how `begin()` works behind the scenes with range-based `for` loops in real-world scenarios like processing student grades:
78+
79+
```cpp
80+
#include <iostream>
81+
#include <array>
82+
83+
int main() {
84+
// Student test scores
85+
std::array<int, 6> scores = {85, 92, 78, 96, 88, 91};
86+
87+
// Calculate average using range-based for loop
88+
// (which internally uses begin() and end())
89+
int total = 0;
90+
for (const auto& score : scores) {
91+
total += score;
92+
}
93+
94+
double average = static_cast<double>(total) / scores.size();
95+
96+
std::cout << "Student Scores: ";
97+
for (const auto& score : scores) {
98+
std::cout << score << " ";
99+
}
100+
std::cout << std::endl;
101+
std::cout << "Average Score: " << average << std::endl;
102+
103+
return 0;
104+
}
105+
```
106+
107+
The output of this code is:
108+
109+
```shell
110+
Student Scores: 85 92 78 96 88 91
111+
Average Score: 88.33
112+
```
113+
114+
## Codebyte Example: Using `std::begin()` with Regular Arrays
115+
116+
This example demonstrates using `std::begin()` with traditional C-style arrays for compatibility with STL algorithms:
117+
118+
```codebyte/cpp
119+
#include <iostream>
120+
#include <algorithm>
121+
122+
int main() {
123+
// Regular C-style array
124+
int temperatures[] = {72, 75, 68, 80, 77, 73, 69};
125+
int size = sizeof(temperatures) / sizeof(temperatures[0]);
126+
127+
// Use std::begin() and std::end() for compatibility
128+
std::cout << "Daily temperatures: ";
129+
for (auto it = std::begin(temperatures); it != std::end(temperatures); ++it) {
130+
std::cout << *it << "F ";
131+
}
132+
std::cout << std::endl;
133+
134+
// Find maximum temperature using STL algorithm
135+
auto max_temp = std::max_element(std::begin(temperatures), std::end(temperatures));
136+
std::cout << "Highest temperature: " << *max_temp << "F" << std::endl;
137+
138+
// Sort temperatures
139+
std::sort(std::begin(temperatures), std::end(temperatures));
140+
std::cout << "Sorted temperatures: ";
141+
for (const auto& temp : temperatures) {
142+
std::cout << temp << "F ";
143+
}
144+
std::cout << std::endl;
145+
146+
return 0;
147+
}
148+
```
149+
150+
## Frequently Asked Questions
151+
152+
### 1. What is the difference between `std::array` `data()` and `begin()`?
153+
154+
`data()` returns a pointer to the underlying array data, providing direct access to the raw array elements. `begin()` returns an iterator to the first element, which is designed for safe iteration and compatibility with STL algorithms.
155+
156+
### 2. What does arr `begin()` return?
157+
158+
For `std::array`, `begin()` returns a random-access iterator, not just a bidirectional iterator. Random-access iterators are more powerful and support additional operations.
159+
160+
### 3. What does `begin()` do in a C++ array?
161+
162+
`begin()` provides a standardized way to obtain an iterator to the first element of an array container. It enables iteration through elements, compatibility with range-based `for` loops, and integration with STL algorithms for operations like sorting, searching, and transforming data.

0 commit comments

Comments
 (0)