Q:

Creating structure variables C#

I'm  a beginner going through the examples in a book which are somewhat hard to follow.  I'm going through the book and compiling the code to get a look at what it does.  I'm in a section of structures, specifically structure variables. The following code has the error point does not take two arguments.  Can someone help me spot what's missing/incorrect here?  Thanks.
    using System;

    class Program
    {
        static void Main(string[] args)
        {

            // Create an initial Point.
            Point myPoint;
Point p1 = new Point(10, 10);
            myPoint.X = 349;
            myPoint.Y = 76;
            myPoint.Display();
            // Adjust the X and Y values.
            myPoint.Increment();
            myPoint.Display();
            Console.ReadLine();
        }
        // Assigning two intrinsic value types results in
        // two independent variables on the stack.
        static void ValueTypeAssignment()
        {
            Console.WriteLine("Assigning value types\n");
            Point p1 = new Point(10, 10);
            Point p2 = p1;
            // Print both points.
            p1.Display();
            p2.Display();
            // Change p1.X and print again. p2.X is not changed.
            p1.X = 100;
            Console.WriteLine("\n=> Changed p1.X\n");
            p1.Display();
            p2.Display();
        }
    }

    struct Point
    {
        // Fields of the structure.
        public int X;
        public int Y;
        // Add 1 to the (X, Y) position.
        public void Increment()
        {
            X++; Y++;
        }
        // Subtract 1 from the (X, Y) position.
        public void Decrement()
        {
            X--; Y--;
        }
        // Display the current position.
        public void Display()
        {
            Console.WriteLine("X = {0}, Y = {1}", X, Y);
        }

    }

A:

You need to add a two-parameter constructor to Point, because you call it with arguments (10, 10).
struct Point
{
    // Fields of the structure.
    public int X;
    public int Y;

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

Alternatively you can construct it with the built-in nullary (no-parameters) constructor and then set the properties:
Point myPoint = new Point();
myPoint.X = 349;
myPoint.Y = 76;

A shorthand for that is:
Point myPoint = new Point { X = 349, Y = 76 };

Or even shorter:
var myPoint = new Point { X = 349, Y = 76 };

Finally, it is generally good practise to make structs immutable: once constructed it should be impossible to modify their contents. This helps to avoid a lot of other pitfalls in the language.

A:

Instead of constructing a point using a constructor with two arguments, instantiate the properties as part of the same call. For example:
Point p = new Point { X = 1, Y = 2 };

You gain the advantage of single line construction without having to write additional code.

