Value & Reference types in C# – Free eBook

Do yo know the concepts of Value & Reference types in C# clearly?

Even novice developer in C# knows it, rite?

Then, let me ask you to quick simple question in that.

Can you tell me the output of this program, without running it. Unfortunately, more than 50% of the developers will not answer it correctly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ValueRef
{
class Point
{
public int X { get; set; }
public int Y { get; set; }
}
class Program
{
static void Main(string[] args)
{
Point P1 = new Point();
P1.X = 10;
P1.Y = 20;
Method1(P1);
Console.WriteLine(P1.X); // Whats gets printed here?
Console.WriteLine(P1.Y); // And here as well?
}
static void Method1(Point P)
{
P.X = 15;
P = null;
}
}
}

If you had guessed “Null reference exception”, you must read this book.

The correct answer is : 15 & 20 will get printed on the screen.

To find out why, download and read this short ebook