如何使用泛型?

编程入门 行业动态 更新时间:2024-10-27 20:33:37
本文介绍了如何使用泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 在下面的示例中,我想用泛型替换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

更多推荐

如何使用泛型?

本文发布于:2023-11-16 17:26:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!