Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Overloading = operator in C#

Reply
 
Old 09-15-2008   #1 (permalink)
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

My System SpecsSystem Spec
Old 09-15-2008   #2 (permalink)
Marc Gravell


 
 

Re: Overloading = operator in C#

No, basically.

Marc
My System SpecsSystem Spec
Old 09-15-2008   #3 (permalink)
Alun Harford


 
 

Re: Overloading = operator in C#

Alberto Bencivenni wrote:
Quote:

> 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
My System SpecsSystem Spec
Old 09-15-2008   #4 (permalink)
Jon Skeet [C# MVP]


 
 

Re: Overloading = operator in C#

Alberto Bencivenni <info@xxxxxx> wrote:
Quote:

> 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@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 09-16-2008   #5 (permalink)
Alberto Bencivenni


 
 

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
My System SpecsSystem Spec
Old 09-16-2008   #6 (permalink)
Jon Skeet [C# MVP]


 
 

Re: Overloading = operator in C#

Alberto Bencivenni <info@xxxxxx> wrote:
Quote:

> 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?
Quote:

> 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.

--
Jon Skeet - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 09-17-2008   #7 (permalink)
Alberto Bencivenni


 
 

Re: Overloading = operator in C#

Quote:

>
> 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?
Quote:

>
> 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
My System SpecsSystem Spec
Old 09-17-2008   #8 (permalink)
Jon Skeet [C# MVP]


 
 

Re: Overloading = operator in C#

Alberto Bencivenni <info@xxxxxx> wrote:
Quote:
Quote:

> > 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.
Quote:
Quote:

> > 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.
And an immutable class will do exactly that. The reference can be
assigned with the = operator with no problems at all.

--
Jon Skeet - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
-ne operator PowerShell
-f operator PowerShell
64 bit binary or (-bor operator)? PowerShell
What does the $() operator do? PowerShell
Suggestion: 1 new operator (well, maybe 6...) PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46