does not implement interface member の原因と直し方
インターフェースを実装すると宣言したクラスが、そのメンバーの一部を実装していないときに出るコンパイルエラーです。
エラーメッセージの読み方
Program.cs(4,16): error CS0535: 'Person' does not implement interface member 'IGreeter.Greet()'
Program.cs- ファイル名
4- 行番号
16- 列番号 — この位置でコンパイラが解析に失敗しました
CS0535- エラーコード — 検索するとMicrosoftの解説ページが見つかります
'Person' does not implement interface member 'IGreeter.Greet()'- 内容 — 期待していたもの、または受け付けられなかったもの
このエラーが出る典型パターン
パターン1
1 interface IGreeter { 2 void Greet(); 3 } 4 class Person : IGreeter { 5 ^ 6 } 7 class Program { 8 static void Main(string[] args) { 9 Person p = new Person(); 10 p.Greet(); 11 } 12 }
Program.cs(4,16): error CS0535: 'Person' does not implement interface member 'IGreeter.Greet()'
インターフェースを実装すると宣言したクラスは、そのメンバーを全て実装する義務があります。1つでも欠けるとコンパイルエラーです。
直し方: (空) を public void Greet() { System.Console.WriteLine("Hi"); } にします。
広告
広告スロット(未設定)
パターン2
1 interface IShape { 2 double Area(); 3 } 4 class Square : IShape { 5 public double Side; 6 ^ 7 } 8 class Program { 9 static void Main(string[] args) { 10 Square s = new Square { Side = 2 }; 11 System.Console.WriteLine(s.Area()); 12 } 13 }
Program.cs(4,16): error CS0535: 'Square' does not implement interface member 'IShape.Area()'
戻り値の型やアクセス修飾子もインターフェースの定義と一致させる必要があります。
直し方: (空) を public double Area() { return Side * Side; } にします。
パターン3
1 interface IDescribable { 2 string Describe(); 3 } 4 class Book : IDescribable { 5 public string Title; 6 ^ 7 } 8 class Program { 9 static void Main(string[] args) { 10 Book b = new Book { Title = "C#" }; 11 System.Console.WriteLine(b.Describe()); 12 } 13 }
Program.cs(4,14): error CS0535: 'Book' does not implement interface member 'IDescribable.Describe()'
publicを付け忘れるとアクセスレベルの不一致で同じCS0535になります。
直し方: (空) を public string Describe() { return Title; } にします。
よくある誤解
シグネチャ(戻り値の型・引数・アクセス修飾子)が1つでもインターフェースの定義と食い違うと、実装していないものとして扱われます。
まとめ
does not implement interface memberは中級でつまずきやすい項目です。上の3パターンを実際に手で直すと、エラーメッセージのどこを読めばよいかが掴めます。