9

Static Abstract Interface Members in C#11

 2 years ago
source link: https://dirkstrauss.com/static-abstract-interface-members/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

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.

</div


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK