473,320 Members | 1,950 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,320 software developers and data experts.

Question regarding generics and type constraints

I'm new to Generics (years and years of VS.NET 2003 development, but very
little .NET 2.0+). I have some routines for conversion from RGB to different
color spaces and back. I would like the routines to support, at least, byte,
int, and double types without having to create different versions of the
routines. Is there any way to do this with generics?

The problem is, if I do something like this:

public static void RGB_to_YIQ<T>(T[] R, T[] G, T[] B, T[] Y, T[] I, T[] Q)
{
Y[index] = 0.299 * R[index] + 0.587 * G[index] + 0.114 * B[index];
...
}

I get an error: "Operator '*' cannot be applied to operands of type 'double'
and 'T'"

Like I said, I'm a newb to the whole generics thing. I read the "Constraints
on Type Parameters" stuff using "where" in MSDN, but I'm not sure how I can
use that, if I can.

Thanks.

Feb 13 '08 #1
5 1437
There is an issue with using operators with generics.

If you have .NET 3.5, I have a full and easy solution to this problem here:

http://www.pobox.com/~skeet/csharp/g...operators.html
(also briefly explains the problem)

Note the "usage" page, and the download via "MiscUtil" (the two links in the
opening lines).

Basically, you can various things involving Operator.Multiply(x,y),
Operator.Add(x,y) - you might also need Operator.Convert<float,T(or
something) to handle your literals (0.299 etc).

I'm about to run away for a few hours, but post back if you want more
info...

Marc
Feb 13 '08 #2
Marc,

Interesting stuff. This code is generally used in performance-critical areas
and it looks like your code would add a good bit of overhead converting all
the operators to method calls, though I don't know enough about it to be
sure.

If it's going to impede performance significantly, I'll just stick to
separate versions of the code.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
There is an issue with using operators with generics.

If you have .NET 3.5, I have a full and easy solution to this problem
here:

http://www.pobox.com/~skeet/csharp/g...operators.html
(also briefly explains the problem)

Note the "usage" page, and the download via "MiscUtil" (the two links in
the opening lines).

Basically, you can various things involving Operator.Multiply(x,y),
Operator.Add(x,y) - you might also need Operator.Convert<float,T(or
something) to handle your literals (0.299 etc).

I'm about to run away for a few hours, but post back if you want more
info...

Marc

Feb 13 '08 #3
Below is a link that may help you understand why you are having this
problem:
http://www.codeproject.com/KB/cs/genericnumerics.aspx

"Fredo" <fr***@hotmail.comwrote in message
news:i-******************************@giganews.com...
I'm new to Generics (years and years of VS.NET 2003 development, but very
little .NET 2.0+). I have some routines for conversion from RGB to
different color spaces and back. I would like the routines to support, at
least, byte, int, and double types without having to create different
versions of the routines. Is there any way to do this with generics?

The problem is, if I do something like this:

public static void RGB_to_YIQ<T>(T[] R, T[] G, T[] B, T[] Y, T[] I, T[] Q)
{
Y[index] = 0.299 * R[index] + 0.587 * G[index] + 0.114 * B[index];
...
}

I get an error: "Operator '*' cannot be applied to operands of type
'double' and 'T'"

Like I said, I'm a newb to the whole generics thing. I read the
"Constraints on Type Parameters" stuff using "where" in MSDN, but I'm not
sure how I can use that, if I can.

Thanks.

Feb 13 '08 #4
Interesting stuff. This code is generally used in performance-critical areas
and it looks like your code would add a good bit of overhead converting all
the operators to method calls, though I don't know enough about it to be
sure.
It actually inlines pretty well, especially for non-nullable types -
but if this is 100% performance critical, then I'd have separate
Int32, Byte, etc methods; but in all seriousness - the slowdown isn't
as bad as you might expect, since this the expression is compiled once
(only) per type and re-used. You lose a bit of time in delegate
invoke, but thats about it.

Marc
Feb 13 '08 #5
"TestDecimal"

it was just the method name that was wrong; this should have been
TestDouble; results stand "as is" though.

Basically, I started with decimal and then moved to double because
that was in your list; I forgot to rename.
Feb 13 '08 #6

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: Pierre Y. | last post by:
Hi, I've just read that article : http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp I'm asking something. Why are generics constraints not been implemented using...
16
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
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...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.