C# 必须声明一个主体,因为它没有被标记为抽象的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16581822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:23:50  来源:igfitidea点击:

must declare a body because it's not marked abstract

c#

提问by user1765862

I have interface

我有接口

INews.cs
public interface INews{
   int Id {get; set;}
   string Name {get; set;}
   void Add(News news);'
   void Remove(News news);
}

and I have News.cs which implements that interface

我有实现该接口的 News.cs

public class News:INews{
  public int Id {get; set;}
  public string Name {get; set;}
  public void Add(News news);
  public void Remove(News news);

}

}

on compile I have following message must declare a body because it's not marked abstract

在编译时我有以下消息 必须声明一个主体,因为它没有标记为抽象

is that mean that I should declare body inside constructor of News class?

这是否意味着我应该在 News 类的构造函数中声明 body?

采纳答案by Jens Kloster

Its your implementation

它是你的实现

public class News: INews
{
  public int Id {get; set;}
  public string Name {get; set;}
  public void Add(News news); //<-- invalid
  public void Remove(News news); //<-- invalid
}

should at least be

至少应该是

public class News: INews
{
  public int Id {get; set;}
  public string Name {get; set;}
  public void Add(News news){

  }

  public void Remove(News news){

  }
}

回答by nvoigt

Your functions need bodies:

您的功能需要机构:

public void Add(News news)
{

} 

public void Remove(News news)
{

}

Functions without bodies are only allowed in abstract classes.

没有主体的函数只允许在抽象类中使用。

回答by Oded

It means that you have not make your Newsclass an abstractclass.

这意味着你还没有让你的News班级成为一个abstract班级。

In a class that is not an abstractclass, the methods must have implementations, not just declarations.

在不是abstract类的类中,方法必须有实现,而不仅仅是声明。

回答by Nate

the method must declare a body if its notan abstractclass

如果该方法必须声明主体不是abstract

public void Add(News news)
{

}

public void Remove(News news)
{

}