In the below code I initialized the client and passed it to the constructor of the new Worker classes. I then set the value to null while the two workers are still running. To my understanding I'm doing a pass by value for reference type in the constructor, so p.myClient and worker1.client should point to the same Client object on the heap. But why doesn't it throw a NullPointerException?
class Client
{
public Client()
{
Console.WriteLine("creating client");
}
public void Go(string s)
{
Console.WriteLine("go {0}", s);
}
}
class Worker
{
private int invokeCount;
Client client;
public Worker(Client c)
{
client = c;
}
public void Pull(Object stateInfo)
{
Console.WriteLine("{0} Pulling {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"), (++invokeCount).ToString());
client.Go("pull");
}
public void Push(Object stateInfo)
{
Console.WriteLine("{0} Pushing {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"), (++invokeCount).ToString());
client.Go("push");
}
}
class Program
{
Client myClient = new Client();
static void Main()
{
Program p = new Program();
var worker1 = new Worker(p.myClient);
Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n", DateTime.Now);
var stateTimer = new Timer(worker1.Push, null, 1000, 400);
var worker2 = new Worker(p.myClient);
Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n", DateTime.Now);
var stateTimer2 = new Timer(worker2.Pull, null, 1000, 400);
p.myClient = null;
Console.ReadKey();
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
Console.Read();
}
}