You are here
Home > Programming > Static Abstract Interface Members in C#11

Static Abstract Interface Members in C#11

Static Abstract Interface Members in C#11 and .NET 7

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
Dirk is a Software Developer from South Africa. He loves all things Technology and is slightly addicted to Jimi Hendrix. Apart from writing code, he also enjoys authoring books and articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has almost mastered.
https://dirkstrauss.com

Similar Articles

Top