Skip to content

Instantly share code, notes, and snippets.

@bembengarifin
Created July 13, 2015 05:20
Show Gist options
  • Save bembengarifin/f6e49108e5ebb4eaa564 to your computer and use it in GitHub Desktop.
Save bembengarifin/f6e49108e5ebb4eaa564 to your computer and use it in GitHub Desktop.
Delegates
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace LambdaExpressionTraining
{
class MyButton
{
//public event EventHandler OnClickEvent = delegate { };
private EventHandler _OnClickEvent;
public event EventHandler OnClickEvent
{
add { _OnClickEvent += value; }
remove { _OnClickEvent -= value; }
}
public EventHandler OnAnotherClickEvent = delegate { };
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
public string MyProp { get; set; }
public void Click()
{
//var handler = OnClickEvent;
//if (handler != null)
//{
//OnClickEvent(null, EventArgs.Empty);
OnAnotherClickEvent(null, EventArgs.Empty);
//}
}
}
class Program
{
delegate void DoSomething(string message);
delegate string DoSomethingWithReturnResult(string message);
delegate int PerformCalculation(int i1, int i2);
static void Main(string[] args)
{
//DoImplementation("Good morning");
//DoSomething ds = new DoSomething(DoImplementation);
////ds += new DoSomething(DoAnotherImplementation);
//ds("called from delegate");
//DoSomethingWithReturnResult ds = new DoSomethingWithReturnResult(DoImplementation);
//ds -= new DoSomething(DoAnotherImplementation);
//foreach (DoSomething inv in ds.GetInvocationList())
//{
// Task.Factory.StartNew(() => 1);
//}
//ds("called after minus");
//MyButton button = new MyButton();
//button.OnClickEvent = Button_OnClickEvent;
//button.OnAnotherClickEvent += Button_OnClickEvent1;
//button.OnAnotherClickEvent = Button_OnClickEvent;
//button.Click();
//PerformCalculation calc = new PerformCalculation(Add);
//PerformCalculation calc = new PerformCalculation(Minus);
//Console.WriteLine(calc(1, 2));
// .net 1.0
DoSomething ds = new DoSomething(delegate (string msg)
{
Console.WriteLine("This is performed by anonymous method");
});
// .net 2.0
DoSomething tempDelegate = delegate (string msg)
{
Console.WriteLine("anonymous method 2nd");
};
// .net 3.5
DoSomething tempDelegate1 = (string msg) =>
{
Console.WriteLine("anonymous method 2nd");
};
DoSomething tempDelegate2 = (msg) =>
{
Console.WriteLine("anonymous method 2nd");
};
DoSomething tempDelegate3 = (msg) => Console.WriteLine("anonymous method 2nd");
ds += tempDelegate;
ds("called from delegate");
//unregister
ds -= tempDelegate;
ds("called from delegate");
//PerformCalculation calc = new PerformCalculation(delegate (int in1, int in2)
//{
// return in2 * in1;
//});
//Console.WriteLine(calc(3,4));
Console.WriteLine("Program is completed");
Console.Read();
//do long processing
// GC = 3 -> 0,1,2
// gen 0 = in few seconds
// gen 1 = 10x more infrequent gen 0
// gen 2 = 10x gen 1
// finalizer queue
// collection
}
static int Add(int i1, int i2)
{
return i1 + i2;
}
static int Minus(int i1, int i2)
{
return i1 - i2;
}
private static void Button_OnClickEvent1(object sender, EventArgs e)
{
Console.WriteLine("My button is clicked again");
}
private static void Button_OnClickEvent(object sender, EventArgs e)
{
Console.WriteLine("My button is clicked");
}
static void DoImplementation(string message)
{
Console.WriteLine("I'm doing something + {0}, Thread: {1}", message, Thread.CurrentThread.ManagedThreadId);
}
static void DoAnotherImplementation(string message)
{
Console.WriteLine("I'm doing something else + {0}, Thread: {1}", message, Thread.CurrentThread.ManagedThreadId);
}
}
class UnmanagedObject : IDisposable
{
public void Dispose()
{
throw new NotImplementedException();
}
//destructor
~UnmanagedObject()
{
Dispose();
}
public UnmanagedObject()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment