-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
102 lines (84 loc) · 2.37 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
namespace ListsAndSequences;
public class Program
{
static void Main()
{
var numbers = new List<int>();
numbers.Add(123);
numbers.Add(99);
numbers.Add(42);
Console.WriteLine(numbers.Count);
Console.WriteLine($"{numbers[0]}, {numbers[1]}, {numbers[2]}");
numbers[1] += 1;
Console.WriteLine(numbers[1]);
numbers.RemoveAt(1);
Console.WriteLine(numbers.Count);
Console.WriteLine($"{numbers[0]}, {numbers[1]}");
#pragma warning disable CA1859 // Use concrete types when possible for improved performance
IList<int> array = new[] { 1, 2, 3 };
array.Add(4); // Will throw an exception
#pragma warning restore CA1859 // Use concrete types when possible for improved performance
}
public static void ListInitializeWithCollectionExpression()
{
List<int> numbers = [123, 99, 42];
}
public static void ListInitializer()
{
var numbers = new List<int> { 123, 99, 42 };
}
public static void ListInitializerWithTargetTypedNew()
{
List<int> numbers = new() { 123, 99, 42 };
}
}
// The book shows these interfaces for illustration purposes. They are defined
// by the .NET Framework Class Library, so we do not need to write our own
// definitions in practice.
#if false
public interface IEnumerable<out T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator<out T> : IDisposable, IEnumerator
{
T Current { get; }
}
public interface IEnumerator
{
bool MoveNext();
object Current { get; }
void Reset();
}
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator(
CancellationToken cancellationToken = default);
}
public interface IAsyncEnumerator<out T> : IAsyncDisposable
{
T Current { get; }
ValueTask<bool> MoveNextAsync();
}
public interface ICollection<T> : IEnumerable<T>, IEnumerable
{
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
int Count { get; }
bool IsReadOnly { get; }
}
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
T this[int index] { get; set; }
}
#endif