![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | Re: Overloading = operator in C# No, basically. Marc |
My System Specs![]() |
| | #3 (permalink) |
| | 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. Alun Harford |
My System Specs![]() |
| | #4 (permalink) |
| | 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++) 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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. 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. 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 Specs![]() |
| | #7 (permalink) |
| | Re: Overloading = operator in C# Quote: > > How would composition with structs create more memory consumption? > 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. > object that cannot be simply assigned with = operator sounds weird to me. Thanks, Alberto |
My System Specs![]() |
| | #8 (permalink) |
| | 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 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. 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 Specs![]() |
![]() |
| 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 | |||