void foo(Tuple<object> t)
{
}
void bar()
{
foo(Tuple.Create("hello"));
}
C#編譯器返回
error CS1502: The best overloaded method match for 'foo(System.Tuple<object>)' has some invalid arguments
error CS1503: Argument 1: cannot convert from 'System.Tuple<string>' to 'System.Tuple<object>'
添加明確的類型Tuple.Create違背了它的目的。我如何說服編譯器接受代碼?
FWIW,我認爲C++不存在這個問題:http://live.boost.org/doc/libs/1_33_1/libs/tuple/doc/tuple_users_guide.html#constructing_tuples
如果你不在乎被強類型化爲什麼不使用void foo(params object [] args)? – cordialgerm 2010-10-05 21:55:13