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