我怎样才能同时编译这两种方法?
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的回答。