Code Fix

上級 › 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?)

空欄を埋めてビルドを通す

広告
広告スロット(未設定)

does not contain a definition の原因と直し方(明示的インターフェース実装)

インターフェース型の変数からしか呼び出せません。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)