これら 2 つのメソッドの両方をコンパイルするにはどうすればよいですか?
public static IEnumerable<string> DoSomething(params string[] args)
{ // do something }
public static IEnumerable<string> DoSomething(this string[] args)
{ // do something }
次のコンパイル エラーが発生します。
Type 'Extensions' already defines a member called 'DoSomething' with the same parameter types Extensions.cs
私がこれを行うことができるように:
new string[] { "", "" }.DoSomething();
Extensions.DoSomething("", "");
params メソッドがなければ、次のようにする必要があります。
Extensions.DoSomething(new string[] { "", "" });
更新: OR Mapperによる回答に基づく
public static IEnumerable<string> DoSomething(string arg, params string[] args)
{
// args null check is not required
string[] argscopy = new string[args.Length + 1];
argscopy[0] = arg;
Array.Copy(args, 0, argscopy, 1, args.Length);
return argscopy.DoSomething();
}
更新: HugoRuneの回答が気に入りました。