Developing generic - wasting time?..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bliss

    #1

    Developing generic - wasting time?..

    I have some problems with my generic.

    Part of code:
    using System;
    using System.Collecti ons.Generic;
    using System.Text;

    namespace Gnrc
    {
    public class Gnrc<T>
    {

    //[some statements]

    protected void SomeMethod()
    {
    T A, B, C;

    //[some statements]

    C = A + B; // CS0019: Operator '+' cannot be applied to operands of type 'T' and 'T'

    //[some statements]
    }
    }
    }

    Is there available some "tricks" to bypass this error and compile ???
    Of course, operator '+' is defined on 'T' type.

    Best regards, Bliss.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Developing generic - wasting time?..

    Bliss,

    It might be defined on the type, but the compiler doesn't know it, and
    can't apply it because the constraint system doesn't allow it.

    Rather, you have to define your operations in an interface, and then
    mark that as part of the constraint. Then, you can cast to your interface,
    and perform operations on that.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Bliss" <kdemin@lipetsk .ruwrote in message
    news:ed2P4dlGIH A.5752@TK2MSFTN GP02.phx.gbl...
    >I have some problems with my generic.
    >
    Part of code:
    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    >
    namespace Gnrc
    {
    public class Gnrc<T>
    {
    >
    //[some statements]
    >
    protected void SomeMethod()
    {
    T A, B, C;
    >
    //[some statements]
    >
    C = A + B; // CS0019: Operator '+' cannot be applied to operands of type
    'T' and 'T'
    >
    //[some statements]
    }
    }
    }
    >
    Is there available some "tricks" to bypass this error and compile ???
    Of course, operator '+' is defined on 'T' type.
    >
    Best regards, Bliss.

    Comment

    • Peter Duniho

      #3
      Re: Developing generic - wasting time?..

      On 2007-10-29 11:13:22 -0700, Bliss <kdemin@lipetsk .rusaid:
      [...]
      Is there available some "tricks" to bypass this error and compile ???
      Of course, operator '+' is defined on 'T' type.
      Of course. :) But that doesn't mean that the generic class has any
      way to represent that.

      How much control do you have over the type used as T? If you can
      modify the type, then one approach would be to define an interface that
      requires the operator+ to be implemented, and then constrain T in the
      generic to types that implement that interface.

      If you can't create a type that allows for a suitable constraint in the
      generic, then I don't believe you'll be able to do what you want.

      Caveat: I haven't actually tried doing this. For all I know, operators
      are a special case and can't be declared in an interface. If that's
      true, then you may have to use a constraint that requires inheritance
      of a specific class that implements the operator+ and of course make
      sure your T type used inherits that class.

      Pete

      Comment

      • Rene

        #4
        Re: Developing generic - wasting time?..

        This may help you:

        http://blogs.msdn.com/ericgu/archive.../14/52852.aspx


        Comment

        • Bliss

          #5
          Re: Developing generic - wasting time?..

          Rene ïèøåò:Nice blog. =) I think that this topic may be closed.

          Best regards to all people who was seen there.

          Comment

          Working...