Click here to Skip to main content
15,887,833 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create a generic class having a variable of the same generic type inside:

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



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
Posted
Comments
Andreas Gieriet 26-Feb-13 12:20pm    
What error?
The code above does not compile since the assignment is not in a method.
Please clarify and fix the example. In addition, is the T1 class a concrete type or a generic parameter? Please give the full example code. See also my solution #1 below.
Andi
serguru604 26-Feb-13 12:49pm    
Hello Andi,
Thank you for your response. Actually the only thing I am trying to find out is: how to declare a variable inside a class MyClass<t> of the same type MyClass<t> but with a different generic parameter, i.e. not T but T1.

In your example you declared a variable T Value. And how to declare a variable MyClass<t1> Value?

Thank you.
Sergey
Andreas Gieriet 26-Feb-13 17:21pm    
What is the concrete design you want to have? I.e. what do you expect to happen? You can not directly do that.
Andi

This code compiles:
C#
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;
    }

}


[EDIT]

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.:
C#
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);

So, you pass the type from outside.

[/EDIT]

Cheers
Andi
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 26-Feb-13 13:59pm    
Andi,

I voted 4 this time, because Func and OtherFunc should be static, if you look at their bodies. Having them as instance functions won't cause any problem, it's just a redundancy, because "this" is not used. For example, FxCop will give you a performance warning.

Besides, there is more to it. Please see my answer.

—SA
Andreas Gieriet 26-Feb-13 17:12pm    
Hello Sergey,
Thanks for your 4. I thought of it while I posted it but did not have a focus on that. You can assume some ... in the function body ;-).
Cheers
Andi
Sergey Alexandrovich Kryukov 26-Feb-13 17:17pm    
Yes I can, but that needs some explanation. You see, improper choice of static vs. instance is one of the usual beginner's problems; I'm sure you do understand the matter, but we should take more care when we answer questions, to avoid teaching wrong techniques. Minor problem, anyway...
—SA
Andreas Gieriet 26-Feb-13 17:43pm    
Updated.
Cheers
Andi
Sergey Alexandrovich Kryukov 26-Feb-13 17:49pm    
You apparently are not satisfied with my 4, and I was picky enough. Well, I think both things only worked out for common good :-). My 5 now!
—SA
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:
C#
    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:
C#
class MyClass {
    MyClass myInstance = new MyClass(); // DON'T DO IT, will cause infinite recursion!
    // ...
}


Do I even have to explain why? If it's not clear, just think just a bit.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900