Generics
List Type Safety
List<T> stores a collection where every item has the same type.
type safety
Type safety catches mismatched values before a program runs.
List Type Safety
ListTypeSafety.cs
Replay: real traced execution (multi-file project)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int extra = 4;
List<int> scores = new List<int>();
scores.Add(10);
scores.Add(extra);
int total = 0;
foreach (int score in scores)
{
total += score;
}
Console.WriteLine($"extra={extra}");
Console.WriteLine($"count={scores.Count}");
Console.WriteLine($"total={total}");
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int extra = 1;
List<int> scores = new List<int>();
scores.Add(10);
scores.Add(extra);
int total = 0;
foreach (int score in scores)
{
total += score;
}
Console.WriteLine($"extra={extra}");
Console.WriteLine($"count={scores.Count}");
Console.WriteLine($"total={total}");
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int extra = 8;
List<int> scores = new List<int>();
scores.Add(10);
scores.Add(extra);
int total = 0;
foreach (int score in scores)
{
total += score;
}
Console.WriteLine($"extra={extra}");
Console.WriteLine($"count={scores.Count}");
Console.WriteLine($"total={total}");
}
}
extra ← 4, total ← 0
5{6 static void Main()7 {8 int extra→ 4 = 4; //@extra=1, 89 List<int> scores = new List<int>();10 scores.Add(10);11 scores.Add(extra4);1213 int total→ 0 = 0;14 foreach (int score in scores)total ← 10
pass 1 of 213int total = 0;14foreach (int score10 in scores)15{16 total→ 10 += score10;17}total ← 14
pass 2 of 213int total = 0;14foreach (int score4 in scores)15{16 total→ 14 += score4;17}Console.WriteLine($"extra={extra}");
19 Console.WriteLine($"extra={extra4}");20 Console.WriteLine($"count={scores.Count2}");21 Console.WriteLine($"total={total14}");22}outputextra=4 count=2 total=14
extra ← 1, total ← 0
5{6 static void Main()7 {8 int extra→ 1 = 1;9 List<int> scores = new List<int>();10 scores.Add(10);11 scores.Add(extra1);1213 int total→ 0 = 0;14 foreach (int score in scores)total ← 10
pass 1 of 213int total = 0;14foreach (int score10 in scores)15{16 total→ 10 += score10;17}total ← 11
pass 2 of 213int total = 0;14foreach (int score1 in scores)15{16 total→ 11 += score1;17}Console.WriteLine($"extra={extra}");
19 Console.WriteLine($"extra={extra1}");20 Console.WriteLine($"count={scores.Count2}");21 Console.WriteLine($"total={total11}");22}outputextra=1 count=2 total=11
extra ← 8, total ← 0
5{6 static void Main()7 {8 int extra→ 8 = 8;9 List<int> scores = new List<int>();10 scores.Add(10);11 scores.Add(extra8);1213 int total→ 0 = 0;14 foreach (int score in scores)total ← 10
pass 1 of 213int total = 0;14foreach (int score10 in scores)15{16 total→ 10 += score10;17}total ← 18
pass 2 of 213int total = 0;14foreach (int score8 in scores)15{16 total→ 18 += score8;17}Console.WriteLine($"extra={extra}");
19 Console.WriteLine($"extra={extra8}");20 Console.WriteLine($"count={scores.Count2}");21 Console.WriteLine($"total={total18}");22}outputextra=8 count=2 total=18