C# 修饰符“static”对于c中的此项/静态接口错误无效

C# 修饰符“static”对于c中的此项/静态接口错误无效,c#,C#,我们如何在接口中实现静态方法 public interface ICache { //Get item from cache static object Get(string pName); //Check an item exist in cache static bool Contains(string pName); //Add an item to cache static void Add(string pName, object pValue);

我们如何在接口中实现静态方法

public interface ICache
{
  //Get item from cache
  static object Get(string pName);

  //Check an item exist in cache
  static bool Contains(string pName); 

  //Add an item to cache
  static void Add(string pName, object pValue);

  //Remove an item from cache
  static void Remove(string pName);
}

上面的界面抛出错误:修饰符“static”对此项无效,并且它绝对正确。不能在接口中指定静态成员。它必须是:

public interface ICache
{
  //Get item from cache
  object Get(string pName);

  //Check an item exist in cache
  bool Contains(string pName);

  //Add an item to cache
  void Add(string pName, object pValue);

  //Remove an item from cache
  void Remove(string pName);
}
顺便说一句,你的评论应该是——这会使它们更有用。我在成员之间添加了一些空格,使代码更易于阅读。您还应该考虑将接口通用化。
首先,为什么要让成员保持静态?你希望实现什么?你做不到。应该是

   public interface ICache
    {
      //Get item from cache
      object Get(string pName);
      //Check an item exist in cache
      bool Contains(string pName);
      //Add an item to cache
      void Add(string pName, object pValue);
      //Remove an item from cache
      void Remove(string pName);
    }
退房

他还写了一个很酷的系列文章叫

对类型参数调用静态方法是非法的,
不,你不能。。。静态方法/变量指的是类本身而不是该类的实例,因为接口的目的是由类实现,所以在接口中不能有静态方法。。。它没有意义

让它成为一个普通的接口,并使用类似于singleton的东西来实现它,而不一定是一个标准的singleton。不要使用静态方法来创建缓存。你不能,不,这不是同一个问题。在另一个问题中,person从同一接口派生,但在派生类中他有一个非静态方法。在这个问题中,我们希望所有派生类都实现一个静态方法。这基本上是一份合同。任何实现ICache的东西都有一个静态get方法。我不相信这些答案。我们可以有这样的合同或分类。。。