内部具有相同类的泛型类 [英] Generic class with the same class inside

查看:71
本文介绍了内部具有相同类的泛型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有相同泛型类型的变量的泛型类:



I am trying to create a generic class having a variable of the same generic type inside:

public class MyClass<T>
{
    MyClass<T1> a = new MyClass<T1>();
}







想法是有一个相同的变量泛型类型和不同的参数类型T1。由于编译错误,我无法做到这一点。



有什么想法吗?谢谢。



Sergey




The idea is to have a variable of the same generic type and with a different parameter type T1. And I cannot do that because of a compilation error.

Any ideas? Thank you.

Sergey

推荐答案

此代码编译:
class MyClass<T>
{
    public T Value { get; private set; }
    public MyClass(T value)
    {
        Value = value;
    }

    public static int Func(int x)
    {
        MyClass<int> inst = new MyClass<int>(x);
        ...
        return inst.Value;
    }

    public static U OtherFunc<U>(U x)
    {
        MyClass<U> inst = new MyClass<U>(x);
        ...
        return inst.Value;
    }

}









您的问题有一个解决方案。您将MyClass定义为具有两种泛型类型。例如。第二个是您的字段类型(下面命名为V)。然后,您可以传递任何满足约束的类型( where 子句)。例如:





There is a solution to your question. You define MyClass to have two generic types. E.g. The second one is for your field type (below named V). You may then pass any type that fulfils the constraint (the where clause). e.g.:

public class MyClass<U, V>
    where V: new()
{
    // note: public only to show the effect here, otherwise use private for fields
    public U _uField = default(U); // could also leave away: "= default(U)"
    public V _vField = new V();    // only parameter less constructor supported
    ...
}
...
MyClass<string, int> obj1 = new MyClass<string, int>();
Console.WriteLine(obj._uField);
Console.WriteLine(obj._vField);
...
MyClass<int, MyClass<string, bool>> obj2 = new MyClass<int, MyClass<string, bool>>();
Console.WriteLine(obj2._uField);
Console.WriteLine(obj2._vField);



所以,你从外面传递类型。



[/编辑]



干杯

Andi


So, you pass the type from outside.

[/EDIT]

Cheers
Andi


谢尔盖,



看起来,在这个时刻,你的脑袋在仿制药方面非常好。否则你不会写这个 T1 。这是一种未定义的类型。相反, T 是泛型类型。它实际上意味着非常完整类型最终将被替换而不是泛型类型参数。如果你的意思是 T 而不是 T1 ,你应该这样写,如果这是另一种类型,你应该已将其添加为类 MyClass< T,T1>中的通用参数{/ * ... * /}



现在,如果同一个类是常用的话,使用变量;请参阅解决方案1.但是,您需要大警告关于您尝试做什么。



这是语法允许的,但< b>将导致无限递归:

Sergey,

It looks like, at this moment, your head is messes up very well where it comes to generics. Otherwise you would not write this T1. This is a type which is not defined anywhere. In contrast, T is a generic type. It actually means "that very complete type which will ultimately be substituted instead of the generic type parameter". If you meant T instead T1, you should have written so, if this is meant to be another type, you should have added it as a generic parameter in class MyClass<T, T1> { /* ... */ }.

Now, using the variable if the same class is a very usual thing; please see Solution 1. However, you need a big warning about what you tried to do.

This is allowed by syntax, but will cause infinite recursion:
    class MyClass<t> {
        MyClass<t> myInstance = new MyClass<t>(); // DON'T DO IT, will cause infinite recursion!
        // ...
    }
</t></t></t>





它与泛型无关;没有这个问题你会遇到完全相同的问题:



And it has nothing to do with generics; you would have exact same problem without then:

class MyClass {
    MyClass myInstance = new MyClass(); // DON'T DO IT, will cause infinite recursion!
    // ...
}





我甚至要解释原因吗?如果不清楚的话,只需考虑一下。



-SA


这篇关于内部具有相同类的泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆