This appendix provides answers to the exercises as found in the book as well as explanations of the answers.
Chapter 2: .NET and how it compiles
Exercise number
Answer
Explanations
2.1
d
AmigaOS is an operating system originally developed for the Amiga PC. Its last major release was in 2016. It is not supported by .NET 5.
2.2
c
2.3
d
2.4
a
2.5
b, a
2.6
e
2.7
c
2.8
a
Chapter 4: Manage your unmanaged resources!
Exercise number
Answer
Explanations
4.1
False
Attributes can be applied to methods, classes, types, properties, and fields.
4.2
True
4.3
False
Enumerations are created with the enum keyword.
4.4
a, d
Only a Hufflepuff would write connection strings on a sticky note.
4.5
b
4.6
a
4.7
c
4.8
True
4.9
False
Chapter 5: Setting up a project and database with Entity Framework Core
Exercise number
Answer
Explanations
5.1
b
5.2
a
5.3
a, d
Tomcat is an open source implementation of Java Servlet (similar to WebHost); JVM is the Java Virtual Machine (similar to the CLR).
5.4
True
5.5
See below the table for answer
5.6
One per database entity
Answer to exercise 5.5: There are a variety of ways to solve this exercise as a one-liner. You can pick from a variety of access modifiers, method names, and variable names. Two constants remain, however: the return type needs to be of type integer, and we need to return the product of the two integer input arguments as follows:
public int Product(int a, int b) => a * b;
Chapter 6: Test-driven development and dependency injection
Exercise number
Answer
Explanations
6.1
c
Answer B (“Don’t perform the same logic in two separate places”) describes the Don’t Repeat Yourself (DRY) principle.
6.2
True
In the book, however, we use TDD-lite where we sometimes break this rule.
6.3
False
Test classes must have an access modifier of public to be used by a test runner.
6.4
c
6.5
False
LINQ allows us to perform queries on collections by using SQL-like statements and methods.
6.6
a
6.7
b
6.8
True
6.9
a
6.10
True
Solution to the exercise found in section 6.2.8:
[TestMethod]
public async Task CreateCustomer_Success()
{
CustomerRepository repository = new CustomerRepository();
Assert.IsNotNull(repository);
bool result = await repository.CreateCustomer("Donald Knuth");
Assert.IsTrue(result);
}
[TestMethod]
public async Task CreateCustomer_Failure_NameIsNull()
{
CustomerRepository repository = new CustomerRepository();
Assert.IsNotNull(repository);
bool result = await repository.CreateCustomer(null);
Assert.IsFalse(result);
}
[TestMethod]
public async Task CreateCustomer_Failure_NameIsEmptyString()
{
CustomerRepository repository = new CustomerRepository();
Assert.IsNotNull(repository);
bool result = await repository.CreateCustomer(string.Empty);
Assert.IsFalse(result);
}
[TestMethod]
[DataRow('#')]
[DataRow('$')]
[DataRow('%')]
[DataRow('&')]
[DataRow('*')]
public async Task CreateCustomer_Failure_NameContainsInvalidCharacters(char
➥ invalidCharacter)
{
CustomerRepository repository = new CustomerRepository();
Assert.IsNotNull(repository);
bool result = await repository.CreateCustomer("Donald Knuth" + invalidCharacter);
Assert.IsFalse(result);
}
Chapter 7: Comparing objects
Exercise number
Answer
Explanations
7.1
Write a unit test that uses the exception found method attribute.
7.2
b
7.3
Exception
You can also derive a custom exception from a different Exception (custom or not) that inherits from the Exception class.
7.4
c
A and B can be default values for the underlying type of the collection, so they are correct in some cases.
7.5
c
When comparing value types, the equality operator compares their values against each other.
7.6
a
When comparing reference types, the equality operator compares their memory address against each other.
7.7
True
7.8
a
When overloading an operator, you also need to overload its counter operator.
7.9
False
Perfect randomness does not exist in computing.
7.10
False
Perfect randomness does not exist in computing.
7.11
True
Chapter 8: Stubbing, generics, and coupling
Exercise number
Answer
Explanations
8.1
c
8.2
False
Two classes that are heavily dependent on each other signifies tight coupling.
8.3
a
8.4
c
8.5
False
Strings are immutable. Every change that is made to a string results in a new memory allocation, with the resulting string stored into that spot in memory.
8.6
False
You have to override the base class’s methods.
8.7
a
The DRY principle stands for the Don’t Repeat Yourself principle. The Phragmén–Lindelöf principle deals with boundedness of a holomorphic function on an unbounded domain.
8.8
b
8.9
c
8.10
False
Generics can be used with classes, methods, and collections.
8.11
False
8.12
False
8.13
True
8.14
b
8.15
False
If you do not declare a default case in a switch statement, and no other cases are matched, no execution will take place within the switch statement.
Chapter 9: Extension methods, streams, and abstract classes
Exercise number
Answer
Explanations
9.1
b
9.2
a
9.3
False
You can use the [DataRow] method attribute with as many data points as you want.
9.4
b
9.5
False
9.6
False
An abstract class can contain both abstract and regular methods.
9.7
False
9.8
True
Chapter 10: Reflection and mocks
Exercise number
Answer
Explanations
10.1
b
10.2
False
The repository layer typically interacts with a database through a database access layer when using an ORM.
10.3
True
10.4
True
10.5
a
10.6
c
We never want to delete code without worrying about the side effects. When encountering commented-out code, do your due diligence to figure out why it is there. There better be a really good excuse or else delete it. Ninety-nine percent of the time, you can delete the code without issue.
10.7
d
10.8
a
10.9
True
10.10
c
10.11
True
While there is still some coupling between the controller and the repository, this is looser coupling when compared to the controller directly calling the repository.
10.12
False
This is the functionality of a stub.
10.13
False
The InternalsVisibleTo allows you to expose an assembly’s internals to a different assembly.
10.14
c
10.15
True
10.16
b
10.17
a
[MethodImpl(MethodImplOptions.NoInlining)] can be applied only to methods.
Chapter 11: Runtime type checking revisited and error handling
Exercise number
Answer
Explanations
11.1
False
11.2
c
11.3
a
Only a service is allowed to call a repository. A repository should not call another repository.
11.4
True
11.5
b
11.6
False
The discard operator can still result in memory allocation.
11.7
a
The first catch block is entered because an ItemSoldOutException can be used as an Exception type.
11.8
a
Chapter 12: Using IAsyncEnumerable<T> and yield return
Exercise number
Answer
Explanations
12.1
True
12.2
False
We would likely need to implement the InventoryService, though.
12.3
b
12.4
a
12.5
Yes
The Dragonfruit class can set the IsFruit property because the IsFruit property has an access modifier of protected. With a protected access modifier, the owning class and its children (derived classes) can access the property. The Dragonfruit class derives from the Fruit class.
12.6
True
12.7
True
12.8
False
When you add a constructor to a struct, you need to set all properties present on the struct or the compiler will not compile your code.
Chapter 13: Middleware, HTTP routing, and HTTP responses