积极答复者
请问,为什么在这里,编译器找不到泛型方法?

问题
-
public interface IAccount { decimal Balance { get; } string Name { get; } } //Account class. public class Account : IAccount { public string Name { get; private set; } public decimal Balance { get; private set; } public Account(string name, Decimal balance) { this.Name = name; this.Balance = balance; } } //Define a algorithm method in a single class. public static class Algorithm { public static T2 Accumulate<T1, T2>(IEnumerable<T1> source, Func<T1, T2, T2> action) { T2 sum = default(T2); foreach(T1 item in source) { sum = action(item, sum); } return sum; } } //Calling the algorithm method in the Main() method. static void Main() { var accounts = new List<Account>() { new Account("Christian", 10), new Account("Stephanie", 20), new Account("Angela", 30) }; decimal amount = Algorithm.Accumulate(accounts, (item, sub) => sub -= item.Balance); } } //我的那个"Accumulate()"方法只有一个,也就是说,这个泛型的方法,为什么在调用的时候我"不显示写出类型",编译不通过... 也就是说,要这样去调用:decimal amount = Algorithm.Accumulate<Account,decimal>(accounts, (item, sub) => sub -= item.Balance); 定义的时候,怎么在调用的时候也要指明?
- 已编辑 JcsonSharpenden 2013年6月12日 3:55
答案
-
你这里只有一个参数,当然可以,系统会默认判断。但是如果你定义了多余一个的参数泛型,你不显示指定的话系统无法智能判断那个类型参数具体对应哪个类型,譬如:
Swap<K,V>到底是:
Swap(K k, V v)
还是
Swap(V v,K k)
???
一旦你定义了类型,那么具体的先后顺序也就确定了。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 ThankfulHeartModerator 2013年6月12日 9:28
全部回复
-
任何泛型都必须制定你那个泛型参数究竟是何种类型。
你的代码明显属于“泛型方法”,当然必须在调用该方法之时制定各个泛型变量的具体类型。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
class Program {
static void Main(string[] args) {
int val01 = 1;
int val02 = 2;
Swap(ref val01,ref val02); //Call the generic method.
Console.ReadKey();
}
//Define the generic method.
public static void Swap<T>(ref T val01, ref T val02) {
T temp = val01;
val01 = val02;
val02 = temp;
}
}//不是,我就见这样的可以...但是上面的不行...我就不懂...
-
你这里只有一个参数,当然可以,系统会默认判断。但是如果你定义了多余一个的参数泛型,你不显示指定的话系统无法智能判断那个类型参数具体对应哪个类型,譬如:
Swap<K,V>到底是:
Swap(K k, V v)
还是
Swap(V v,K k)
???
一旦你定义了类型,那么具体的先后顺序也就确定了。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 ThankfulHeartModerator 2013年6月12日 9:28
-
//谢谢...说的有道理...再请问一下...
public static int AddAge<Person, int>(IEnumerable<Person> person, Func<Person, int, int> action) { int sum = 0; foreach(var item in person) { sum = action(item,sum); } return sum; }
//上面在 AddAge<Person, int> 中的参数 int 报错:
Type parameter declaration must be an identifier not a type
//请问这是为什么哈...我是想用你的观点去测试...结果报的错是这样的...
- 已编辑 JcsonSharpenden 2013年6月12日 8:42
-
不是,只有泛型方法,没有(直接带泛型)的函数。函数只能定义泛型,不能直接把确定的类型定义上去。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
泛型和委托不是一个概念……
泛型:在编程涉及到具体泛型类型函数或者类的时候指定类型。
委托:可以调用同参数签名(顺序,类型,数量)以及返回值一致,但处于不同类中的方法。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
说错了...我是想说泛型...写的时候写到委托...对了...怎么结贴哈...我新到的...
- 已标记为答案 JcsonSharpenden 2013年6月12日 9:26
- 取消答案标记 ThankfulHeartModerator 2013年6月12日 9:27
-
如果你以为某些答案满意,可以点击“标记为答复”,否则就留着;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
不要标记毫无意义的答案;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report