static void Main(string[] args) { // Delegate can be referenced as a class var delegateInstance = new Delegate01(MyMethod01); delegateInstance(); // Call // Trivial cases can simplify (Same result) // 1. Omit 'new' Delegate01 delegateInstance2 = MyMethod01; delegateInstance2(); // Call // Final Trivial case // Action delegate void and takes no parameters Action delegateInstance3 = MyMethod01; // Same as above delegateInstance3(); Delegate02 delegateInstance4 = (x) => { return(x * x * x); }; // Lambda // Input parameters // Code body Delegate02 delegateInstance5 = x => x * x * x; // Lambda // Input parameters // Code body }
static void Main(string[] args) { // dalegate can bbe referenced as a class // use new keyword var delegateInstance = new Delegate01(MyMethod01); // cal the delegate instances delegateInstance(); // call the method // trivial cases can be simplify (same result) Delegate01 delegateInstance2 = MyMethod01; // same as the above delegateInstance2(); // call // final trival case // action DELEGATE IS VOID AND TAKES NO PARAMETERS Action delegateInstance03 = MyMethod01; // same as the above delegateInstance03(); // Action delegateInstance4 = MyMethod02; <-- will never work because of the type delegate does not have return type // whereas MyMethod 02 does have a return type of string. Delegate02 delegateInstance4 = (x) => { return(x * x * x); }; // LAMBDA // INPUT PARAMS { // CODE BODY } Delegate02 delegateInstance5 = x => (x * x * x); // EASIER WAY OF IMPLEMENTING LAMBDA checked { Console.WriteLine(MyMethod03(delegateInstance5(delegateInstance5(10)))); } }
static void Main(string[] args) { // A delegate can be referenced as a class //so we can use the new keyword. var delegateInstance = new Delegate01(MyMethod01); // call this delegateInstance(); // in trivial cases we can simplify to get the same result // 1. omit 'New' Delegate01 delegateInstance2 = MyMethod01; delegateInstance2(); // final trivial case // Action delegate is void and takes no parameters Action delegateInstance3 = MyMethod01; delegateInstance3(); //Action delegateInstance4 = MyMethod02; - cannot do this as an action delegate must have no return type Delegate02 delegateInstance4 = (x) => { return(x * x * x); }; //Lambda // Input Params { // Code Body } Delegate02 delegateInstance5 = x => x * x * x; //Lambda // Input Params { // Code Body } Console.WriteLine(MyMethod03(delegateInstance5(35))); }
static void Main(string[] args) { //delegate can be referenced as a class so we can use NEW keyword var delegateInstance = new Delegate01(MyMethod01); // call this by... delegateInstance(); //trivial cases can simplify (same result) // 1. omit 'new' Delegate01 delegateInstance2 = MyMethod01; delegateInstance2(); //final trivial case //action DELEGATE is void and takes no paramenters Action delegateInstance3 = MyMethod01; //same as above delegateInstance3(); // will never work Action delegateInstance4 = MyMethod02; Delegate02 delegateInstance4 = (x) => { return(x * x * x); }; // Input Params { // code body } Delegate02 delegateInstance5 = x => x * x * x;; }
static void Main(string[] args) { // delegate CAN BE REFERENCED AS A CLASS // use new keyword var delegateInstance = new Delegate01(MyMethod01); // call this delegateInstance(); // call the method // trivial cases can simplify (same result) // 1. omit 'new' Delegate01 delegateInstance2 = MyMethod01; // same as above delegateInstance2(); // call // final trivial case // ACTION DELEGATE IS VOID AND TAKES NO PARAMETERS Action delegateInstance3 = MyMethod01; // same as above delegateInstance3(); // call // will never work !!!! Action delegateInstance4 = MyMethod02; Delegate02 delegateInstance4 = (x) => { return(x * x * x); }; // LAMBDA // INPUT PARAMS { // CODE BODY } Delegate02 delegateInstance5 = x => x * x * x; // LAMBDA checked { Console.WriteLine(MyMethod03(delegateInstance5(delegateInstance5(10)))); } }
static void Main(string[] args) { var delegateInstance = new Delegate01(Method01); //same as above delegateInstance(); //call the method //trivial cases can simplyfy (same result) //1. omit 'new' Delegate01 delegateInstance2 = Method01; //same as above delegateInstance2(); //call //final trivial case // ACTION delegate is void and takes no parameters Action delegateInstance3 = Method01; //same as above delegateInstance3(); //LAMBDA INPUT PARAMS {//Code body } Delegate02 delegateInstance4 = (x) => { return(x * x * x); }; Console.WriteLine(delegateInstance4(5)); Delegate02 delegateInstance5 = x => (x * x * x); //Console.WriteLine(delegateInstance5(4)); // Pass the delegate into a method Console.WriteLine(Method02(delegateInstance5(10))); // Pass the delegate into a delegate then into a method Console.WriteLine(Method02(delegateInstance5(delegateInstance5(3)))); }
static void Main(string[] args) { Delegate01 myDelegateInstance = Method01; Action myOtherDelegateInstance = Method02; Delegate02 secondDelegate = Method03; myDelegateInstance(); myOtherDelegateInstance(); Console.WriteLine(secondDelegate(3, false)); }
static void Main(string[] args) { //shorter version // notice no brackets in method => just a placeholder, dont call it right now Delegate01 delegateInstance = Method01; Action myOtherDelegateInstance = Method02; Delegate02 stringInstance = StringMethod; //run delegateInstance(); myOtherDelegateInstance(); Console.WriteLine(stringInstance(2, true)); }
static void Main(string[] args) { var delegate01 = new Delegate01(Method01); delegate01 += Method02; delegate01(); var del = new Delegate02(Method03); del(1, 2); //most common delegate type is type of void MyDelegate(); ie no inputs, no outputs ==> action delegate var delegate03 = new Action(Method01); //often see word 'action()' in code == pointer to method types of void DoThis(); }
static void Main(string[] args) { // shorter version // notice : no brackets in method ==> just placeholder, don't call right now Delegate01 delegateInstance = Method01; Action myOtherDelegateInstance = Method02; // Declare DelegateTest Delegate02 delegateTest = GetString; // run delegateInstance(); myOtherDelegateInstance(); // run Delegate Test Console.WriteLine(delegateTest(20, false)); Console.WriteLine(delegateTest(10, true)); }
static void Main(string[] args) { // shorter version // notice : no brackets in method ==> just placeholder, don't call right now Delegate01 mydelegateinstance = Method01; Action myotherdelegateinstance = Method02; // declare Delegate02 delegateinstance = Method03; // run mydelegateinstance(); myotherdelegateinstance(); // run Console.WriteLine(delegateinstance(15, true)); Console.WriteLine(delegateinstance(23, false)); }
static void Main(string[] args) { var delegateInstance = new Delegate01(MyMethod01); // Calls method delegateInstance(); // Same as above Delegate01 delegateInstance2 = MyMethod01; delegateInstance2(); // Same as above // Action delegate is void and takes no parameters Action delegateInstance3 = MyMethod01; delegateInstance3(); // Using lambda Delegate02 delegateInstance4 = x => x * x; }