Skip to content

Conversation

BartoszKlonowski
Copy link
Contributor

@BartoszKlonowski BartoszKlonowski commented Oct 19, 2024

This pull request closes #41668
It adds the section presenting the static virtual methods of an interface.

This PR adds the separate implementation files for IGetDuplicated interface containing the static virtual example as well as the Duplicate type implementing that interface.
The implementation of the Duplicate struct is (on purpose) very similar to already existing RepeatSequence type, so that it bases on the reader's understanding and provides them with the comparison between static abstract and static virtual scenarios.

The description is brief, with only required details, and follows the same style/layout as the ## Static abstract interface methods section.
The only addition is the note regarding the pitfall of leaving the static virtual method unimplemented. Compiler won't scream about lack of it explicitly, so I wanted to highlight that having the default implementation does not mean that this default implementation will be called.

This is broad topic, so any concerns or remarks will be highly appreciated.


Internal previews

📄 File 🔗 Preview link
docs/csharp/whats-new/tutorials/static-virtual-interface-members.md Tutorial: Explore C# 11 feature - static virtual members in interfaces

@BartoszKlonowski BartoszKlonowski requested review from BillWagner and a team as code owners October 19, 2024 20:48
@dotnetrepoman dotnetrepoman bot added this to the October 2024 milestone Oct 19, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates PR is created by someone from the .NET community. label Oct 19, 2024
@BillWagner
Copy link
Member

@BartoszKlonowski Do you want to come back to this one? I think it's close.

@BartoszKlonowski
Copy link
Contributor Author

@BillWagner Yes, let me come back to this considering the last input you made here 👍

@BillWagner
Copy link
Member

@BartoszKlonowski Thanks. Also, as it's now mid-August, I have less concerns about using the field feature. November and .NET 10 are coming soon.

@BartoszKlonowski BartoszKlonowski force-pushed the feature/41668/static-virtual branch from dd40445 to 6929ea6 Compare August 24, 2025 15:47
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this @BartoszKlonowski

I had one wording suggestion. Let me know what you think, and then I'll merge this.

@@ -66,6 +66,26 @@ AAAAAAAAAA

This small example demonstrates the motivation for this feature. You can use natural syntax for operators, constant values, and other static operations. You can explore these techniques when you create multiple types that rely on static members, including overloaded operators. Define the interfaces that match your types' capabilities and then declare those types' support for the new interface.

## Static virtual interface methods

Similar can be achieved using the static virtual methods of an interface. This is done with the syntax of `static` and `virtual` modifiers added to any member that should be implemented in the type implementing that interface. The following example defines the `IGetDuplicated<T>` interface, providing any type with a constraint that a type will implement the `operator ++`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This opening sentence seemed out of place somehow. How about this alternative:

Suggested change
Similar can be achieved using the static virtual methods of an interface. This is done with the syntax of `static` and `virtual` modifiers added to any member that should be implemented in the type implementing that interface. The following example defines the `IGetDuplicated<T>` interface, providing any type with a constraint that a type will implement the `operator ++`:
You can also provide a default implementation for static methods defined in an interface. This is done with the syntax of `static` and `virtual` modifiers added to any member that should be implemented in the type implementing that interface. The following example defines the `IGetDuplicated<T>` interface, providing any type with a constraint that a type will implement the `operator ++`:

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this.

There's one build error that we need to fix. I have a suggestion that emphasizes the fact that default interface members must be accessed through a reference of the interface type.

LMK what you think, and then I'll merge.

@@ -9,6 +9,11 @@
Console.WriteLine(str++);
// </TestRepeat>

// <TestUniqueInstance>
var instance = UniqueInstance.Instance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this to work, you'll need to use the interface type:

Suggested change
var instance = UniqueInstance.Instance;
var instance = (UniqueInstance as ISingleton)?.Instance;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner I committed this suggestion, although locally, with VS still on version 17.4 I do have issues with your suggestion: CS0119 for UniqueInstance and CS0305 for ISingleton (which can be fixed by specifying the T type for this cast).
Let's see if remotely it will also fail and then we can check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner I should mention, that locally, with VS v17.4 I can also see the initial error for Instance not being a part of UniqueInstance.
Somewhere I read that static virtual feature requires:

  • .NET v8.0 or above - locally I'm running on .NET 9, and I see this staticinterfaces project runs on .NET 8, which should be fine?
  • preview enabled as LangVersion - we have that.
  • VS 17.6 or above - this is what I'm missing locally, but can't say regarding the CI checks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @BartoszKlonowski

I see the new errors. To answer your question:

.NET v8.0 or above - locally I'm running on .NET 9, and I see this staticinterfaces project runs on .NET 8, which should be fine?

Yes, it should be. As a normal practice, we're happy to move to the latest GA release (.NET 9 now).

preview enabled as LangVersion - we have that.

Check.

VS 17.6 or above - this is what I'm missing locally, but can't say regarding the CI checks.

We should be using the .NET 10 preview build (because some of the docs are for the preview).

Now, this example might be tricky to get right unless the type overrides the interface method (even if it just calls the interface implementation.) Why? Well, an ISingleton<int> could be implemented by more than one class type. So, which one should be used? Without an instance of the type, that's hard to express.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-csharp/svc whats-new/subsvc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

One of the samples should show a static virtual method
2 participants