rksoftware

Visual Studio とか C# とかが好きです

呼び出し元の引数の式

■ C# 10.0 での新機能

・呼び出し元の引数の式 (Caller argument expression)
  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/caller-argument-expression
 引数の部分で書かれた式を呼び出し先のメソッドで文字列として引数として受け取れる。

var array = new[] { 1 };

Debug.Assert(array != null);        // 「array != null」と出力される
Debug.Assert(array.Length == 1);    // 「array.Length == 1」と出力される

public static class Debug
{
    public static void Assert(bool condition, [System.Runtime.CompilerServices.CallerArgumentExpression("condition")] string message = null)
    {
        System.Console.WriteLine(message);
    }
}