471,317 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Generics, use mathematical operatos on type parameter

Hi all,

I would like to use mathematical operators (*, +, etc) on a generic
parameter, see the code below:

public class TestGenerics<T> where ???
{
private T d;
public TestGenerics(int a)
{
d = a * 3;
}
}

Is this possible?
If yes, what constraint should I use on the T type argument?

Thanks
Jock
Feb 4 '06 #1
7 1127
On Sat, 04 Feb 2006 10:50:07 GMT, "Jock" <Jo**@nospam.com> wrote:
Is this possible?


No. You'll have to work around this problem with an interface or base
class that implements the required functionality.
--
http://www.kynosarges.de
Feb 4 '06 #2
>>Is this possible?

No. You'll have to work around this problem with an interface or base
class that implements the required functionality.

That what I suspected!
Unfortunately when you deal with native types (int, float, double, etc)
Generics are not so handy!
When will Microsoft provide us a useful Generics implementation similar to
C++ Template?!? :))

Jock
Feb 4 '06 #3
> When will Microsoft provide us a useful Generics implementation similar
to C++ Template?!? :))


For instance would be very handy having something like that:

public class TestGenerics<T> where T : numeric
{
private T d;
public TestGenerics(int a)
{
d = a * 3;
d = (d + 7) *5;
}
}

"where T : numeric" means that you can apply to T every math operator that
is supported by every numeric *primitive* types.
So the code in the constructor above will be valid!

What do you think?

Jock
Feb 4 '06 #4
That would be quite useful. I suggest you submit your idea to the
MSDN Product Feedback Center so that the developers see it. In fact,
there's already a submission for operator restriction:

http://lab.msdn.microsoft.com/produc...c-36f1806d7ea8

You could vote for it so that MS is more likely to implement it for
the next release.
--
http://www.kynosarges.de
Feb 5 '06 #5
Wait, here's another existing suggestion that's a bit closer to your
own proposal:

http://lab.msdn.microsoft.com/produc...1-8ce455606f72
--
http://www.kynosarges.de
Feb 5 '06 #6
You could vote for it so that MS is more likely to implement it for
the next release.


Christoph,

Thanks for your suggestion.
I an going to read and vote for the suggestions on the MSDN Product Feedback
Center you point
(I even didn't know that something like that exists!).

I hope we can achieve more power with Generics very soon.

Jock
Feb 5 '06 #7
Christoph Nahr wrote:
Wait, here's another existing suggestion that's a bit closer to your
own proposal:

http://lab.msdn.microsoft.com/produc...1-8ce455606f72

Here's another:
http://tinyurl.com/8amzd

Here is an article describing the suggestion:
http://www.codeproject.com/csharp/genericnumerics.asp

HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Feb 6 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by andrew queisser | last post: by
10 posts views Thread by Ruediger Klaehn | last post: by
3 posts views Thread by Marshal | last post: by
17 posts views Thread by atgraham | last post: by
1 post views Thread by Vladimir Shiryaev | last post: by
4 posts views Thread by Cedric Rogers | last post: by
2 posts views Thread by Ravi Shekhar | last post: by
6 posts views Thread by CassioT | last post: by
5 posts views Thread by teel | last post: by

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.