Overloading = operator in C#

A

Alberto Bencivenni

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
 
A

Alun Harford

Alberto said:
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
 
J

Jon Skeet [C# MVP]

Alberto Bencivenni said:
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.)
 
A

Alberto Bencivenni

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
 
J

Jon Skeet [C# MVP]

Alberto Bencivenni said:
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 counterintuitive to other .NET
programmers, but it would also be expensive in terms of memory.
 
A

Alberto Bencivenni

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 counterintuitive 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
 
J

Jon Skeet [C# MVP]

Alberto Bencivenni said:
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top