Microsoft Developer Network > Forums Home > Visual C# Forums > Visual C# Language > C# generics on conversion operator and constructor
Ask a questionAsk a question
 

AnswerC# generics on conversion operator and constructor

  • Monday, November 21, 2005 12:10 AMBrian Kramer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have so far a class Expr with a constructor taking a string, and the following two conversion operators.

    public static implicit operator Expr(int expr)
    {
      
    return new Expr(expr.ToString());
    }

    public static implicit operator Expr(string expr)
    {
      
    return new Expr(expr);
    }

    I have various others, but they all get distilled down to a string using .ToString().  So, the obvious question: How would i generalize this with generics?  The following doesn't work because the C# parser interprets Expr<T> (a generic type) instead of (operator Expr)<T> (a generics parameter on the method operator Expr.

    public static implicit operator Expr<T>(T expr)
    {
      return new Expr(expr.ToString());
    }

    Similarly, is there way to specify a generic constructor that takes on arbitrary paramters?

    public Expr<T>(T expr)
    {
      data = expr.ToString();
    }

Answers

  • Tuesday, December 27, 2005 9:58 PMMads Torgersen - MSFTOwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Brian,

     

    From the Spec:

    Properties, events, indexers, operators, constructors, and destructors cannot themselves have type parameters. However, they can occur in generic types and use the type parameters from an enclosing type.

     

    So no generic operators! Which also means, you cannot use generics for your purpose. Moreover, when you think about it, what you are trying to do is probably not a good thing anyway: You are introducing an implicit conversion from *any* type to your Expr type. That is bound to be harmful.

     

    As for the constructor, why do you want it to be generic? What's wrong with taking object?

All Replies

  • Tuesday, December 27, 2005 9:58 PMMads Torgersen - MSFTOwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Brian,

     

    From the Spec:

    Properties, events, indexers, operators, constructors, and destructors cannot themselves have type parameters. However, they can occur in generic types and use the type parameters from an enclosing type.

     

    So no generic operators! Which also means, you cannot use generics for your purpose. Moreover, when you think about it, what you are trying to do is probably not a good thing anyway: You are introducing an implicit conversion from *any* type to your Expr type. That is bound to be harmful.

     

    As for the constructor, why do you want it to be generic? What's wrong with taking object?

  • Wednesday, January 04, 2006 6:19 PMBrian Kramer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    That makes sense, thanks.  My question was of exploratory nature in a project that is using C# as a "scripting language."

    Brian