How to use generics?
- From: Martin <martingagne@xxxxxxxxx>
- Date: 8 May 2007 15:55:24 -0700
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
.
- Follow-Ups:
- Re: How to use generics?
- From: Jon Skeet [C# MVP]
- Re: How to use generics?
- Prev by Date: Re: self-confidence of compiler
- Next by Date: Re: self-confidence of compiler
- Previous by thread: GridView not displaying data in browser on pressing button Go when bind to DataTable
- Next by thread: Re: How to use generics?
- Index(es):
Loading