You are currently viewing Static Abstract Interface Members in C#11

Static Abstract Interface Members in C#11

Introduced as a preview feature in .NET 6, Static Abstract Interface Members have been made generally available in .NET 7 with C#11. This feature is enabled by default when you create a new project targeting .NET 7 and C#11.

Not what you were looking for? Try these links instead.

Static Abstract Interface Members in Action

Let’s use a simple Interface in C# to illustrate this feature.

public interface IPerson
{
    string FirstName { get; set; }
    string LastName { get; set; }
    static abstract bool IsAdult();
}

Here, I have added a static abstract interface member called IsAdult. In previous versions of .NET, this would result in a compilation error. In .NET 7, using C#11 this no longer generates an error.

What Use are Static Abstract Interface Members?

The benefit of this language feature is evident when you start using generics. If you want to call a static method from a generic type parameter, you can now do so. As seen below, you can add a constraint on a generic method that the type parameter should be derived from your IPerson Interface.

public static void ShowInfo<T>(T person) where T : IPerson
{
    Console.WriteLine($"{person.FirstName} is an adult: {T.IsAdult()}");
}

Let’s look at this in action, where we implement the Interface on two classes.

Implementing the Interface

Using the IPerson Interface, let us create two classes called Student and Scholar as seen in the code listing below. We define the scholar as not being an adult.

public class Scholar : IPerson
{
    public Scholar()
    {
    }

    required public string FirstName { get; set; }
    required public string LastName { get; set; }

    public static bool IsAdult()
    {
        return false;
    }
}

When we create a student class, we want to define them as being an adult.

public class Student : IPerson
{ 
    public Student()
    {
    }

    required public string FirstName { get; set; }
    required public string LastName { get; set; }

    public static bool IsAdult()
    {
        return true;
    }
}

Running the code

Creating a simple Console application where we create instances of our Student and Scholar classes, we can see this feature in action when we call the generic ShowInfo method.

static void Main(string[] args)
{
    var student = new Student()
    {
        FirstName = "John",
        LastName = "Smit"
    };
    ShowInfo(student);

    var scholar = new Scholar()
    {
        FirstName = "Mary",
        LastName = "Smith"
    };
    ShowInfo(scholar);

    Console.ReadLine();
}

The output from the Console application will look as follows.

Static Abstract Interface Member output in Terminal.

See some of the documentation on Static Abstract Interface Members if you want to learn more. I created this code example using Visual Studio for Mac version 17.4.

Dirk Strauss

As a seasoned software developer with a long-standing career in C# and Visual Studio, I have had the privilege of working with a number of companies and learning from some of the most talented individuals in the industry. In addition to my professional experience, I have authored multiple books on topics such as C#, Visual Studio, and ASP.NET Core. My passion for programming is unwavering, and I am dedicated to staying current with the latest technology and sharing my expertise with others.