【发布时间】:2026-01-13 10:35:01
【问题描述】:
如果我有接口IExampleInterface:
interface IExampleInterface {
int GetValue();
}
有没有办法在子接口中为GetValue() 提供默认实现?即:
interface IExampleInterfaceChild : IExampleInterface {
// Compiler warns that we're just name hiding here.
// Attempting to use 'override' keyword results in compiler error.
int GetValue() => 123;
}
【问题讨论】:
-
接口不包含任何代码。
-
@TaW 他们在最新版本的 c# 中做。许多人认为这是一个糟糕的设计选择,但这是一个见仁见智的问题。 @Xenoprimate 你试过
new int GetValue() => 123;吗? -
嗯,很有趣,虽然很违反直觉。但是事情往往会在一个人身上成长.. ;-)
标签: c# c#-8.0 default-interface-member