473,773 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making intention explicit - assignment operator

I do recognise a number of very appealing features of C#, in particular the
strong typing and the need in some contexts to be very explicit regarding
your intention. But the most common operator - assignment -seems to me a
right source of confusion and mistakes because it has two very different
meanings. Why not have one syntax for value assigment and a different
syntax for aliasing? With classes you might be permitted both types of
assigment if the target is not null. Hypothetical question I know but I'd
be interested in people's opinions.

Peter Seaman
Nov 15 '05 #1
5 2048
Peter,

Can you explain what you mean by "aliasing" exactly and how it compares
to "assignment "?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Seaman" <Peter MS Seaman at StableSoftware. com> wrote in message
news:el******** ******@TK2MSFTN GP10.phx.gbl...
I do recognise a number of very appealing features of C#, in particular the strong typing and the need in some contexts to be very explicit regarding
your intention. But the most common operator - assignment -seems to me a right source of confusion and mistakes because it has two very different
meanings. Why not have one syntax for value assigment and a different
syntax for aliasing? With classes you might be permitted both types of
assigment if the target is not null. Hypothetical question I know but I'd
be interested in people's opinions.

Peter Seaman

Nov 15 '05 #2
<"Peter Seaman" <Peter MS Seaman at StableSoftware. com>> wrote:
I do recognise a number of very appealing features of C#, in particular the
strong typing and the need in some contexts to be very explicit regarding
your intention. But the most common operator - assignment -seems to me a
right source of confusion and mistakes because it has two very different
meanings. Why not have one syntax for value assigment and a different
syntax for aliasing? With classes you might be permitted both types of
assigment if the target is not null. Hypothetical question I know but I'd
be interested in people's opinions.


What are the two very different meanings? If you're talking about the
difference between reference types and value types, the assignment
operator does the same in both cases - it sets the value of a variable
to the value of the expression on the right hand side. For reference
types, that value is a reference. Note that the value of a variable is
*never* a reference type object - it can only be a *reference* to an
object.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Hi Peter,

What you mean with aliasing in regard of classes, the only use of the =
symbol in aliasing is intented to be used for the using directive, like:
using [alias = ]class_or_namesp ace;

Is this what you mean?
If it's so I think that the meaning is clear for anybody seeing the code and
does not have any confusion if you are assigning or aliasing.
Merry X-Mas.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Seaman" <Peter MS Seaman at StableSoftware. com> wrote in message
news:el******** ******@TK2MSFTN GP10.phx.gbl...
I do recognise a number of very appealing features of C#, in particular the strong typing and the need in some contexts to be very explicit regarding
your intention. But the most common operator - assignment -seems to me a right source of confusion and mistakes because it has two very different
meanings. Why not have one syntax for value assigment and a different
syntax for aliasing? With classes you might be permitted both types of
assigment if the target is not null. Hypothetical question I know but I'd
be interested in people's opinions.

Peter Seaman

Nov 15 '05 #4
Jon,

Thanks for your reply and I understand and accept what you say.

To answer the questions asked by the other contributers before Xmas:

This is what prompted the question. In C++ structures and classes are very
similar in both syntax and semantics. Assignment of structures assigns the
values of the data members, and the same applies to classes. In C++, if A
and B are classes then A=B does not make A an alias of B - subsequent
changes to B have no effect on A. But in C# A does become an alias of B
i.e. subsequent changes to B also change A. So in C#, unlike C++, the
assignment operator operates very differently if A and B are classes to the
way it operates if A and B are structures.

Although I accept Jon's explanation I do retain a slight reservation in that
in C# we have reference types and value types and suggest that it can be
confusing to mix the terminology and talk about the "value" of reference
types.

Peter Seaman


Nov 15 '05 #5
<"Peter Seaman" <Peter MS Seaman at StableSoftware. com>> wrote:
Thanks for your reply and I understand and accept what you say.

To answer the questions asked by the other contributers before Xmas:

This is what prompted the question. In C++ structures and classes are very
similar in both syntax and semantics. Assignment of structures assigns the
values of the data members, and the same applies to classes. In C++, if A
and B are classes then A=B does not make A an alias of B - subsequent
changes to B have no effect on A. But in C# A does become an alias of B
i.e. subsequent changes to B also change A.
No, subsequent changes to B itself don't change A in the slightest.
Subsequent changes to the object B's value refers to will be visible
through the reference value of A, but that's just the same as using
pointers in C++.
So in C#, unlike C++, the
assignment operator operates very differently if A and B are classes to the
way it operates if A and B are structures.
No it doesn't. It doesn't behave differently at all. It assigns the
value of the expression to the variable. The difference in C++ is that
the value of a variable can be an object - that's not possible in C#.
Values of variables can only either be null, a reference, or a value
type.
Although I accept Jon's explanation I do retain a slight reservation in that
in C# we have reference types and value types and suggest that it can be
confusing to mix the terminology and talk about the "value" of reference
types.


On the contrary - it's more confusing to talk about "aliasing" A and B
when the variables themselves are entirely unrelated except they happen
to have the same value. What's important is that the value is a
reference. Using the terminology that the value of a variable can be a
reference is accurate and is what is used throughout proper
documentation. Furthermore, it makes parameter passing simple to
explain.

Once you understand the difference between a variable and the object it
refers to, it's all very simple. There's only one type of assignment to
understand, garbage collection is understandable, etc. Changing the
assignment operator would be an awful substitute for understanding what
reference types are, IMO.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
17879
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
5
4834
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I missing something here and there is something I need to learn about? Clear, easy to understand...
16
2619
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
6
13385
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
85
3296
by: masood.iqbal | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am talking about the non-OO stuff, that seems to be handled much more elegantly in C++, as compared to C. For example new & delete, references, consts, declaring variables just before use etc. I am asking this question with a vested interest. I...
351
13130
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and do) occur in any language. These assumptions are generally made because of familiarity with the language. As a non-code example, consider the idea that the faulty code is written by blackguards bent on foulling the language. The term...
5
2293
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
4
3380
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
50
4514
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.