是的,C# 8.0引入了一些新的特性,包括接口中的默认实现、接口中的私有成员、接口中的静态成员和接口中的扩展方法。
interface IExample
{
void Method1();
void Method2()
{
Console.WriteLine("Default implementation of Method2");
}
}
class Example : IExample
{
public void Method1()
{
Console.WriteLine("Implementation of Method1");
}
}
interface IExample
{
private void PrivateMethod()
{
Console.WriteLine("Private method in interface");
}
void PublicMethod()
{
PrivateMethod();
}
}
interface IExample
{
static void StaticMethod()
{
Console.WriteLine("Static method in interface");
}
}
IExample.StaticMethod();
interface IExample
{
void Method();
}
static class ExampleExtensions
{
public static void EnhancedMethod(this IExample example)
{
Console.WriteLine("Enhanced method in extension");
}
}
class Example : IExample
{
public void Method()
{
Console.WriteLine("Implementation of Method");
}
}
Example example = new Example();
example.EnhancedMethod();