Generics problem
-
I'm still learning about generics so I create a generics function like this :
generic<class T> where T : ValueType
T Process(T val)
{
val = val * 15;
return val;
}and called
float a = Process<float>(10.0)
, but then I got " error C2296: '*' : illegal, left operand has type 'T' " in the lineval = val * 15;
. Then I try a simpler method like this :generic<class T> where T : ValueType
T Process()
{
T val = 15;
return val;
}Called
float a = Process<float>()
and I still got "error C2440: 'initializing' : cannot convert from 'int' to 'T' " in the lineT val = 15;
What is the correct way to use generics? Thanks in advance -
I'm still learning about generics so I create a generics function like this :
generic<class T> where T : ValueType
T Process(T val)
{
val = val * 15;
return val;
}and called
float a = Process<float>(10.0)
, but then I got " error C2296: '*' : illegal, left operand has type 'T' " in the lineval = val * 15;
. Then I try a simpler method like this :generic<class T> where T : ValueType
T Process()
{
T val = 15;
return val;
}Called
float a = Process<float>()
and I still got "error C2440: 'initializing' : cannot convert from 'int' to 'T' " in the lineT val = 15;
What is the correct way to use generics? Thanks in advance -
I'm still learning about generics so I create a generics function like this :
generic<class T> where T : ValueType
T Process(T val)
{
val = val * 15;
return val;
}and called
float a = Process<float>(10.0)
, but then I got " error C2296: '*' : illegal, left operand has type 'T' " in the lineval = val * 15;
. Then I try a simpler method like this :generic<class T> where T : ValueType
T Process()
{
T val = 15;
return val;
}Called
float a = Process<float>()
and I still got "error C2440: 'initializing' : cannot convert from 'int' to 'T' " in the lineT val = 15;
What is the correct way to use generics? Thanks in advance -
The problem in your code is that you cannot convert an
int
to aValueType
.T
is aValueYype
, so you can use only the methods defined in that class when using objects of typeT
. What are you trying to achieve?Thanks for your reply, Actually I want to make a 2D matrix class that doesn't just limited in one type (ValueType) only so that I can create a
Matrix<float>
orMatrix<int>
depending on what I need. I thought that generic is the most appropriate and also the simplest solution but it turns out that I was wrong. -
Thanks for your reply, Actually I want to make a 2D matrix class that doesn't just limited in one type (ValueType) only so that I can create a
Matrix<float>
orMatrix<int>
depending on what I need. I thought that generic is the most appropriate and also the simplest solution but it turns out that I was wrong.Unfortunately C# generics are not C++ generics, C# doesn't support numeric generics because there is no common base class of int/float/whatever that support calculation operations. You can do it the old manner, creating a class for every type you wish to support. There is another solution that uses generics in a different manner here[^].