By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

generic class with numeric type?

P: n/a
Is that not allowed to assume generic type numeric type? As far as I've
tried, I got an error with the following code:

public class AAA<T>
{
public int Foo(T a)
{
return a; // error: Cannot implicitly convert type 'T' to 'int'
}
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

where my intention is to use AAA<on numeric value only, which is
acceptable in C++.

How can I resolve that compilation error? Please reply.
Thanks in advance.

Hyun-jik Bae
Oct 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hyun-jik Bae wrote:
Is that not allowed to assume generic type numeric type? As far as
I've tried, I got an error with the following code:

public class AAA<T>
{
public int Foo(T a)
{
return a; // error: Cannot implicitly convert type 'T' to
'int' }
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

where my intention is to use AAA<on numeric value only, which is
acceptable in C++.

How can I resolve that compilation error? Please reply.
Since .NET generics are fully compiled and represented in the IL, in order
for that IL to be verifiable (provably safe), there are limitations on what
..NET generics can do - they are severely limited compared to C++ templates
(and also have significant advantages compared to C++ templates in some
cases).

To use a type as a generic type parameter, when you write the generic class,
you specify constraints on the generic type parameters. These constraints
actually increase what you can do with the type. There are only two types
of constraints: a constructor contraint, written as new(), and an interface
constraint.

Unfortunately, there's no "IConvertsToInt" interface defined by the .NET
framework, and defining such an interface yourself would be unfulfilling at
best.

Your best bet in the example you supplied is probably to use the Convert
class

public class AAA<T>
{
public int Foo(T a)
{
return Convert.ToInt32(a);
}
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

Unfortunately, this will result in 'a' being boxed onto the managed heap,
since Convert.ToIn32 takes a parameter of type System.Object. But it will
work.

Doing arithmetic - even simple comparisions - with generic parameters is
hard. There are a couple of artibles on code project that explain the
limitations and what you can do to work around them.

Here's one of them - there's at least one other good one, but I couldn't
find it with a quick search.

http://www.codeproject.com/csharp/genericnumerics.asp

-cd

Oct 23 '06 #2

P: n/a
Hyun-jik Bae schrieb:
Is that not allowed to assume generic type numeric type? As far as I've
tried, I got an error with the following code:

public class AAA<T>
{
public int Foo(T a)
{
return a; // error: Cannot implicitly convert type 'T' to 'int'
}
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

where my intention is to use AAA<on numeric value only, which is
acceptable in C++.

How can I resolve that compilation error? Please reply.
Thanks in advance.

Hyun-jik Bae
you can specify what type the generic type should be.

public class AAA<T>
where T: int
{
public int Foo(T a)
{
return Convert.ToInt32(a);
}
}

have a look to "where T ....". then you can do something like

public T Foo(T a)
{
return a;
}

or

public int Foo(T a)
{
return (int)a;
}

depends on what the method in praxis should do.
hope this helps.

cheers,
marc

Oct 23 '06 #3

P: n/a
"Marc" <ma**@idev.cha écrit dans le message de news:
11**********************@e3g2000cwe.googlegroups.c om...

| you can specify what type the generic type should be.
|
| public class AAA<T>
| where T: int
| {
| public int Foo(T a)
| {
| return Convert.ToInt32(a);
| }
| }

This is not possible, you really need to check suggestions before posting
answers :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 23 '06 #4

P: n/a

Joanna Carter [TeamB] schrieb:
This is not possible, you really need to check suggestions before posting
answers :-)
yes, you are right.
just wanna point to the "where" keyword for generics where one can
limit the types that can be used for a generic. this could help here
but it depends on the concrete implementation of the methods and their
results. i mean, does it always need to be an int or just the same as
the generic type?

cheers,
marc

Oct 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.