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

confusion about generics

I've read several documents about upcoming C# generics and I still cannot
understand one thing.

Would it be valid to write a code like this:

class SomeClass
{
public void AMethod<T>(T a, T b)
{
T c = a + b; // ?
...
}
}

since + is defined for some simple types and can be overloaded for reference
types, then what kind of constraint should be used to allow the + in above
context?

or would it be forbidden to use standard binary operators in generic code?
(in fact, it would require to have the interface, IAddable, make all numeric
types implement the interface and then add "where T : IAddable" to the above
code)

if it is forbidden to use + in above context, then how will I create a
generic class that acts like this one above? (suppose I need to call the
method AMethod with any class that supports + operation, including <int>
<double> and other numeric types).

I would really like to hear from an expert on this issue.
Thanks in advance.

Wiktor Zychla
Nov 15 '05 #1
2 2699
Wiktor,

The constraint mechanism doesn't define operators on the type as a
constraint. Because of this, you will have to get around it in some other
way. Like you specified, an interface which indicates that the operation
could be performed would be the best way to go.

Generally speaking, I think that using an operation for a constraint
would be a bad idea anyways, since not all .NET languages support operator
overloads (VB for example) and you would limit generics on those platforms.
However, interfaces are accessible in all languages (or at least, more
accessible than overloaded operators).

Because of this, you should have a constraint on an IAddable interface
and then use the Add method on that interface (I am guessing this would be
there).

But, this raises the problem of what do you do for already defined
classes which expose this functionality and can not be changed to implement
IAddable? In this case, you will have to check the type of T, and then find
the static method that corresponds to the operator overload (op_Addition).
If it exists, then you can call that through Reflection. I know it is
painful, but it is the only way with the way generics are currently
proposed.

Hope this helps.

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

"Wiktor Zychla" <ie****@microsoft.com.no.spam> wrote in message
news:e8**************@TK2MSFTNGP12.phx.gbl...
I've read several documents about upcoming C# generics and I still cannot
understand one thing.

Would it be valid to write a code like this:

class SomeClass
{
public void AMethod<T>(T a, T b)
{
T c = a + b; // ?
...
}
}

since + is defined for some simple types and can be overloaded for reference types, then what kind of constraint should be used to allow the + in above
context?

or would it be forbidden to use standard binary operators in generic code?
(in fact, it would require to have the interface, IAddable, make all numeric types implement the interface and then add "where T : IAddable" to the above code)

if it is forbidden to use + in above context, then how will I create a
generic class that acts like this one above? (suppose I need to call the
method AMethod with any class that supports + operation, including <int>
<double> and other numeric types).

I would really like to hear from an expert on this issue.
Thanks in advance.

Wiktor Zychla

Nov 15 '05 #2
> But, this raises the problem of what do you do for already defined
classes which expose this functionality and can not be changed to implement IAddable? In this case, you will have to check the type of T, and then

find

this is exactly what I wanted to hear. as usual, thanks Nicholas.
regards, Wiktor
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
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...
23
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...
12
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...
1
by: Epetruk | last post by:
Hello, I have a solution with two projects. One of the projects is called MyProj with a root namespace called MyProj.Obj. The single source (vb) file for MyProj has a class called Obj....
9
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...
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...
13
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...
2
by: joseph_gallagher | last post by:
Hi, I've been playing with generics and I was wondering if anyone could tell me why the following doesnt work, and if there is another way to do it public class A<Twhere T : B, new() {...
1
by: Peter | last post by:
Hi I'm trying to call a C# Method from with VB.NET and am having all sorts of troubles as I'm new to Generics. The C# method (which I didn't develop) is as follows: /// <summary> ///...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
0
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...
0
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...

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.