Making intention explicit - assignment operator

  • Thread starter Thread starter Peter Seaman
  • Start date Start date
P

Peter Seaman

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
 
Peter,

Can you explain what you mean by "aliasing" exactly and how it compares
to "assignment"?
 
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.
 
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_namespace;

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