473,785 Members | 3,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use generics?

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(maxEnd ingHere + source[i],
0);
maxSoFar = Math.Max(maxSoF ar, 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

May 8 '07 #1
32 1880
Martin <ma*********@gm ail.comwrote:
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.
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.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 8 '07 #2
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** ************@ms news.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 :(
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

May 8 '07 #3
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

May 8 '07 #4
Mark Rae <ma**@markNOSPA Mrae.netwrote:
There isn't one, I'm afraid. This is the kind of thing that generics
just don't work for in their current form :(

Nor in the forthcoming version, AFAIK...

Would you know if this sort of functionality is even being considered for
the future...?
I wouldn't, I'm afraid. I dare say I wouldn't be allowed to give any
details if I *did* know what's being planned for C# 4, but I'm probably
allowed to say that I don't have the faintest idea what's coming up on
that front.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 8 '07 #5
Martin <ma*********@gm ail.comwrote:
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.
Yes, I'm afraid so.

If you weren't summing them, you could probably use a constraint of
IComparable<Tan d use CompareTo instead of Math.Max, but I don't know
of any "adding" interface.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 8 '07 #6
"Martin" <ma*********@gm ail.comwrote in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
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.
Yes.
--
http://www.markrae.net

May 8 '07 #7
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** ************@ms news.microsoft. com...
>Would you know if this sort of functionality is even being considered for
the future...?

I wouldn't, I'm afraid. I dare say I wouldn't be allowed to give any
details if I *did* know what's being planned for C# 4, but I'm probably
allowed to say that I don't have the faintest idea what's coming up on
that front.
LOL!

And there was me thinking that all you C# MVPs "do lunch" with Anders
Hejlsberg every week and argue the toss over things like multiple
inheritance etc... ;-)
--
http://www.markrae.net

May 8 '07 #8
I've had the pleasure of meeting Anders a few times. The man could take
on all of the MVPs single handedly in debates over language design and
general programming issues. He's just that knowledgable on the subject, to
the point where it isn't even worth trying to debate him on an issue that he
knows about.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Mark Rae" <ma**@markNOSPA Mrae.netwrote in message
news:u1******** ******@TK2MSFTN GP02.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** ************@ms news.microsoft. com...
>>Would you know if this sort of functionality is even being considered
for
the future...?

I wouldn't, I'm afraid. I dare say I wouldn't be allowed to give any
details if I *did* know what's being planned for C# 4, but I'm probably
allowed to say that I don't have the faintest idea what's coming up on
that front.

LOL!

And there was me thinking that all you C# MVPs "do lunch" with Anders
Hejlsberg every week and argue the toss over things like multiple
inheritance etc... ;-)
--
http://www.markrae.net

May 9 '07 #9
"Nicholas Paldino [.NET/C# MVP]":
I've had the pleasure of meeting Anders a few times. The man could
take on all of the MVPs single handedly in debates over language design
and general programming issues. He's just that knowledgable on the
subject, to the point where it isn't even worth trying to debate him on an
issue that he knows about.
.... if that were true, he would have put in Multiple Inheritence by now.

Obviously, it's not in there because he doesn't understand how it works and
how beneficial it can be (... and if ya believe that one, I've got some
swampland for sale).

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
May 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
2463
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and still no can do... Any info would be great!
2
3106
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
2554
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics vs C++ templates. Does anyone knows a workaround to do this ? Thx : public class C<T> { private T myValue;
12
2744
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as it defines the language more completely. Great to use.
5
2921
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have an "AppleBasket" which is a class that contains a collection of apples. So, some code: class Fruit{ }
11
2501
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
9
5986
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal Experience Only, Please. ...
1
2438
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes "ThrowInConstructor" and "ThrowInFoo". First one throws "MyOwnException" in constructor, second one in "Foo()" method. There is a "GenericCatch" generics class able to accept "ThrowInConstructor" and "ThrowInFoo" as type parameter "<T>". There are two methods in...
7
3257
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the System.Collections.Generic namespace. Literature I have read on this ties generics in with collections, hence articulate their examples as such. That's fine, I understand what is being said. My question is more towards the application and implementation...
13
3837
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.