473,795 Members | 2,443 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
32 1884
I'd personally like to see good example of where multiple inheritance would
be usefull. Personally, I'm glad C# doesn't implement it.

I can't see myself using it in any situation... unless I was employed to
develop nickelodoen's CatDog series into a game :-)

But seriously, I'd really like to see an example of where multiple
inheritance is required or a good idea.
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
"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 #11
I so dare you to say that to his face, hahahahaha....

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
"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 #12
Julie,

Chris was kidding. However, I have to say, I laughed out loud when I
read about CatDog. I think that's probably the best argument for multiple
inheritance to date (even though you could still use interface
implementation to do it correctly haha).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Julie Smith" <so*****@home.c omwrote in message
news:uf******** ********@TK2MSF TNGP04.phx.gbl. ..
I'd personally like to see good example of where multiple inheritance
would be usefull. Personally, I'm glad C# doesn't implement it.

I can't see myself using it in any situation... unless I was employed to
develop nickelodoen's CatDog series into a game :-)

But seriously, I'd really like to see an example of where multiple
inheritance is required or a good idea.
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>"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 #13
Here we go again.

MI is like sight. If you grew up with it, you miss it dearly. If you never
had it, you can not imagine what its like. When modelling the world, if you
have MI, you can model orthoganal features knowing you can implement using
MI. I grew up with it and miss it dearly.

"Julie Smith" <so*****@home.c omwrote in message
news:uf******** ********@TK2MSF TNGP04.phx.gbl. ..
I'd personally like to see good example of where multiple inheritance
would be usefull. Personally, I'm glad C# doesn't implement it.

I can't see myself using it in any situation... unless I was employed to
develop nickelodoen's CatDog series into a game :-)

But seriously, I'd really like to see an example of where multiple
inheritance is required or a good idea.
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>"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 #14
Different people have different opinions. I found both the strong
support for templates and multiple inheritance quite useful in C++ -
where both are lacking in C#. I recall discussion this with Anders as
well. But eventually these discussions always boil down to "personal
taste" and are therefore quite useless.

To comment on your statement: a Porsche isn't really necessary to
drive to your work when you have a Peugeot 206 as well, but if I can
choose freely, I sure as hell don't have to think about how I would
like to go to work. MI isn't really necessary, no. But if you write
(proper) C++ for a couple of years, half the code you will write will
contain MI.

IMHO C# is still on its way to become the next generation Porsche.

Cheers,
Stefan.

On May 9, 4:15 am, "Julie Smith" <some...@home.c omwrote:
I'd personally like to see good example of where multiple inheritance would
be usefull. Personally, I'm glad C# doesn't implement it.

I can't see myself using it in any situation... unless I was employed to
develop nickelodoen's CatDog series into a game :-)

But seriously, I'd really like to see an example of where multiple
inheritance is required or a good idea.

"Chris Mullins [MVP]" <cmull...@yahoo .comwrote in messagenews:%2* *************** @TK2MSFTNGP04.p hx.gbl...
"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 #15
Martin wrote:
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
Or you could use a static constructor. I haven't tried this with a
static method. You may have to restructure how you would call this but
it would allow the use of the generic.
public class Max<T: Value where T : IComparable
{
static Max()
{
if (!(typeof(T) == typeof(int) || typeof(T) == typeof(string)
|| typeof(T) == typeof(bool) || typeof(T) == typeof(float)
|| typeof(T) == typeof(Single) || typeof(T) == typeof(double) ||
typeof(T) == typeof(decimal) )
{
throw new
System.InvalidO perationExcepti on(typeof(T).To String() + " is invalid for
this class");
}
}
public T Max (T[])
{
}
}
May 9 '07 #16
"Nicholas Paldino [.NET/C# MVP]" wrote:

[Multiple Inheritence]
>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).
I so dare you to say that to his face, hahahahaha....
I would, without hesitation. The only argument I've ever head put forward
is, "Most people do it wrong, therefore we don't include it in our language.
Have you see the Dreaded Diamond Diagram?".

This argument is so lame, and the feature so powerfull, that I really can't
understand why there hasn't been a huge cry for it in both .Net and Java.

I frequently have to jump through crazy hoops to get stuff done, that would
be effortless with multiple inheritence. This is especially true when your
inheritence tree is dictated for you - as is often then case with Streams,
WinForm Forms, WinForm Controls, ASP.Net controls, etc. I hate working
around problems by creating an Interface and manually implementing it in 10
different classes.

I learned OO in C++, and it's a feature I really miss in these newer
languages.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
May 9 '07 #17
Chris Mullins [MVP] <cm******@yahoo .comwrote:
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).
I so dare you to say that to his face, hahahahaha....

I would, without hesitation. The only argument I've ever head put forward
is, "Most people do it wrong, therefore we don't include it in our language.
Have you see the Dreaded Diamond Diagram?".

This argument is so lame, and the feature so powerfull, that I really can't
understand why there hasn't been a huge cry for it in both .Net and Java.
I don't think the argument is lame. It's just as valid for MI as it is
for macros and templates: yes, they're all very powerful features, but
not only is it *possible* to abuse them, but people *will* abuse them.

I know people will always be able to abuse any language feature, but
some are more naturally easy to abuse (and with more dire consequences)
than others.
I frequently have to jump through crazy hoops to get stuff done, that would
be effortless with multiple inheritence.
Whereas I believe if MI were available, I'd frequently have to jump
through hoops to maintain code which used MI inappropriately ...

<snip>

Obviously it's a matter of personal opinion where the line should be
drawn, but C# has clearly been designed to remove some of the most
powerful features of other languages in order to try to avoid some of
the messes we've had in the past.

--
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 9 '07 #18
On May 9, 9:30 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
Chris Mullins [MVP] <cmull...@yahoo .comwrote:
>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).
I so dare you to say that to his face, hahahahaha....
I would, without hesitation. The only argument I've ever head put forward
is, "Most people do it wrong, therefore we don't include it in our language.
Have you see the Dreaded Diamond Diagram?".
This argument is so lame, and the feature so powerfull, that I really can't
understand why there hasn't been a huge cry for it in both .Net and Java.

I don't think the argument is lame. It's just as valid for MI as it is
for macros and templates: yes, they're all very powerful features, but
not only is it *possible* to abuse them, but people *will* abuse them.

I know people will always be able to abuse any language feature, but
some are more naturally easy to abuse (and with more dire consequences)
than others.
I frequently have to jump through crazy hoops to get stuff done, that would
be effortless with multiple inheritence.

Whereas I believe if MI were available, I'd frequently have to jump
through hoops to maintain code which used MI inappropriately ...

<snip>

Obviously it's a matter of personal opinion where the line should be
drawn, but C# has clearly been designed to remove some of the most
powerful features of other languages in order to try to avoid some of
the messes we've had in the past.
Somehow I don't grasp one aspect of your statement: who are you (or
for that matter anyone else) to decide what usage of features is
called "abusive" and what usage is not? It's all in the eye of the
beholder; you say it yourself :
Whereas I believe if MI were available, I'd frequently have to jump
through hoops to maintain code which used MI inappropriately ...
What might seem like "bad" to you might seem "good" to someone else.
I've seen "abusive" usage of - especially MI and templates - in the
past that any "sane" programmer would call abusive, but when further
investigation followed the choice made perfectly sense (and was often
from that perspective better than the straight-forward approach).

Call me insane, I would rather educate people I hire in making sane
choices than removing the hammer from my toolbox. That's more like a
discussion about "freedom" versus "self protection" than a discussion
about "personal taste".

Cheers,
Stefan.

May 10 '07 #19
atlaste <at*****@gmail. comwrote:

<snip>
Obviously it's a matter of personal opinion where the line should be
drawn, but C# has clearly been designed to remove some of the most
powerful features of other languages in order to try to avoid some of
the messes we've had in the past.

Somehow I don't grasp one aspect of your statement: who are you (or
for that matter anyone else) to decide what usage of features is
called "abusive" and what usage is not? It's all in the eye of the
beholder; you say it yourself :
I'm the potential maintainer of code containing such abuses - as is
everyone else. I have no more right of judgement than anyone else, but
I believe it's reasonable for anyone to think that a particular feature
is a potential source of trouble.

Just because it's all in the eye of the beholder doesn't make it any
easier to maintain the code...
Whereas I believe if MI were available, I'd frequently have to jump
through hoops to maintain code which used MI inappropriately ...

What might seem like "bad" to you might seem "good" to someone else.
I've seen "abusive" usage of - especially MI and templates - in the
past that any "sane" programmer would call abusive, but when further
investigation followed the choice made perfectly sense (and was often
from that perspective better than the straight-forward approach).
It would depend on the exact situation, of course, but often the
"clever" solution takes so long for each maintenance programmer to
understand that it would still have been more sensible to go with the
more straight-forward approach.
Call me insane, I would rather educate people I hire in making sane
choices than removing the hammer from my toolbox. That's more like a
discussion about "freedom" versus "self protection" than a discussion
about "personal taste".
It all sounds great in theory, but in practice it doesn't seem to work,
otherwise we wouldn't have such rubbish code around.

(I have other issues with MI in terms of keeping the language simple
where possible, and I don't believe the benefits of MI outweight the
complexity, but that's a separate issue.)

--
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 10 '07 #20

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
3107
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
2556
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
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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
10164
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,...
1
7540
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
6780
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.