Chapter roadmap
Decompose behavior into methods and work effectively with arrays, lists, dictionaries, sets, and collection transformations. Work through the concepts in order, type every example yourself, and keep a short debugging log that records predictions, errors, fixes, and what each fix taught you.
Learning objectives
- Design method contracts
- Pass values and references
- Choose collection types
- Traverse and transform collections
03.1
Methods and parameters
A method signature includes accessibility, modifiers, return type, name, and parameter types. Parameters are passed by value by default, including object references copied by value. ref, out, and in make reference-passing intent explicit and should be used only when a returned object or tuple is not clearer.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplestatic double Average(double first, double second, double third)
{
return (first + second + third) / 3.0;
}
Console.WriteLine(Average(80, 90, 100));Result90
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
03.2
Optional, named, and tuple returns
Optional parameters provide compile-time defaults and named arguments clarify calls with several similar values. Tuples can return a small related group without defining a class, and named tuple elements document meaning. Larger domain results deserve a record or class.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplestatic (int Min, int Max) Bounds(int[] values)
{
return (values.Min(), values.Max());
}
var bounds = Bounds(new[] { 8, 2, 11, 4 });
Console.WriteLine($"{bounds.Min} to {bounds.Max}");Result2 to 11
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
03.3
Arrays, List<T>, and HashSet<T>
Arrays have fixed length. List<T> is an ordered dynamic collection, and HashSet<T> stores unique items with efficient membership checks. Program to the collection behavior the problem needs rather than choosing List<T> automatically.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplevar tags = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"CSharp", "Study", "csharp"
};
Console.WriteLine(tags.Count);Result2
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
03.4
Dictionary<TKey,TValue>
A dictionary maps unique keys to values. TryGetValue performs a lookup without throwing for a missing key. Iteration yields KeyValuePair values, and a suitable equality comparer controls how keys such as strings are compared.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplevar inventory = new Dictionary<string, int>
{
["notebook"] = 12,
["marker"] = 8
};
if (inventory.TryGetValue("marker", out int count))
{
Console.WriteLine(count);
}Result8
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
Chapter assignments
Complete these in order. Later assignments assume that earlier skills are working. Do not only test the sample values; design tests that challenge boundaries, missing values, incorrect types, empty collections, and other likely failures.
- A3.1
Create focused methods for conversions, validation, statistics, and formatting, then combine them in one console application.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A3.2
Implement list operations for add, remove, update, sort, search, and duplicate removal.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A3.3
Build a word-frequency dictionary that normalizes case and punctuation and reports the most common words.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A3.4
Compare an array, List<T>, HashSet<T>, and Dictionary<TKey,TValue> for four scenarios and justify each choice.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
Chapter project
Course Gradebook
Course Gradebook โ Store students and score collections, support updates and searches, calculate statistics in separate methods, and produce sorted summaries. Use dictionaries for keyed lookup and return records or tuples for multi-value statistics.
Required process
- Write a short specification listing inputs, outputs, rules, and failure cases.
- Break the work into small functions, queries, modules, or classes appropriate to the language.
- Build the smallest working version before adding optional features.
- Test normal, boundary, empty, and invalid cases and record the results.
- Refactor names and duplication, then write a concise user guide.
Key terms
- method
- signature
- parameter
- ref
- out
- tuple
- generic collection
- List
- HashSet
- Dictionary