M
M
Michael2016-02-09 13:38:28
.NET
Michael, 2016-02-09 13:38:28

Fragment of the C# book. How can that be?

Value Types, Reference Types, and the Assignment Operation
When one value type is assigned to another, a member-wise copy of the
data fields is obtained. In the case of a simple data type such as System.Int32, the only
copyable member is a numeric value. However, in a Point situation,
the X and Y values ​​will be copied into the new structure variable. To illustrate,
let's create a new console application project named ValueAndReferenceTypes and
copy the previous definition of Point into the new namespace. We then add
the following method to the Program type:
// Assigning two internal value types // results in
two independent variables on the stack.
static void ValueTypeAssignment()
{
Console.WriteLine("Assigning value types\n");
Point pl = new Point(10, 10);
Point p2 = pl;
// Print both variables Point.
pl.Display();
p2.Display();
// Change pl.X and output again. The value of p2.X has not changed.
pl.X = 100;
Console.WriteLine("\n=> Changed pl.X\n");
pl.Display();
p2.Display();
}
Here, a variable of type Point (pl) is first created, which is then assigned to
another variable of type Point (p2). Because Point is a value type, there
are two copies of MyPoint on the stack, each of which can be manipulated independently.
simo. Therefore, when changing the value of pl.X, the value of p2.X remains unaffected:
Assigning value types
X = 10, Y = 10
X = 10, Y = 10
=> Changed pl. X
X = 100, Y = 10
X = 10, Y = 10
Unlike value types, when an assignment operation is applied to
reference types (i.e., instances of all classes), it redirects to
whatever the reference variable points to. in mind. For illustration purposes, let's create a new
class called PointRef with the same members as the Point struct, but only rename
the constructor to match the name of that class:
// Classes are always reference types,
class PointRef
{
// Same members as in the Point structure. . .
// Don't forget to change the constructor name to PointRef!
public PointRef(int XPos, int YPos)
{
X = XPos;
Y = YPos;
}
}
Now let's use this type of PointRef in the new method shown below.
Note that other than working with the PointRef class rather than the Point struct, the code
is identical to the ValueTypeAssignment() method:
static void ReferenceTypeAssignment()
{
Console.WriteLine("Assigning reference types\n");
PointRef pl = new PointRef(10, 10);
PointRef p2 = pl;
// Print both variables PointRef.
pl.Display();
p2.Display();
// Change pl.X and output again.
pl.X = 100;
Console.WriteLine("\n=> Changed pl.X\n");
pl.Display();
p2.Display();
}
This results in two references pointing to the same object on the
managed heap. Thus, when changing the value of X using the reference
pl, the value of p2 will also change. X. Calling this new method in Main() produces the following
output:
Assigning reference types
X = 10, Y = 10
X = 10, Y = 10
=> Changed pl.X
X = 100, Y = 10
X = 100, Y = 10
I don't understand something. Where are the links here? In the second case, the same as in the first. Memberwise copying. Each field has a separate address.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2016-02-09
@petermzg

In the second case, there is no memberwise copying. pl and pl2 point to the same memory address.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question