更新同样可以争论SomeMethod(string, string) 和SomeMethod (string, params string[]),但它有效"实际上,不,它没有.你会遇到同样的模棱两可的方法问题.public static string TypedToString(string item1, string item2){返回 "";}public static string TypedToString(string item1, params string[] items){返回 "";}public static void CallToString(){TypedToString("someString", null);//失败}I appears to me as though there is a bug/inconsistency in the C# compiler.This works fine (first method gets called):public void SomeMethod(string message, object data);public void SomeMethod(string message, params object[] data);// ....SomeMethod("woohoo", item);Yet this causes "The call is ambiguous between the following methods" error:public void SomeMethod<T>(string message, T data);public void SomeMethod<T>(string message, params T[] data);// ....SomeMethod("woohoo", (T)item);I could just use the dump the first method entirely, but since this is a very performance sensitive library and the first method will be used about 75% of the time, I would rather not always wrap things in an array and instantiate an iterator to go over a foreach if there is only one item.Splitting into different named methods would be messy at best IMO.Thoughts?EDIT:I guess Andrew might be onto something.Full example:public static class StringStuffDoer{ public static string ToString<T>(T item1, T item2) { return item2.ToString() + item1.ToString(); } public static string ToString<T>(T item, params T[] items) { StringBuilder builder = new StringBuilder(); foreach (T currentItem in items) { builder.Append(currentItem.ToString()); } return item.ToString() + builder.ToString(); } public static void CallToString() { ToString("someString", null); // FAIL ToString("someString", "another string"); // SUCCESS ToString("someString", (string)null); // SUCCESS }}I still think it is odd that the cast is needed - the call is not ambiguous. It works if you replace T with string or object or any non-generic type, so why wouldn't it work for the generic? It properly finds the two possible matching methods, so I believe that by the spec, it should pick the one that doesn't use params if possible. Correct me if I'm wrong here.(NOT SO) FINAL UPDATE:Sorry for taking you guys on this tyraid, I've apparently been staring at this too long...too much looking at generics and params for one night. The non-generic version throws the ambiguous error as well, I just had the method signature off in my mockup test.REAL FINAL UPDATE:Okay, this is why the problem didn't show up in my non-generic test. I was using "object" as the type parameters. SomeMethod(object) and SomeMethod(params object[]) do NOT throw the ambiguous error, I guess "null" automatically gets cast to "object". A bit strange I'd say, but somewhat understandable, maybe.So, oddly enough, this call DOES work:SomeMethod<object>("someMessage", null); 解决方案 Seems to work for me, does the rest of your code look like the following?class TestThing<T>{ public void SomeMethod(string message, T data) { Console.WriteLine("first"); } public void SomeMethod(string message, params T[] data) { Console.WriteLine("second"); }}class Program{ static void Main(string[] args) { var item = new object(); var test_thing = new TestThing<object>(); test_thing.SomeMethod("woohoo", item); test_thing.SomeMethod("woohoo", item, item); Console.ReadLine(); }}Compiles fine on .NET 3.5 and outputs "first" and then "second" when run. Which version are you using/targeting?EDITThe issue from your code above is that the compiler can't tell what type null is. It is equally valid to assume that it is a string or an array of strings, hence why it is ambiguous when just null and not ambiguous when you specifically cast it (i.e. you're telling the compiler how it should be treated).UPDATE "The same can be argued for SomeMethod(string, string) and SomeMethod (string, params string[]), yet it works"Actually, no it doesn't. You get the same ambiguous method problem.public static string TypedToString(string item1, string item2){ return "";}public static string TypedToString(string item1, params string[] items){ return "";}public static void CallToString(){ TypedToString("someString", null); // FAIL} 这篇关于C#方法泛型params参数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-26 11:02