■
Note
It is typically considered bad style to define public data within a class or structure. Rather, you will want to
define private data, which can be accessed and changed using public properties. These details will be examined in
Chapter 5.
Here is a Main() method that takes our Point type out for a test drive. Figure 4-9 shows the pro-
gram’s output.
static void Main(string[] args)
{
Console.WriteLine("***** A First Look at Structures *****");
// Create an initial Point.
Point myPoint;
myPoint.X = 349;
myPoint.Y = 76;
myPoint.Display();
// Adjust the X and Y values.
myPoint.Increment();
myPoint.Display();
Console.ReadLine();
}
Figure 4-9.
Our Point structure in action
Creating Structure Variables
When you wish to create a structure variable, you have a variety of options. Here, we simply create a
Point variable and assign each piece of public field data before invoking its members. If we do not
assign each piece of public field data (X and Y in our case) before making use of the structure, we
will receive a compiler error:
Do'stlaringiz bilan baham: |