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) { // 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) { // Lambda in delegate Delegate01 Handler = x => x * x; Console.WriteLine(Handler(5)); // Lambda in collection List <int> list01 = new List <int>(); list01.Add(1); list01.Add(2); list01.Add(3); using (StreamWriter writer = new StreamWriter("output.txt")) { list01.ForEach(i => writer.WriteLine(i)); Console.WriteLine("Check output.txt for the output"); } // Foreach list01.ForEach(i => Console.WriteLine(i)); }
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) { //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) { // 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) { Delegate01 Handler = x => x * x; Console.WriteLine(Handler(5)); Delegate01 Handler2 = (x) => (x * x * x); Console.WriteLine(Handler2(5)); }
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 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) { // 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) { 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; }
static void Main(string[] args) { // var delegate01 = new Delegate(); var delegate02 = new Delegate01(Method01); delegate02 += Method02; delegate02 += Method03; // NOTHING IS RUNNING!!! // TO RUN , CAN CALL DELEGATE WITH BRACKETS delegate02(); var delegate03 = new Delegate03(Method04); delegate03(10, 100); // MOST COMMON DELEGATE TYPE IS TYPE OF void MyDelegate(); // IE NO INPUTS, NO OUTPUTS ==> ACTION DELEGATE!!! var delegate04 = new Action(Method01); // often see word 'Action()' in code ==> POINTER TO METHOD TYPES OF Void DoThis(); }
static void Main(string[] args) { //var delegate01 = new Delegate(); var delegate02 = new Delegate01(Method01); delegate02 += Method02; delegate02 += Method03; //Nothing is running //To run, can call delegate with brackets delegate02(); var delegate03 = new Delegate03(Method04); delegate03(10, 100); //Most common delegate type is type of void MyDelegate(); //I.e no inputs, no outputs ==> action delegate var delegate04 = new Action(Method01); //often see the word Action() in code ==> pointer to method types of void DoThis(); }
static void Main(string[] args) { // Async - Main() thread // Sync - sequence in order // Process = .exe running application which is able to send commands to CPU for processing // Thread ==> set of instructions bundled up and sent to CPU for processing // Main() //{ //Sub() ==> execute as different process on computer //} // C# Threading ==> manually create thread // Tasks ==> hard work removed ==> easy for programmer to work with sub-threads/sub-tasks // Key Words //Process - running exe //Application - run by user and runs in foreground //Service - run by the computer at startup and runs in background (DNS, DHCP, WebSite) //Thread - set of instructions sent to CPU for processing //Single-Threaded runs on main thread only //Multi-Threaded can take advantage of multi-core CPUs which can run multiple threads simultaneously // Faster? NO // Scale out - More CPUs YES // When are tasks/threads useful? // a) Multi-task processing eg background processing of graphics // b) Background processing jobs eg overnight tasks for credit card financial reporting // c) Website => click => data can take 5 seconds but rather than freeze screen, can still work // !!! Just be aware with threads and tasks we have ABSOLUTELY NO CONTROL OVER WHEN THE OPERATING SYSTEM WILL RUN A PARTICULAR TASK var thread = new Thread( () => { Thread.Sleep(3000); Console.WriteLine($"This is a thread {s.ElapsedMilliseconds}"); } ); var task01 = new Task( () => { Thread.Sleep(1000); Console.WriteLine($"This is a task {s.ElapsedMilliseconds}"); } ); // Create and start var task02 = new Task( () => { Thread.Sleep(2000); Console.WriteLine($"Creating and running task at same time{s.ElapsedMilliseconds}"); } ); // Delegates Delegate01 delegateInstance = Method01; var task03 = new Task( new Action(delegateInstance) );