473,473 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to costraint a generics to be a number

Hi.

I want to costraint a generics T to be a number (Int32, Double, Byte etc...)
but
how I can do?

I need beacuse I want to use arithmetic operators (+, -...).

Thx! ;-)

--
Matteo Migliore.
Blog: http://blogs.ugidotnet.org/matteomigliore

Nov 3 '07 #1
6 1168
Matteo,

You can't, at least not at compile time using constraints. You could
perform a check against the type parameter at run time in the static
constructor for the type and see if it is Int32, Double, etc, etc.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matteo Migliore" <ma*****************@gmail.comwrote in message
news:ug*************@TK2MSFTNGP02.phx.gbl...
Hi.

I want to costraint a generics T to be a number (Int32, Double, Byte
etc...) but
how I can do?

I need beacuse I want to use arithmetic operators (+, -...).

Thx! ;-)

--
Matteo Migliore.
Blog: http://blogs.ugidotnet.org/matteomigliore
Nov 3 '07 #2
Matteo Migliore wrote:
I want to costraint a generics T to be a number (Int32, Double, Byte
etc...) but
how I can do?

I need beacuse I want to use arithmetic operators (+, -...).
You can't.

Many people have wished so, but it is not possible.

Try read:
http://www.codeproject.com/csharp/genericnumerics.asp
for an example.

Arne
Nov 3 '07 #3
Try read:
http://www.codeproject.com/csharp/genericnumerics.asp
for an example.
Thanks all! :-)

I just reader the article on CodeProject. The possibile
solution is that :-).

Thx again! ;-)

--
Matteo Migliore.
Blog: http://blogs.ugidotnet.org/matteomigliore
Nov 3 '07 #4
I use "where T: IComparable<T>" for this. It seems to work okay. Odds
are you needed it constrained to be a number because you need to use
less than, greater than, or equals on it. All of those are easily
possible with the compare interface.

Nov 4 '07 #5
"Matteo Migliore" <ma*****************@gmail.comwrote
Hi.

I want to costraint a generics T to be a number (Int32, Double, Byte
etc...) but
how I can do?

I need beacuse I want to use arithmetic operators (+, -...).
You might be able to use extension methods in .Net 3.5. Somthing like this:

public static int Sum(this MyClass<intlist)
{
int sum = 0;
foreach (int i in list)
sum += i;
return sum;
}

And then use it like this:

MyClass<inta;
int total = a.Sum();
Peter
Nov 4 '07 #6
You might be able to use extension methods in .Net 3.5. Somthing like
this:
Thank you for support :-). I know that is possible in .NET 3.x but
I needed to solve problem writing a class.

I solved thanks again! ;-)

--
Matteo Migliore.
Blog: http://blogs.ugidotnet.org/matteomigliore
Nov 4 '07 #7

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

Similar topics

7
by: hyppos | last post by:
Hi, I've a XML Schema with this user Type <xsd:simpleType name="ResultArrayType"> <xsd:restriction base="xsd:nonNegativeInteger"> <xsd:maxExclusive value="1000"/> </xsd:restriction>...
8
by: Jon Slaughter | last post by:
I'm reading Alexandrescu's Modern C++ design about the concept of policies. What I'm wondering is if policies are a common design pattern in C++(OOP) nowdays? It seems like a good idea but I'm...
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...
9
by: Chuck Bowling | last post by:
I assume that 2.0 will be rolled out with VS.NET 2004. Does anybody know if MS is planning to ship an STL package with it?
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...
4
by: Michael Sparks | last post by:
I started writing some code with the 2.0 beta compiler, and found out quickly that specialization is not supported with generics. For example, I want to do something like this: class number...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.