views:

279

answers:

4

If I write a generic class like class MyGeneric<T> is it possible to write an implicit cast to type T, so I can do stuff like:

public class MyGeneric<T>
{
...
}

public class GenericProperties
{
   public MyGeneric<string> MyGenericString {get;set;}

   public void UseMyGeneric()
   {
       string sTest = MyGenericString;
       MyGenericString = "this is a test";
   }
}

Is it possible to do that by overloading operators? I know it could be done if my class wasn't a generic...

+12  A: 

Well, yes, but for the love of zombie jesus do NOT do that. It's really confusing. You're slightly misunderstanding the purpose of generics, I think. It's not used to "turn" a class into that type, it's used to have that type (MyGenericString) be 'aware' of the type you want, for various purposes (typically those are collection-based purposes).

Noon Silk
+1 just for the "for the love of zombie jesus" LOL
Paulo Santos
I agree, this feature is useful, but the example given seems like a bad one and indicates a possible misunderstanding of the purpose of generics.
Paul Creasey
+11  A: 

yep..but don't over-do it, this tends to confuse people. i would only use it for wrapper types.

class Wrapper<T>
{
    public T Value {get; private set;}
    public Wrapper(T val) {Value = val;}

    public static implicit operator T(Wrapper<T> wrapper) {return wrapper.Value;}
    public static implicit operator Wrapper<T>(T val) {return new Wrapper<T>(val);}
}



var intWrapper = new Wrapper<int>(7);
var usingIt = 7 * intWrapper; //49

Wrapper<int> someWrapper = 9; //woohoo
dan
I've upvoted you, because it's technically correct (the best kind of correct), but really, I think it's almost never appropriate to do this. I hope the OP realises that.
Noon Silk
thanks. i upvoted you too, for zombie jesus, and because I agree.
dan
I'd love to see a practical use for this.
Hans Passant
@nobugz: `Nullable<T>` has an `explicit operator T`. And an implicit conversion *from* `T`. That's pretty practical!
Aaronaught
+1  A: 

Yes it's possible using implicit Conversion Operator Overloading

class Program
{
    static void Main(string[] args)
    {
        myclass<string> a = new myclass<string>();
        a.inner = "Hello";
        string b = a;
        Console.WriteLine(b);
    }
}

class myclass<T>
{
    public T inner;
    public myclass()
    {

    }
    public static implicit operator T(myclass<T> arg1)
    {
        return arg1.inner;
    }
}
Paul Creasey
+9  A: 

As others have said, that is legal but dangerous. There are many pitfalls you can fall into. For example, suppose you defined a user-defined conversion operator between C<T> and T. Then you say

C<object> c = new C<object>("hello");
object o = (object) c;

What happens? does your user-defined conversion run or not? No, because c is already an object.

Like I said, there are crazy situations you can get into when you try to define generic conversion operators; do not do it unless you have a deep and detailed understanding of section 10.10.3 of the specification.

Eric Lippert