計算機科学のブログ

Interfaces, casting, and is: Making classes keep their promises - cast, is keyword

Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 7(Interfaces, casting, and is: Making classes keep their promises)、p.381(Sharpen your pencil)の解答を求めてみる。

コード

Program.cs

using System;

namespace ConsoleApp3;

public interface IWorker
{
    public string Job { get; }
    void WorkTheNextShift();
}

入出力結果(Terminal, Zsh)

% dotnet run
/.../Program.cs(7,11): warning CS8601: Possible null reference assignment. [/Users/.../ConsoleApp3.csproj]
1. 0, 2, 5
0 True
1 False
2 True
3 False
4 False
5 True
6 False
7 False
2. 0, 1, 2, 3, 5
0 True
1 True
2 True
3 True
4 False
5 True
6 False
7 False
3. 0, 1, 2, 3, 5
0 True
1 True
2 True
3 True
4 False
5 True
6 False
7 False
%