上級 › does not contain a definition の原因と直し方(明示的インターフェース実装) › パターン2
does not contain a definition の原因と直し方(明示的インターフェース実装)
明示的インターフェース実装(void IWorker.Work()のような書き方)で定義したメンバーは、クラス型の変数からは見えません。
1 interface IFlyer { void Fly(); } 2 class Bird : IFlyer { 3 void IFlyer.Fly() { System.Console.WriteLine("Flying"); } 4 } 5 class Program { 6 static void Main(string[] args) { 7 ______ b = new Bird(); ^ 8 b.Fly(); 9 } 10 }
Program.cs(8,11): error CS1061: 'Bird' does not contain a definition for 'Fly' and no accessible extension method 'Fly' accepting a first argument of type 'Bird' could be found (are you missing a using directive or an assembly reference?)
空欄を埋めてビルドを通す
BirdクラスにFlyメソッドが見えていても、それは明示的実装なのでBird型の変数からは呼び出せません。
次の問題