大家好, 在下面的示例中,我想用泛型替换int。在 订单字中,我想用float,double,byte或 替换int。我试图找到一个通用的界面或 这样的东西,但我没找到。 public static int MaxSum(int [来源) { int maxSoFar = 0,maxEndingHere = 0; for(int i = 0; i< source.Length ; i ++) { maxEndingHere = Math.Max(maxEndingHere + source [i], 0); maxSoFar = Math.Max(maxSoFar,maxEndingHere); } 返回maxSoFar; } 想用公共静态替换此方法签名T $ / $ MaxSum< T>(T [] source)其中T:I ???。谢谢你的帮助。 马丁
Hi all, In the following example, I''d like to replace int by a generic. In order word, I would like to replace int by float, double, byte or something like that. I''ve tried to find a common interface or something like that, but I didn''t find. public static int MaxSum(int[] source) { int maxSoFar = 0, maxEndingHere = 0; for (int i = 0; i < source.Length; i++) { maxEndingHere = Math.Max(maxEndingHere + source[i], 0); maxSoFar = Math.Max(maxSoFar, maxEndingHere); } return maxSoFar; } In would like to replace this method signature by public static T MaxSum<T>(T[] source) where T : I???. Thank you for your help. Martin
推荐答案Martin< ma ** *******@gmail写道: Martin <ma*********@gmailwrote: 在下面的例子中,我想用泛型替换int。在 订单字中,我想用float,double,byte或 替换int。我试图找到一个共同的界面或 这样的东西,但我没找到。 In the following example, I''d like to replace int by a generic. In order word, I would like to replace int by float, double, byte or something like that. I''ve tried to find a common interface or something like that, but I didn''t find.
没有人,我很害怕。这是仿制药的一种东西,而不是以现在的形式工作:( - Jon Skeet - < sk *** @ pobox> http: //www.pobox/~skeet 博客: http:// www.msmvps/jon.skeet 如果回复小组,请不要给我发邮件
There isn''t one, I''m afraid. This is the kind of thing that generics just don''t work for in their current form :( -- Jon Skeet - <sk***@pobox> www.pobox/~skeet Blog: www.msmvps/jon.skeet If replying to the group, please do not mail me too
" Jon Skeet [C#MVP]"< sk *** @ poboxwrote in message 新闻:MP ******************* *@msnews.microsoft .. "Jon Skeet [C# MVP]" <sk***@poboxwrote in message news:MP********************@msnews.microsoft.. . 没有人,我很害怕。这是仿制药的一种东西。 > 只是不能以他们目前的形式工作:( There isn''t one, I''m afraid. This is the kind of thing that generics just don''t work for in their current form :(
也不会在即将推出的版本中,AFAIK ... 你知道这种功能是否被考虑用于未来......?b $ b? - http:// www。 markrae
Nor in the forthcoming version, AFAIK... Would you know if this sort of functionality is even being considered for the future...? -- www.markrae
嗨John和Mark, 谢谢你的回答。所以,如果我理解得很好,我必须切割并使用不同的签名来粘贴这个方法吗? - public static int MaxSum(int [] source) - public static float MaxSum(float [] source) - public static double MaxSum(double [] source) - etc. 马丁 Hi John and Mark, Thank you for you answer. So, if I understand well, I have to cut and paste this method with different signature? - public static int MaxSum(int[] source) - public static float MaxSum(float[] source) - public static double MaxSum(double[] source) - etc. Martin
更多推荐
如何使用泛型?
发布评论