Code Fix

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

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

明示的インターフェース実装(void IWorker.Work()のような書き方)で定義したメンバーは、クラス型の変数からは見えません。

 1  interface ILogger { void Log(); }
 2  class Service : ILogger {
 3      void ILogger.Log() { System.Console.WriteLine("Logging"); }
 4  }
 5  class Program {
 6      static void Main(string[] args) {
 7          _______ s = new Service();
               ^
 8          s.Log();
 9      }
10  }
Program.cs(8,11): error CS1061: 'Service' does not contain a definition for 'Log' and no accessible extension method 'Log' accepting a first argument of type 'Service' could be found (are you missing a using directive or an assembly reference?)

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

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

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

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

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

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