Coding the struct
Let's begin by deleting all of the default code and comments in the class you have created. We're going to make a really simple struct called Point
. Enter the following, starting from the very top:
public struct Point
You can still define a couple of variables to represent Point
and properties. So, for example, enter the following below the public struct Point
line:
public double X { get; set; } public double Y { get; set; }
You can also initialize them and you can still have a constructor. So, type the following below the preceding lines:
public Point(double x, double y)
Then, you can set the values inside the constructor of the properties. Enter the following within a set of curly braces below this line:
X = x; Y = y;
So, X
equals x
and Y
equals y
, as we have here.
This is the basic struct
example, as shown in Figure 6.19.2. Remember, it looks superficially similar to a class
, but internally, it behaves differently because when you make copies it's a "by value" kind of...