Chapter 4: Writing, Debugging, and Testing Functions [Flashcarder]
(177 cards)
What is a fundamental principle of programming mentioned in the book?
Don’t Repeat Yourself (DRY).
What should you do if you find yourself writing the same statements over and over again while programming?
Turn those statements into a function.
How are functions described in the book?
Functions are like tiny programs that complete one small task, usually having inputs and outputs.
What feature was introduced with C# 9 and is used by the default project template for console apps since C# 10 and .NET 6?
The top-level program feature.
How do functions work with the automatically generated Program class and its <Main>$ method in top-level programs?
Functions defined in top-level programs become local functions within the <Main>$ method of the Program class.
What is the suggested practice for the placement of functions in a file?
Functions should be placed at the bottom of the file rather than mixed with other top-level statements.
What compiler error occurs if types like classes are declared in the middle of the Program.cs file?
Compiler error CS8803.
What does the compiler automatically generate for a local function in C#?
The compiler generates an inner method within the existing method or function, making it local to that context.
If the local function is within the Main
method of the Program
class, it remains nested inside that method, without creating a new class or method.
Where must import statements (using) be placed in the Program.cs file?
Import statements must go at the top of the Program.cs file.
Where can statements that go into the <Main>$ function be placed in the Program.cs file?
Statements that will go in the <Main>$ function can be mixed with functions in the middle of the Program.cs file. Any functions will become local functions in the <Main>$ method.
Why can’t local functions have XML comments?
Local functions cannot have XML comments because they cannot be used outside the member in which they are declared.
What is a better approach than defining functions within the Program.cs file?
A better approach is to write any functions in a separate file and define them as static members of the Program class.
What should you name a new class file for defining functions as static members of the Program class?
The name of the file does not actually matter, but using a naming convention like Program.Functions.cs is sensible.
What happens when you define a partial Program class with static functions in a separate file?
The compiler defines a Program class with a <Main>$ function and merges your function as a member of the Program class.
Why should you not define a namespace for your partial Program class?
If you define a namespace for your partial Program class, it will be in a different namespace and will not merge with the auto-generated partial Program class.
What is the strict definition of a parameter in a function?
A parameter is a variable in a function definition.
Provide an example of a function with a parameter.
void Hire(DateTime startDate) { // Function implementation. }
What is the strict definition of an argument in a function call?
An argument is the data you pass into the method’s parameters when a method is called.
Provide an example of passing an argument to a function.
DateTime when = new(year: 2024, month: 11, day: 5); Hire(when);
How can you specify the parameter name when passing an argument?
You can specify the parameter name by using named arguments.
For example, MyMethod(paramName: value);
DateTime when = new(year: 2024, month: 11, day: 5); Hire(startDate: when);
What terms do the official Microsoft documentation use interchangeably?
Named and optional arguments and
named and optional parameters.
How can a single object act as both a parameter and an argument?
Within a function implementation, a parameter can be passed as an argument to another function.
Provide an example where a parameter is passed as an argument to another function.
void Hire(DateTime startDate) { ... SaveToDatabase(startDate, employeeRecord); ... }
What is a classic example of naming a parameter in C#?
The parameter named args in the Main function.
static void Main(String[] args) { ... }