提问人:Aleksandar 提问时间:9/4/2023 更新时间:9/4/2023 访问量:35
为什么我的类尽管有定义的事件,但仍未实现接口?
Why doesn't my class implement the interface despite having a defined event?
问:
我目前正在学习具有以下代码的事件:
public interface INotifyMessageChanged
{
public event EventHandler MessageChanged;
}
public class Academy : Organization, INotifyMessageChanged
{
public event EventHandler? NotifySubScriber;
private string message = "";
public string Message
{
get { return message; }
set { message = value; MessageChanged(); }
}
public Academy(string name, string address) : base(name)
{
Address = address;
}
void MessageChanged()
{
NotifySubScriber?.Invoke(this, nul);
}
}
该代码产生以下错误:
CS0535 'Academy' does not implement interface member 'INotifyMessageChanged.MessageChanged'
接口中的事件 MessageChanged 不应该与类中的事件相对应吗?
答:
1赞
Mad hatter
9/4/2023
#1
您的活动签名不同,其名称也不相同: 和EventHandler MessageChanged
EventHandler? NotifySubscriber
public interface INotifyMessageChanged
{
public event EventHandler? MessageChanged;
}
public class Academy : Organization, INotifyMessageChanged
{
public event EventHandler? MessageChanged;
private string message = "";
public string Message
{
get { return message; }
set { message = value; this.OnMessageChanged(); }
}
public Academy(string name, string address) : base(name)
{
Address = address;
}
void OnMessageChanged()
{
this.MessageChanged?.Invoke(this, null);
}
}
评论
0赞
Aleksandar
9/4/2023
无论我是否使它们都可为空,错误仍在产生 编辑:使其与您的示例一起使用 - 谢谢!
评论
event EventHandler MessageChanged
MessageChanged