-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Provide "static virtual interface members" with examples and description of static virtual methods
#43130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BartoszKlonowski
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
BartoszKlonowski:feature/41668/static-virtual
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+46
−0
Open
Provide "static virtual interface members" with examples and description of static virtual methods
#43130
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2c5afc
Implement entities showing static virtual in interface
BartoszKlonowski 0fd0790
Utilize the static virtual methods defining in TestDuplication example
BartoszKlonowski 6929ea6
Describe in details the static virtual methods of an interface
BartoszKlonowski 51c71a6
Reimplement the example of static virtual to present the Singleton ap…
BartoszKlonowski f0c613d
Rephrase the description to mention ISingleton and rephrase the entry
BartoszKlonowski 69a3f65
Cast UniqueInstance to ISingleton to access Instance
BartoszKlonowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Duplicate.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public struct Duplicate : IGetDuplicated<Duplicate> | ||
| { | ||
| public int _num; | ||
|
|
||
| public Duplicate(int num) | ||
| { | ||
| _num = num; | ||
| } | ||
|
|
||
| public static Duplicate operator ++(Duplicate other) | ||
| => other with { _num = other._num + other._num }; | ||
|
|
||
| public override string ToString() => _num.ToString(); | ||
| } |
4 changes: 4 additions & 0 deletions
4
docs/csharp/whats-new/tutorials/snippets/staticinterfaces/GetDuplicated.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| public interface IGetDuplicated<T> where T : IGetDuplicated<T> | ||
| { | ||
| static virtual T operator ++(T other) => throw new NotImplementedException(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 ++`: | ||||||
|
||||||
| 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 ++`: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.