[英]Why can't I run the following method from a generic object?
I am (trying) to learn about generics and I thought I understood them. 我(尝试)了解仿制药,我认为我理解它们。 I have the following code for the generic class:
我有以下泛型类的代码:
/// <summary>
/// generics
/// </summary>
class Point<T> where T : IConvertible
{
public T X;
NumberFormatInfo nf = NumberFormatInfo.CurrentInfo;
public double Subtract(Point<T> pt)
{
return (X.ToDouble(nf) + pt.X.ToDouble(nf));
}
}
and in the Main()
, I can create the objects fine: 在
Main()
,我可以创建对象:
Point<int> pt = new Point<int>();
Point<double> pt2 = new Point<double>();
Point<string> pt3 = new Point<string>();
pt.X = 10;
pt2.X = 10;
pt3.X = "10";
Now, I can run the Subtract
Method on two int
s, double
s or even string
s, but I can't run on mixed, which I thought I would be able to because of the .ToDouble
conversion. 现在,我可以在两个
int
, double
或甚至是string
s上运行Subtract
方法,但是我无法在mixed上运行,我认为我可以通过.ToDouble
转换运行。
If I try to run Console.WriteLine(pt.Subtract(pt2));
如果我尝试运行
Console.WriteLine(pt.Subtract(pt2));
I get the following errors: 我收到以下错误:
Error 1 The best overloaded method match for Date.Point<int>.Subtract(Date.Point<int>)' has some invalid arguments
Error 2 Argument 1: cannot convert from 'Date.Point<double>' to 'Date.Point<int>'
The code itself is not so important as I am simply trying to understand/learn generics, so I would just like to understand what is wrong here and why it wouldn't work.... The actual method is not that important/will not be used. 代码本身并不是那么重要,因为我只是想了解/学习泛型,所以我只想了解这里有什么问题以及为什么它不起作用......实际的方法并不重要/不会使用。
Your Subtract
method takes a Point<T>
. 您的
Subtract
方法采用Point<T>
。
T
here is the type parameter of your class, which means that it can only take a Point
of the same type. T
这里是你的类,这意味着它只能采取的类型参数Point
同一类型的。
You need to give Subtract
a separate generic parameter so that it can take a Point<U>
for any U
. 您需要为
Subtract
一个单独的泛型参数,以便它可以为任何U
取一个Point<U>
。
The type T
must be the same in the class declaration and the method: 类声明和方法中的类型
T
必须相同:
class Point<T> where T : IConvertible
{
public double Subtract(Point<T> pt)
You could introduce a second generic parameter: 您可以引入第二个通用参数:
class Point<T> where T : IConvertible
{
public double Subtract<T2>(Point<T2> pt) where T2 : IConvertible
Your method signature: 你的方法签名:
public double Subtract(Point<T> pt)
Asks for a Point
of generic type T
, which matches the generic type in your class definition: 要求一个
Point
泛型类型的T
,这在你的类定义相匹配的泛型类型:
class Point<T> where T : IConvertible
That means it's asking for the same type. 这意味着它要求相同的类型。
You can specify a different generic type to allow it to mix different types: 您可以指定不同的泛型类型以允许它混合使用不同的类型:
public double Subtract<U>(Point<U> pt) where U : IConvertible
{
return (X.ToDouble(nf) + pt.X.ToDouble(nf));
}
Note, however, that since the actual type of U
can be inferred from the Point<U>
argument you pass, you can call your method in the same way without needing to specify the type: 但请注意,由于可以从传递的
Point<U>
参数推断出U
的实际类型,因此可以以相同的方式调用方法,而无需指定类型:
Console.WriteLine(pt.Subtract(pt2)); // As opposed to pt.Subtract<double>(pt2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.