是的,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();