Arrays, Collections, Strings, and Nullability
Work with sequences of data using arrays, lists, dictionaries, strings, and null-safe coding habits.
Inside this chapter
- Arrays and Lists
- Dictionaries and Sets
- Strings and String Handling
- Nullability
- foreach and Safe Traversal
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C# syntax and OOP to modern .NET web development, data access, async programming, architecture, and advanced engineering practice. Use the navigation at the bottom to move smoothly through the full series.
Arrays and Lists
int[] numbers = { 1, 2, 3, 4 };
List<string> names = new() { "Alice", "Bob", "Chen" };
Arrays are fixed-size, while lists are dynamic and usually more convenient in application code. Choosing the right collection matters for readability and performance.
Dictionaries and Sets
Dictionary<string, int> ages = new()
{
["Alice"] = 25,
["Bob"] = 30
};
Dictionaries are useful for key-value lookup. Sets track uniqueness. The .NET collection ecosystem is rich, and students should learn its common types well.
Strings and String Handling
string fullName = "Alice Johnson";
Console.WriteLine(fullName.ToUpper());
Console.WriteLine(fullName.Replace("Johnson", "Smith"));
Strings are immutable in C#. For many string-building scenarios, StringBuilder can be more efficient than repeated concatenation in loops.
Nullability
string? optionalName = Console.ReadLine();
if (optionalName is not null)
{
Console.WriteLine(optionalName.Length);
}
Nullable reference types help developers reason about possible null values more explicitly. This feature improves safety when used consistently.
foreach and Safe Traversal
foreach (var name in names)
{
Console.WriteLine(name);
} Real-World Usage Snapshot
Collections and strings are used constantly in APIs, enterprise apps, data transformation layers, and business workflows. Understanding them well avoids many beginner performance and null-reference mistakes.