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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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