Created
February 13, 2025 07:53
-
-
Save ufcpp/f4e1caf8b7316fa1ad4eebad1b233f84 to your computer and use it in GitHub Desktop.
OverloadResolutionPriority でそういうことはできないっぽい
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
using System.Runtime.CompilerServices; | |
int x = 1; | |
_ = x.M(); // 拡張メソッドの this 引数は ref 渡しできる。 | |
_ = (x * x).M(); // ref 渡しには右辺値は渡せないので M(ref int) を呼べない。 | |
// M(int) の方呼んでほしいけどもダメっぽい。エラーに。 | |
static class A | |
{ | |
// 右辺値の時だけこっちを呼んでほしいけど、OverloadResolutionPriority でそういうことはできないっぽい。 | |
[OverloadResolutionPriority(-1)] | |
public static int M(this int x) => x + 1; | |
public static int M(this ref int x) => x + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment