Ran*_*dom 6 c# default-interface-member
我按照本指南https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods来使用默认接口实现功能。我复制了一段代码,该代码在接口中定义了默认实现IA,然后在接口中覆盖它IB:
interface I0
{
void M() { Console.WriteLine("I0"); }
}
interface I1 : I0
{
override void M() { Console.WriteLine("I1"); }
}
Run Code Online (Sandbox Code Playgroud)
但它给出了一个错误CS0106 The modifier 'override' is not valid for this item和一个警告CS0108 'I1.M()' hides inherited member 'I0.M()'. Use the new keyword if hiding was intended。TargetFramework设置为net5.0,LangVersion是latest。为什么即使官方文档中描述了它也不起作用?
Ran*_*dom 18
显然,带有override关键字的示例是不正确的,必须删除该关键字。此外,只有显式指定方法接口时它才有效:
interface I0
{
void M() { Console.WriteLine("I0"); }
}
interface I1 : I0
{
void I0.M() { Console.WriteLine("I1"); }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4190 次 |
| 最近记录: |