How to use generics?

M

Martin

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,
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
 
J

Jon Skeet [C# MVP]

Martin said:
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 :(
 
M

Mark Rae

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...?
 
M

Martin

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
 
J

Jon Skeet [C# MVP]

Mark Rae said:
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.
 
J

Jon Skeet [C# MVP]

Martin said:
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<T> and use CompareTo instead of Math.Max, but I don't know
of any "adding" interface.
 
M

Mark Rae

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.
 
M

Mark Rae

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... ;-)
 
N

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.
 
C

Chris Mullins [MVP]

"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).
 
N

Nicholas Paldino [.NET/C# MVP]

I so dare you to say that to his face, hahahahaha....

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris Mullins said:
"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).
 
J

Julie Smith

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 said:
"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).
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Julie Smith said:
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 said:
"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).
 
R

Radek Cerny

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 said:
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 said:
"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).
 
A

atlaste

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.

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 said:
"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).
 
D

Dan Holmes

Martin said:
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.InvalidOperationException(typeof(T).ToString() + " is invalid for
this class");
}
}
public T Max (T[])
{
}
}
 
C

Chris Mullins [MVP]

:

[Multiple Inheritence]
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.
 
J

Jon Skeet [C# MVP]

Chris Mullins said:
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.
 
A

atlaste

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.


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.
 
J

Jon Skeet [C# MVP]

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...
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top