コードインスペクション:拡張機能ブロックへ移動
C# 14 では、拡張メソッドを宣言する新しい方法として 拡張機能ブロックが導入されており、同じ拡張対象型に対して複数のメソッドをエレガントに宣言できます。 そのため、ReSharper は特定のターゲット拡張型の拡張メソッドを、その拡張型に一致する extension ブロックに移動することを提案します。 たとえば、次のようになります。
public static class StringExtensions
{
extension(string str)
{
public bool IsNullOrEmpty()
{
return string.IsNullOrEmpty(str);
}
public bool IsNullOrWhiteSpace()
{
return string.IsNullOrWhiteSpace(str);
}
}
public static string TrimStart(this string str, string prefix)
{
if (str.StartsWith(prefix))
return str.Substring(prefix.Length);
return str;
}
}
public static class StringExtensions
{
extension(string str)
{
public bool IsNullOrEmpty()
{
return string.IsNullOrEmpty(str);
}
public bool IsNullOrWhiteSpace()
{
return string.IsNullOrWhiteSpace(str);
}
public string TrimStart(string prefix)
{
if (str.StartsWith(prefix))
return str.Substring(prefix.Length);
return str;
}
}
}
2026 年 6 月 12 日