Overloading = operator in C#

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

    Overloading = operator in C#

    Hi All,


    I know there is no way to overload the = operator in C# for a class
    object: you can only get something similar using the (myClass)
    myClass.Clone() ; method or creating a copy constructor.

    The question is: Does a more elegant way to do this exist? (I am
    thinking to how elegant is the operator = overloading in standard C++)

    Thanks,

    Alberto
  • Marc Gravell

    #2
    Re: Overloading = operator in C#

    No, basically.

    Marc

    Comment

    • Alun Harford

      #3
      Re: Overloading = operator in C#

      Alberto Bencivenni wrote:
      Hi All,
      >
      >
      I know there is no way to overload the = operator in C# for a class
      object: you can only get something similar using the (myClass)
      myClass.Clone() ; method or creating a copy constructor.
      It sounds like you want value-type semantics. If so, you want a struct.

      Alun Harford

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: =?ISO-8859-1?Q?Overloading _=3D_operator_i n_C#?=

        Alberto Bencivenni <info@devdept.c omwrote:
        I know there is no way to overload the = operator in C# for a class
        object: you can only get something similar using the (myClass)
        myClass.Clone() ; method or creating a copy constructor.
        >
        The question is: Does a more elegant way to do this exist? (I am
        thinking to how elegant is the operator = overloading in standard C++)
        You've described how you'd achieve some effect in C++, but not really
        stated what the goal is. *Why* do you want to overload the = operator?
        What are the benefits? (Think particularly carefully of the
        readability, especially when C# developers are used to reference
        assignment semantics.)

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • Alberto Bencivenni

          #5
          Re: Overloading = operator in C#

          Hi All,

          thanks for your answers.

          We currently use a Point struct but need to extend it someway and
          composition is not an option (in terms of memory comsumption), so we
          need a class.

          By the way classes don't allow easy = operator and for a Point
          paradigm is a big lack.

          Let me know what you think.

          Thanks,

          Alberto

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: =?ISO-8859-1?Q?Overloading _=3D_operator_i n_C#?=

            Alberto Bencivenni <info@devdept.c omwrote:
            thanks for your answers.
            >
            We currently use a Point struct but need to extend it someway and
            composition is not an option (in terms of memory comsumption), so we
            need a class.
            How would composition with structs create more memory consumption?
            By the way classes don't allow easy = operator and for a Point
            paradigm is a big lack.
            >
            Let me know what you think.
            I think you'd be a lot better off by making your type immutable, with
            methods which produce a new instance with a changed value - the same
            sort of design as System.String.

            Copying the *contents* every time you used the assignment operator
            would not only be completely counterintuitiv e to other .NET
            programmers, but it would also be expensive in terms of memory.

            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            • Alberto Bencivenni

              #7
              Re: Overloading = operator in C#

              >
              How would composition with structs create more memory consumption?
              >
              Suppose you have a Point(x,y) but in a few cases you need
              Point(x,y,r,g,b ) and you have an array of 1,000,000 points. Dows it
              worth to make a struct that has 5 members?
              >
              Copying the *contents* every time you used the assignment operator
              would not only be completely counterintuitiv e to other .NET
              programmers, but it would also be expensive in terms of memory.
              >
              I completely agree with your thought, although thinking about a Point
              object that cannot be simply assigned with = operator sounds weird to
              me.

              Thanks,

              Alberto

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: =?ISO-8859-1?Q?Overloading _=3D_operator_i n_C#?=

                Alberto Bencivenni <info@devdept.c omwrote:
                How would composition with structs create more memory consumption?
                >
                Suppose you have a Point(x,y) but in a few cases you need
                Point(x,y,r,g,b ) and you have an array of 1,000,000 points. Dows it
                worth to make a struct that has 5 members?
                I'd have three structs: Point, Color, and ColoredPoint. The idea that
                I'd have a million points and *some* would be coloured but some
                wouldn't is of course feasible, but it's pretty rare. In that
                particular case it does make sense to use classes instead - but I'd
                consider whether an alternative data structure overlaying the whole
                thing might not make even more sense. Obviously you know more about
                your situation than I do - I'm just saying that when you have unusual
                requirements, it can be worth trying to think of other ways of looking
                at the problem.
                Copying the *contents* every time you used the assignment operator
                would not only be completely counterintuitiv e to other .NET
                programmers, but it would also be expensive in terms of memory.
                >
                I completely agree with your thought, although thinking about a Point
                object that cannot be simply assigned with = operator sounds weird to
                me.
                And an immutable class will do exactly that. The reference can be
                assigned with the = operator with no problems at all.

                --
                Jon Skeet - <skeet@pobox.co m>
                Web site: http://www.pobox.com/~skeet
                Blog: http://www.msmvps.com/jon.skeet
                C# in Depth: http://csharpindepth.com

                Comment

                Working...