如何使用泛型? [英] How to use generics?

查看:57
本文介绍了如何使用泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在下面的示例中,我想用泛型替换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.com写道:
Martin <ma*********@gmail.comwrote:

在下面的例子中,我想用泛型替换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.com>
http: //www.pobox.com/~skeet 博客: http:// www.msmvps.com/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.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


" Jon Skeet [C#MVP]"< sk *** @ pobox.comwrote in message

新闻:MP ******************* *@msnews.microsoft.com ..
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .

没有人,我很害怕。这是仿制药的一种东西。 >
只是不能以他们目前的形式工作:(
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.net

Nor in the forthcoming version, AFAIK...

Would you know if this sort of functionality is even being considered for
the future...?

--
http://www.markrae.net


嗨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


这篇关于如何使用泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆