Code inspection: Redundant explicit array creation in argument of 'params' parameter
In C#, methods can be defined with a variable parameter list indicated by the params keyword before the last parameter. When invoking these methods, the arguments can be listed directly in the method call, or as an array of elements of the appropriate type.
This inspection reports cases where you are explicitly creating an array for a params
argument instead of simply listing the arguments. This explicit array creation is redundant and can be simplified without changing the program's behavior.
Taking into account the signature of the String.Join() method — public static string Join(string separator, params string[] value)
, the example below illustrates how the redundant array creation can be removed to clarify the code.
string WrapString(string str)
{
return string.Join(Environment.NewLine,
new[] {"-BEGIN-", str, "-END-"});
}
string WrapString(string str)
{
return string.Join(Environment.NewLine,
"-BEGIN-", str, "-END-");
}
Last modified: 05 June 2024