Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created February 13, 2025 07:53
Show Gist options
  • Save ufcpp/f4e1caf8b7316fa1ad4eebad1b233f84 to your computer and use it in GitHub Desktop.
Save ufcpp/f4e1caf8b7316fa1ad4eebad1b233f84 to your computer and use it in GitHub Desktop.
OverloadResolutionPriority でそういうことはできないっぽい
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