Skip to content

clarify API Compatibility rules for virtual keyword #26169

@smasher164

Description

@smasher164

The policy around adding a virtual keyword was decided back in dotnet/runtime#21750. While this may technically be a breaking change, this method of making a method virtual has been used in the past in a backwards-compatible way. C# users in particular have treated adding a virtual keyword as a legal API change.


Interface members are dynamically dispatched, regardless of whether the virtual keyword is present. However, the dispatch for a sealed interface method can be call, although the C# compiler generates callvirt.

Even if the dispatch is always the same, this could potentially be a breaking change if an interface member is changed from sealed to virtual or vice-versa. For example:

using System;

B b = new C();
b.F(); // prints "B" if sealed, "C" otherwise

interface B {
    public sealed void F() {
        Console.WriteLine("B");
    }
}

public class C: B {
    public void F() {
        Console.WriteLine("C");
    }
}

Perhaps API compatibility checkers should introduce the above checks when an additional flag is passed? Strict mode is already taken to mean bidirectional checking, so maybe a Full flag to indicate that all of the rules are verified?

/cc @ericstj @MichalStrehovsky @joperezr @ViktorHofer

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions