Show/Hide
You have a Provider class, pretty simple:
And class Consumer a bit more difficult:
And finally here is favourable ;) static void Main:
static void Main()
{
var provider = new Provider();
var topLevelConsumers = new List();
for (var i = 0; i < 5; i++)
{
var consumer = new Consumer { Id = i };
provider.Notify += consumer.Consumer_Notify;
}
for (var i = 5; i < 10; i++)
{
var consumer = new Consumer { Id = i };
provider.Notify += consumer.Consumer_Notify;
topLevelConsumers.Add(consumer);
}
Console.WriteLine("Step 1. Collect garbage");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine("Step 2. Dispose provider");
provider = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine("Step 3. Close app");
Console.ReadKey();
}
Try to predict output of such main function (don't pay attention to the Id's order in output)?
public class Provider
{
public delegate void NotifyDelegate(string message);
public event NotifyDelegate Notify;
}
And class Consumer a bit more difficult:
public class Consumer
{
public int Id { get; set; }
~Consumer()
{
Console.WriteLine("Consumer " + Id + " finalized...");
}
public void Consumer_Notify(string message)
{
}
}
And finally here is favourable ;) static void Main:
static void Main()
{
var provider = new Provider();
var topLevelConsumers = new List
for (var i = 0; i < 5; i++)
{
var consumer = new Consumer { Id = i };
provider.Notify += consumer.Consumer_Notify;
}
for (var i = 5; i < 10; i++)
{
var consumer = new Consumer { Id = i };
provider.Notify += consumer.Consumer_Notify;
topLevelConsumers.Add(consumer);
}
Console.WriteLine("Step 1. Collect garbage");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine("Step 2. Dispose provider");
provider = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine("Step 3. Close app");
Console.ReadKey();
}
Try to predict output of such main function (don't pay attention to the Id's order in output)?
No comments:
Post a Comment