ReSharper 2024.2 Help

Code inspection: Redundant argument passed to caller argument expression parameter

The [CallerArgumentExpression] attribute, introduced in C# 10, allows you to capture the string representation of the expression passed to a method parameter. This can be useful for logging, validation, or debugging purposes.

If you are using an API marked by this attribute, passing the string representation of the corresponding argument with another argument is not necessary. Therefore, ReSharper reports it as redundant and suggests removing it.

In the example below, the attribute is applied to the str parameter of the Write method. This means that if you call Write without specifying an argument for str, the compiler will automatically use the string representation of the expression provided for num. Since the argument "a + b" passed to str matches the string representation of the expression a + b passed to num, the second argument becomes redundant and can be safely removed.

using System; using System.Runtime.CompilerServices; class RedundantArgument { void Write(int num, [CallerArgumentExpression("num")] string str = "") => Console.WriteLine(str); void Test(int a, int b) => Write(a + b, "a + b"); }
using System; using System.Runtime.CompilerServices; class RedundantArgument { void Write(int num, [CallerArgumentExpression("num")] string str = "") => Console.WriteLine(str); void Test(int a, int b) => Write(a + b); }
Last modified: 27 August 2024