C# 10 new feature CallerArgumentExpression, argument check and more
using System.Runtime.CompilerServices;
void Function(int a, TimeSpan b, [CallerArgumentExpression("a")] string c = "", [CallerArgumentExpression("b")] string d = "")
{
Console.WriteLine($"Called with value {a} from expression '{c}'");
Console.WriteLine($"Called with value {b} from expression '{d}'");
Console.WriteLine();
}
Function(1, default);
Function(1, default, "1", "default");
int x = 1;
TimeSpan y = TimeSpan.Zero;
Function(x, y);
Function(x, y, "x", "y");
Function(int.Parse("2") + 1 + Math.Max(2, 3), TimeSpan.Zero - TimeSpan.MaxValue);
Function(int.Parse("2") + 1 + Math.Max(2, 3), TimeSpan.Zero - TimeSpan.MaxValue, "int.Parse(\"2\") + 1 + Math.Max(2, 3)", "TimeSpan.Zero - TimeSpan.MaxValue");
Called with value 1 from expression '1'
Called with value 00:00:00 from expression 'default'
Called with value 1 from expression '1'
Called with value 00:00:00 from expression 'default'
Called with value 1 from expression 'x'
Called with value 00:00:00 from expression 'y'
Called with value 1 from expression 'x'
Called with value 00:00:00 from expression 'y'
Called with value 6 from expression 'int.Parse("2") + 1 + Math.Max(2, 3)'
Called with value -10675199.02:48:05.4775807 from expression 'TimeSpan.Zero - TimeSpan.MaxValue'
Called with value 6 from expression 'int.Parse("2") + 1 + Math.Max(2, 3)'
Called with value -10675199.02:48:05.4775807 from expression 'TimeSpan.Zero - TimeSpan.MaxValue'
p_agonok