473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1207
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
3
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class...
17
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at...
1
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...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
2
by: Ravi Shekhar | last post by:
Hello, So I'm doing some mathematical modeling and it turns out I need distinct imaginary and complex types to carry out complex contour integration. I want to have it in all three precisions...
6
by: CassioT | last post by:
Hi. I want to create a base form class with a generic parameter. public BaseForm<T: Form {} public MyForm : BaseForm<MyType> {} The problem here is that the inherited form doesn't work in...
5
by: teel | last post by:
Hi there, I'm trying to apply "less than" and "more than" operators on the Generics (class template-like) type objects. Below is the code of my class representing a parameter that can be any type,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.