-
Notifications
You must be signed in to change notification settings - Fork 6k
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
3
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.
Open
Provide "static virtual interface members" with examples and description of static virtual
methods
#43130
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7629f32
Implement entities showing static virtual in interface
BartoszKlonowski 8cfa247
Utilize the static virtual methods defining in TestDuplication example
BartoszKlonowski dd40445
Describe in details the static virtual methods of an interface
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to think through this and use a different example for a
static virtual
(notabstract
) method. That's because in practice, if the only reasonable implementation in the interface declaration is tothrow
aNotImplementedException
, that indicates that it should beabstract
.Maybe consider an interface that provides
<
and>
, but assumes thatT
implementsIComparable<T>
. Something like:I know that adds a bit more concepts to explain, so I'm open to any alternative example. That's just the first obvious one that came to my mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is valid point, @BillWagner, thanks! But it looks like my knowledge is missing something, because my question is: What would be the use case of such implementation? In other words how to call the static virtual defined in an interface without any definition in the type implementing that interface? I know I'm asking kinda for an implementation details, but can you point me some examples of such use cases or implementations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's difficult to have a good scenario here @BartoszKlonowski
That's why I suggested the
<
and>
operators. That's the one scenario I could think of where this works. It's not great, but it's reasonable.Or, since any static method could be used, something like this might work:
I have concerns here as well, because it uses a preview feature. But maybe we can adapt it somehow to do something like
Lazy<T>
, but use a static virtual method.