How struct variables are stored in C#

I Read this StackOverflow Question And i read this blog here

I couldn't understand completely. Here are my doubts.

 struct S
{
    private int x;
    private int y;

    public int X { get { return x; } }
    public int Y { get { return y; } }

    public S(int x, int y, bool panic)
    {          
        this.x = x;
        this.y = y;

        if (panic)
            throw new Exception();
    }
}

static class P
{
    static void Main()
    {
        S s = default(S);

        try
        {                

            s = new S(1, 2, false);

            Console.WriteLine("{0}, {1}", s.X, s.Y);

            s = new S(3, 4, true);
        }
        catch
        {
            Console.WriteLine("{0}, {1}", s.X, s.Y);
        }

        Console.ReadLine();
    }

}

Here before throwing exception i assigned the values. But why is it not assigning to the object s i mean in the console i written in the catch block, says (1,2).

Because it is initialized again in the second line , and called the constructor with (3,4). so how it is (1,2).

Somehow i cant understand this.

And also,

Therefore, using the new operator on a value type allocates no additional memory. Rather, the memory already allocated for the value is used.

In that blog , the answer is No.

If that is the case, is it initializing with new memory. If so, how comes (1,2) is coming in the catch block.

As i am new to C# i am not able understand this.

I know its silly doubt, but someone please help me in understanding this concept.

Jon Skeet
people
quotationmark

Because it is initialized again in the second line , and called the constructor with (3,4).

You've called the constructor, but the constructor itself hasn't finished - so the assignment to s never takes place.

This of this:

s = new S(3, 4, true);

As equivalent to :

// Create the new value
S tmp = new S(3, 4, true);
// Copy it into the s variable
s = tmp;

The first statement never completes, so the assignment doesn't occur... so you still see the first value of s in the catch block.

people

See more on this question at Stackoverflow