简体   繁体   English

为什么我不能从通用对象运行以下方法?

[英]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. 现在,我可以在两个intdouble或甚至是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.

相关问题 为什么我不能将此对象从视图传递到控制器方法? - Why I can't pass this object from a view to a controller method? 为什么我不能在 C# 中实现泛型方法? - Why Can't I implement generic method in C#? 为什么以下泛型方法调用不需要类型? - Why doesn't the following generic method call require a type? 为什么我不能将对象强制转换为约束泛型? - Why can't I cast an object to a constrained generic? 为什么不能从由相同泛型定义的参数推断出泛型类型作为委托方法的一部分? - Why can't the generic type be inferred from a parameter defined by the same generic as part of a delegate method? 我如何返回通用列表 <T> 从通用方法 <T> () - How can i return a generic List<T> from a generic method<T>() 可以在扩展方法中使用通用T吗? - Can I use generic T in an extension method? 为什么我不能声明通用列表 <T> 作为接口方法的返回类型? - Why can't I declare generic List<T> as return type for interface method? 为什么我不能替换IEnumerable <T> 通过扩展方法中的泛型变量? - Why can't I replace IEnumerable<T> by a generic type variable in extension method? 当我传递HashSet时,为什么我的泛型类型方法认为T是system.object - Why does my generic type method think T is system.object when I'm passing in a HashSet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM