├── .github
├── CODEOWNERS
├── ISSUE_TEMPLATE.md
├── PULL_REQUEST_TEMPLATE.md
└── workflows
│ └── main.yml
├── .gitignore
├── .vscode
├── launch.json
└── tasks.json
├── CONTRIBUTING.md
├── Finished
├── Delegates
│ ├── AnonymousDelegates
│ │ ├── AnonymousDelegates.csproj
│ │ └── Program.cs
│ ├── BasicDelegates
│ │ ├── BasicDelegates.csproj
│ │ └── Program.cs
│ ├── Composable
│ │ ├── Composable.csproj
│ │ └── Program.cs
│ ├── Composable2
│ │ ├── Composable2.csproj
│ │ └── Program.cs
│ └── DelegatesSolution
│ │ ├── DelegatesSolution.csproj
│ │ ├── Program.cs
│ │ └── Shipping.cs
├── Events
│ ├── BasicEvents
│ │ ├── BasicEvents.csproj
│ │ └── Program.cs
│ ├── ChainedEvents
│ │ ├── ChainedEvents.csproj
│ │ └── Program.cs
│ └── EventsSolution
│ │ ├── EventsSolution.csproj
│ │ └── Program.cs
└── Lambdas
│ ├── BasicLambdas
│ ├── BasicLambdas.csproj
│ └── Program.cs
│ ├── LambdaDelegates
│ ├── LambdaDelegates.csproj
│ └── Program.cs
│ └── LambdasSolution
│ ├── LambdasSolution.csproj
│ └── Program.cs
├── LICENSE
├── NOTICE
├── README.md
└── Start
├── Delegates
├── AnonymousDelegates
│ ├── AnonymousDelegates.csproj
│ └── Program.cs
├── BasicDelegates
│ ├── BasicDelegates.csproj
│ └── Program.cs
├── Composable
│ ├── Composable.csproj
│ └── Program.cs
├── Composable2
│ ├── Composable2.csproj
│ └── Program.cs
└── DelegatesSolution
│ ├── DelegatesSolution.csproj
│ └── Program.cs
├── Events
├── BasicEvents
│ ├── BasicEvents.csproj
│ └── Program.cs
├── ChainedEvents
│ ├── ChainedEvents.csproj
│ └── Program.cs
└── EventsSolution
│ ├── EventsSolution.csproj
│ └── Program.cs
└── Lambdas
├── BasicLambdas
├── BasicLambdas.csproj
└── Program.cs
├── LambdaDelegates
├── LambdaDelegates.csproj
└── Program.cs
└── LambdasSolution
├── LambdasSolution.csproj
└── Program.cs
/.github/CODEOWNERS:
--------------------------------------------------------------------------------
1 | # Codeowners for these exercise files:
2 | # * (asterisk) deotes "all files and folders"
3 | # Example: * @producer @instructor
4 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 |
7 |
8 | ## Issue Overview
9 |
10 |
11 | ## Describe your environment
12 |
13 |
14 | ## Steps to Reproduce
15 |
16 | 1.
17 | 2.
18 | 3.
19 | 4.
20 |
21 | ## Expected Behavior
22 |
23 |
24 | ## Current Behavior
25 |
26 |
27 | ## Possible Solution
28 |
29 |
30 | ## Screenshots / Video
31 |
32 |
33 | ## Related Issues
34 |
35 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: Copy To Branches
2 | on:
3 | workflow_dispatch:
4 | jobs:
5 | copy-to-branches:
6 | runs-on: ubuntu-latest
7 | steps:
8 | - uses: actions/checkout@v2
9 | with:
10 | fetch-depth: 0
11 | - name: Copy To Branches Action
12 | uses: planetoftheweb/copy-to-branches@v1
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | .tmp
4 | npm-debug.log
5 | bin/
6 | obj/
7 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.2.0",
3 | "configurations": [
4 | {
5 | // Use IntelliSense to find out which attributes exist for C# debugging
6 | // Use hover for the description of the existing attributes
7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8 | "name": ".NET Core Launch (console)",
9 | "type": "coreclr",
10 | "request": "launch",
11 | "preLaunchTask": "build",
12 | // If you have changed target frameworks, make sure to update the program path.
13 | "program": "${workspaceFolder}/Finished/Delegates/BasicDelegates/bin/Debug/net5.0/BasicDelegates.dll",
14 | "args": [],
15 | "cwd": "${workspaceFolder}/Finished/Delegates/BasicDelegates",
16 | // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17 | "console": "internalConsole",
18 | "stopAtEntry": false
19 | },
20 | {
21 | "name": ".NET Core Attach",
22 | "type": "coreclr",
23 | "request": "attach"
24 | }
25 | ]
26 | }
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "2.0.0",
3 | "tasks": [
4 | {
5 | "label": "build",
6 | "command": "dotnet",
7 | "type": "process",
8 | "args": [
9 | "build",
10 | "${workspaceFolder}/Finished/Delegates/BasicDelegates/BasicDelegates.csproj",
11 | "/property:GenerateFullPaths=true",
12 | "/consoleloggerparameters:NoSummary"
13 | ],
14 | "problemMatcher": "$msCompile"
15 | },
16 | {
17 | "label": "publish",
18 | "command": "dotnet",
19 | "type": "process",
20 | "args": [
21 | "publish",
22 | "${workspaceFolder}/Finished/Delegates/BasicDelegates/BasicDelegates.csproj",
23 | "/property:GenerateFullPaths=true",
24 | "/consoleloggerparameters:NoSummary"
25 | ],
26 | "problemMatcher": "$msCompile"
27 | },
28 | {
29 | "label": "watch",
30 | "command": "dotnet",
31 | "type": "process",
32 | "args": [
33 | "watch",
34 | "run",
35 | "${workspaceFolder}/Finished/Delegates/BasicDelegates/BasicDelegates.csproj",
36 | "/property:GenerateFullPaths=true",
37 | "/consoleloggerparameters:NoSummary"
38 | ],
39 | "problemMatcher": "$msCompile"
40 | }
41 | ]
42 | }
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 |
2 | Contribution Agreement
3 | ======================
4 |
5 | This repository does not accept pull requests (PRs). All pull requests will be closed.
6 |
7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license.
8 |
--------------------------------------------------------------------------------
/Finished/Delegates/AnonymousDelegates/AnonymousDelegates.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Delegates/AnonymousDelegates/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace AnonymousDelegates
4 | {
5 | public delegate string MyDelegate(int arg1, int arg2);
6 |
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | // Implement an anonymous delegate
12 | MyDelegate f = delegate (int arg1, int arg2)
13 | {
14 | return (arg1 + arg2).ToString();
15 | };
16 | Console.WriteLine("The number is: " + f(10, 20));
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Finished/Delegates/BasicDelegates/BasicDelegates.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Delegates/BasicDelegates/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BasicDelegates
4 | {
5 | // declare the delegate type
6 | public delegate string MyDelegate(int arg1, int arg2);
7 |
8 | class MyClass
9 | {
10 | // Delegates can be bound to instance members as well as
11 | // static class functions
12 | public string instanceMethod1(int arg1, int arg2)
13 | {
14 | return ((arg1 + arg2) * arg1).ToString();
15 | }
16 | }
17 |
18 | class Program
19 | {
20 | // TODO: Create functions to serve as delegate implementations
21 | static string func1(int a, int b)
22 | {
23 | return (a + b).ToString();
24 | }
25 | static string func2(int a, int b)
26 | {
27 | return (a * b).ToString();
28 | }
29 |
30 | static void Main(string[] args)
31 | {
32 | MyDelegate f = func1;
33 | Console.WriteLine("The number is: " + f(10, 20));
34 | f = func2;
35 | Console.WriteLine("The number is: " + f(10, 20));
36 |
37 | MyClass mc = new MyClass();
38 | f = mc.instanceMethod1;
39 | Console.WriteLine("The number is: " + f(10, 20));
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Finished/Delegates/Composable/Composable.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Delegates/Composable/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Composable
4 | {
5 | // declare the delegate type
6 | public delegate void MyDelegate(int arg1, int arg2);
7 |
8 | class Program
9 | {
10 | static void func1(int arg1, int arg2)
11 | {
12 | string result = (arg1 + arg2).ToString();
13 | Console.WriteLine("The number from func1 is: " + result);
14 | }
15 | static void func2(int arg1, int arg2)
16 | {
17 | string result = (arg1 * arg2).ToString();
18 | Console.WriteLine("The number from func2 is: " + result);
19 | }
20 | static void Main(string[] args)
21 | {
22 | MyDelegate f1 = func1;
23 | MyDelegate f2 = func2;
24 | // Create a composed delegate from f1 and f2
25 | MyDelegate f1f2 = f1 + f2;
26 |
27 | int a=10;
28 | int b=20;
29 |
30 | // call each delegate and then the chain
31 | Console.WriteLine("Calling the first delegate");
32 | f1(a, b);
33 | Console.WriteLine("Calling the second delegate");
34 | f2(a, b);
35 | // Call the composed delegate
36 | Console.WriteLine("\nCalling the chained delegates");
37 | f1f2(a, b);
38 |
39 | // subtract off one of the delegates
40 | Console.WriteLine("\nCalling the unchained delegates");
41 | f1f2 -= f1;
42 | f1f2(b, b);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Finished/Delegates/Composable2/Composable2.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Delegates/Composable2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Composable2
4 | {
5 | // declare the delegate type
6 | public delegate void MyDelegate(int arg1, ref int arg2);
7 |
8 | class Program
9 | {
10 | static void func1(int arg1, ref int arg2)
11 | {
12 | string result = (arg1 + arg2).ToString();
13 | arg2 += 20;
14 | Console.WriteLine("The number is: " + result);
15 | }
16 | static void func2(int arg1, ref int arg2)
17 | {
18 | string result = (arg1 * arg2).ToString();
19 | Console.WriteLine("The number is: " + result);
20 | }
21 | static void Main(string[] args)
22 | {
23 | MyDelegate f1 = func1;
24 | MyDelegate f2 = func2;
25 | // Create a composed delegate from f1 and f2
26 | MyDelegate f1f2 = f1 + f2;
27 |
28 | int a=10;
29 | int b=20;
30 |
31 | // call each delegate and then the chain
32 | Console.WriteLine("Calling the first delegate");
33 | f1(a, ref b);
34 | Console.WriteLine("Calling the second delegate");
35 | f2(a, ref b);
36 | // Call the composed delegate
37 | Console.WriteLine("\nCalling the chained delegates");
38 | f1f2(a, ref b);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Finished/Delegates/DelegatesSolution/DelegatesSolution.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Delegates/DelegatesSolution/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DelegatesSolution
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | ShippingFeesDelegate theDel;
10 | ShippingDestination theDest;
11 |
12 | string theZone;
13 | do
14 | {
15 | // get the destination zone
16 | Console.WriteLine("What is the destination zone?");
17 | theZone = Console.ReadLine();
18 |
19 | // if the user wrote "exit" then terminate the program,
20 | // otherwise continue
21 | if (!theZone.Equals("exit"))
22 | {
23 | // given the zone, retrieve the associated shipping info
24 | theDest = ShippingDestination.getDestinationInfo(theZone);
25 |
26 | // if there's no associated info, then the user entered
27 | // an invalid zone, otherwise continue
28 | if (theDest != null)
29 | {
30 | // ask for the price and convert the string to a decimal number
31 | Console.WriteLine("What is the item price?");
32 | string thePriceStr = Console.ReadLine();
33 | decimal itemPrice = decimal.Parse(thePriceStr);
34 |
35 | // Each ShippingDestination object has a function called calcFees,
36 | // use that as the delegate for calculating the fee
37 | theDel = theDest.calcFees;
38 |
39 | // For high-risk zones, we tack on the delegate that adds more
40 | if (theDest.m_isHighRisk)
41 | {
42 | theDel += delegate (decimal thePrice, ref decimal itemFee)
43 | {
44 | itemFee += 25.0m;
45 | };
46 | }
47 |
48 | // now all that is left to do is call the delegate and output
49 | // the shipping fee to the Console
50 | decimal theFee = 0.0m;
51 | theDel(itemPrice, ref theFee);
52 | Console.WriteLine("The shipping fees are: {0}", theFee);
53 | }
54 | else
55 | {
56 | Console.WriteLine("Hmm, you seem to have entered an uknown destination. Try again or 'exit'");
57 | }
58 | }
59 | } while (theZone != "exit");
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Finished/Delegates/DelegatesSolution/Shipping.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace DelegatesSolution
8 | {
9 | // declare the delegate type used to calculate the fees
10 | public delegate void ShippingFeesDelegate(decimal thePrice, ref decimal fee);
11 |
12 | // This is a base class that is used as a foundation
13 | // for each of the destination zones
14 | abstract class ShippingDestination
15 | {
16 | public bool m_isHighRisk;
17 | public virtual void calcFees(decimal price, ref decimal fee) { }
18 |
19 | // This static method returns an actual ShippingDestination object
20 | // given the name of the destination, or null if none exists
21 | public static ShippingDestination getDestinationInfo(string dest)
22 | {
23 | if (dest.Equals("zone1"))
24 | {
25 | return new Dest_Zone1();
26 | }
27 | if (dest.Equals("zone2"))
28 | {
29 | return new Dest_Zone2();
30 | }
31 | if (dest.Equals("zone3"))
32 | {
33 | return new Dest_Zone3();
34 | }
35 | if (dest.Equals("zone4"))
36 | {
37 | return new Dest_Zone4();
38 | }
39 | return null;
40 | }
41 | }
42 |
43 | // Now we define implementation classes for each of the real shipping
44 | // destinations. We can add as many as we like as the need arises
45 |
46 | class Dest_Zone1 : ShippingDestination
47 | {
48 | public Dest_Zone1()
49 | {
50 | this.m_isHighRisk = false;
51 | }
52 | public override void calcFees(decimal price, ref decimal fee)
53 | {
54 | fee = price * 0.25m;
55 | }
56 | }
57 |
58 | class Dest_Zone2 : ShippingDestination
59 | {
60 | public Dest_Zone2()
61 | {
62 | this.m_isHighRisk = true;
63 | }
64 | public override void calcFees(decimal price, ref decimal fee)
65 | {
66 | fee = price * 0.12m;
67 | }
68 | }
69 |
70 | class Dest_Zone3 : ShippingDestination
71 | {
72 | public Dest_Zone3()
73 | {
74 | this.m_isHighRisk = false;
75 | }
76 | public override void calcFees(decimal price, ref decimal fee)
77 | {
78 | fee = price * 0.08m;
79 | }
80 | }
81 |
82 | class Dest_Zone4 : ShippingDestination
83 | {
84 | public Dest_Zone4()
85 | {
86 | this.m_isHighRisk = true;
87 | }
88 | public override void calcFees(decimal price, ref decimal fee)
89 | {
90 | fee = price * 0.04m;
91 | }
92 | }
93 | }
--------------------------------------------------------------------------------
/Finished/Events/BasicEvents/BasicEvents.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Events/BasicEvents/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BasicEvents
4 | {
5 | // define the delegate for the event handler
6 | public delegate void myEventHandler(string value);
7 |
8 | class EventPublisher
9 | {
10 | private string theVal;
11 |
12 | // declare the event
13 | public event myEventHandler valueChanged;
14 |
15 | public string Val
16 | {
17 | set {
18 | this.theVal = value;
19 | // when the value changes, fire the event
20 | this.valueChanged(theVal);
21 | }
22 | }
23 | }
24 |
25 | class Program
26 | {
27 | static void Main(string[] args)
28 | {
29 | // use a named function as an event handler
30 | EventPublisher obj = new EventPublisher();
31 | obj.valueChanged += new myEventHandler(obj_valueChanged);
32 |
33 | // use an anonymous delegate as an event handler
34 | obj.valueChanged += delegate (string val) {
35 | Console.WriteLine("The value changed to {0}", val);
36 | };
37 |
38 | string str;
39 | do {
40 | Console.WriteLine("Enter a value: ");
41 | str = Console.ReadLine();
42 | if (!str.Equals("exit")) {
43 | obj.Val = str;
44 | }
45 | } while (!str.Equals("exit"));
46 | Console.WriteLine("Goodbye!");
47 | }
48 |
49 | // This function will be called when the value changes in the EventPublisher class
50 | static void obj_valueChanged(string value)
51 | {
52 | Console.WriteLine("The value changed to {0}", value);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/Finished/Events/ChainedEvents/ChainedEvents.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Events/ChainedEvents/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ChainedEvents
4 | {
5 | // define the delegate for the event handler
6 | public delegate void myEventHandler(string value);
7 |
8 | class EventPublisher
9 | {
10 | private string theVal;
11 | // declare the event handler
12 | public event myEventHandler valueChanged;
13 | public event EventHandler objChanged;
14 |
15 | public string Val
16 | {
17 | set {
18 | this.theVal = value;
19 | // when the value changes, fire the event
20 | this.valueChanged(theVal);
21 | this.objChanged(this, new ObjChangeEventArgs() { propChanged = "Val" });
22 | }
23 | }
24 | }
25 |
26 | class ObjChangeEventArgs : EventArgs
27 | {
28 | public string propChanged;
29 | }
30 |
31 | class Program
32 | {
33 | static void Main(string[] args)
34 | {
35 | // create the test class
36 | EventPublisher obj = new EventPublisher();
37 | // Connect multiple event handlers
38 | obj.valueChanged += new myEventHandler(changeListener1);
39 | obj.valueChanged += new myEventHandler(changeListener2);
40 |
41 | // Use an anonymous delegate as the event handler
42 | obj.valueChanged += delegate (string s) {
43 | Console.WriteLine("This came from the anonymous handler!");
44 | };
45 |
46 | obj.objChanged += delegate (object sender, ObjChangeEventArgs e) {
47 | Console.WriteLine("{0} had the '{1}' property changed", sender.GetType(), e.propChanged);
48 | };
49 |
50 | string str;
51 | do {
52 | Console.WriteLine("Enter a value: ");
53 | str = Console.ReadLine();
54 | if (!str.Equals("exit")) {
55 | obj.Val = str;
56 | }
57 | } while (!str.Equals("exit"));
58 | Console.WriteLine("Goodbye!");
59 | }
60 |
61 | static void changeListener1(string value)
62 | {
63 | Console.WriteLine("The value changed to {0}", value);
64 | }
65 | static void changeListener2(string value)
66 | {
67 | Console.WriteLine("I also listen to the event, and got {0}", value);
68 | }
69 | }
70 | }
--------------------------------------------------------------------------------
/Finished/Events/EventsSolution/EventsSolution.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Events/EventsSolution/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace EventsSolution
4 | {
5 | public delegate void BalanceEventHandler(decimal theValue);
6 |
7 | class PiggyBank
8 | {
9 | private decimal m_bankBalance;
10 | public event BalanceEventHandler balanceChanged;
11 |
12 | public decimal theBalance
13 | {
14 | set
15 | {
16 | m_bankBalance = value;
17 | balanceChanged(value);
18 | }
19 | get
20 | {
21 | return m_bankBalance;
22 | }
23 | }
24 | }
25 |
26 | class BalanceLogger
27 | {
28 | public void balanceLog(decimal amount)
29 | {
30 | Console.WriteLine("The balance amount is {0}", amount);
31 | }
32 | }
33 |
34 | class BalanceWatcher
35 | {
36 | public void balanceWatch(decimal amount)
37 | {
38 | if (amount > 500.0m)
39 | Console.WriteLine("You reached your savings goal! You have {0}", amount);
40 | }
41 | }
42 |
43 | class Program
44 | {
45 | static void Main(string[] args)
46 | {
47 | PiggyBank pb = new PiggyBank();
48 | BalanceLogger bl = new BalanceLogger();
49 | BalanceWatcher bw = new BalanceWatcher();
50 |
51 | pb.balanceChanged += bl.balanceLog;
52 | pb.balanceChanged += bw.balanceWatch;
53 |
54 | string theStr;
55 | do
56 | {
57 | Console.WriteLine("How much to deposit?");
58 |
59 | theStr = Console.ReadLine();
60 | if (!theStr.Equals("exit"))
61 | {
62 | decimal newVal = decimal.Parse(theStr);
63 |
64 | pb.theBalance += newVal;
65 | }
66 | } while (!theStr.Equals("exit"));
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/Finished/Lambdas/BasicLambdas/BasicLambdas.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Lambdas/BasicLambdas/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BasicLambdas
4 | {
5 | // define a few delegate types
6 | public delegate int MyDelegate(int x);
7 | public delegate void MyDelegate2(int x, string prefix);
8 | public delegate bool ExprDelegate(int x);
9 |
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | // Create a basic delegate that squares a number
15 | MyDelegate d1 = (x) => x * x;
16 | Console.WriteLine("The result of d1 is: {0}", d1(5));
17 |
18 | // Dynamically change the delegate to something else
19 | d1 = (x) => x * 10;
20 | Console.WriteLine("The result of d1 is: {0}", d1(5));
21 |
22 | // Create a delegate that takes multiple arguments
23 | MyDelegate2 d2 = (x, y) =>
24 | {
25 | Console.WriteLine("The two-arg lambda: {1}, {0}", x * 10, y);
26 | };
27 | d2(25, "Some string");
28 |
29 | // Define an expression delegate
30 | ExprDelegate d3 = (x) => x > 10;
31 | Console.WriteLine("Calling d3 with 5: {0}", d3(5));
32 | Console.WriteLine("Calling d3 with 15: {0}", d3(15));
33 | }
34 | }
35 | }
--------------------------------------------------------------------------------
/Finished/Lambdas/LambdaDelegates/LambdaDelegates.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Lambdas/LambdaDelegates/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace LambdaDelegates
4 | {
5 | // define a delegate
6 | public delegate void myEventHandler(string value);
7 |
8 | class MyClass
9 | {
10 | private string theVal;
11 | public event myEventHandler valueChanged;
12 |
13 | public string Val
14 | {
15 | set
16 | {
17 | this.theVal = value;
18 | // when the value changes, fire the event
19 | this.valueChanged(theVal);
20 | }
21 | }
22 | }
23 |
24 | class Program
25 | {
26 | static void Main(string[] args)
27 | {
28 | MyClass obj = new MyClass();
29 |
30 | // Use a Lambda expression to define an event handler
31 | // Note that this is a statement lambda, due to use of { }
32 | obj.valueChanged += (x) =>
33 | {
34 | Console.WriteLine("The value changed to {0}", x);
35 | };
36 |
37 | string str;
38 | do
39 | {
40 | str = Console.ReadLine();
41 | if (!str.Equals("exit"))
42 | {
43 | obj.Val = str;
44 | }
45 | } while (!str.Equals("exit"));
46 |
47 | Console.WriteLine("Goodbye!");
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Finished/Lambdas/LambdasSolution/LambdasSolution.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Finished/Lambdas/LambdasSolution/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace LambdasSolution
4 | {
5 | public delegate void BalanceEventHandler(decimal theValue);
6 |
7 | class PiggyBank
8 | {
9 | private decimal m_bankBalance;
10 | public event BalanceEventHandler balanceChanged;
11 |
12 | public decimal theBalance
13 | {
14 | set
15 | {
16 | m_bankBalance = value;
17 | balanceChanged(value);
18 | }
19 | get
20 | {
21 | return m_bankBalance;
22 | }
23 | }
24 | }
25 |
26 | class Program
27 | {
28 | static void Main(string[] args)
29 | {
30 | PiggyBank pb = new PiggyBank();
31 |
32 | pb.balanceChanged += (amount) => Console.WriteLine("The balance amount is {0}", amount);
33 | pb.balanceChanged += (amount) => { if (amount > 500.0m) Console.WriteLine("You reached your savings goal! You have {0}", amount); };
34 |
35 | string theStr;
36 | do
37 | {
38 | Console.WriteLine("How much to deposit?");
39 |
40 | theStr = Console.ReadLine();
41 | if (!theStr.Equals("exit"))
42 | {
43 | decimal newVal = decimal.Parse(theStr);
44 |
45 | pb.theBalance += newVal;
46 | }
47 | } while (!theStr.Equals("exit"));
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | LinkedIn Learning Exercise Files License Agreement
2 | ==================================================
3 |
4 | This License Agreement (the "Agreement") is a binding legal agreement
5 | between you (as an individual or entity, as applicable) and LinkedIn
6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning
7 | exercise files in this repository (“Licensed Materials”), you agree to
8 | be bound by the terms of this Agreement. If you do not agree to these
9 | terms, do not download or use the Licensed Materials.
10 |
11 | 1. License.
12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn
13 | members during their LinkedIn Learning subscription a non-exclusive,
14 | non-transferable copyright license, for internal use only, to 1) make a
15 | reasonable number of copies of the Licensed Materials, and 2) make
16 | derivative works of the Licensed Materials for the sole purpose of
17 | practicing skills taught in LinkedIn Learning courses.
18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject
19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members
20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable
21 | copyright license to distribute the Licensed Materials, except the
22 | Licensed Materials may not be included in any product or service (or
23 | otherwise used) to instruct or educate others.
24 |
25 | 2. Restrictions and Intellectual Property.
26 | - a. You may not to use, modify, copy, make derivative works of, publish,
27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the
28 | Licensed Materials, except as expressly set forth above in Section 1.
29 | - b. Linkedin (and its licensors) retains its intellectual property rights
30 | in the Licensed Materials. Except as expressly set forth in Section 1,
31 | LinkedIn grants no licenses.
32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any
33 | alleged infringement or misappropriation of any intellectual property rights
34 | of any third party based on modifications you make to the Licensed Materials,
35 | ii) any claims arising from your use or distribution of all or part of the
36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold
37 | harmless, and indemnify LinkedIn and its affiliates (and our and their
38 | respective employees, shareholders, and directors) from any claim or action
39 | brought by a third party, including all damages, liabilities, costs and
40 | expenses, including reasonable attorneys’ fees, to the extent resulting from,
41 | alleged to have resulted from, or in connection with: (a) your breach of your
42 | obligations herein; or (b) your use or distribution of any Licensed Materials.
43 |
44 | 3. Open source. This code may include open source software, which may be
45 | subject to other license terms as provided in the files.
46 |
47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS”
48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY,
49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY
50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR
51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR
52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS
53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING
54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A
55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT.
56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND
57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE
58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR
59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR
60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT
61 | EXPRESSLY STATED IN THESE TERMS.
62 |
63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT,
64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING
65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER
66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU
67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL:
68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT,
69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT
70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS
71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND
72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR
73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE
74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS.
75 |
76 | 6. Termination. This Agreement automatically terminates upon your breach of
77 | this Agreement or termination of your LinkedIn Learning subscription. On
78 | termination, all licenses granted under this Agreement will terminate
79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this
80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue
81 | the availability of some or all of the Licensed Materials at any time for any
82 | reason.
83 |
84 | 7. Miscellaneous. This Agreement will be governed by and construed in
85 | accordance with the laws of the State of California without regard to conflict
86 | of laws principles. The exclusive forum for any disputes arising out of or
87 | relating to this Agreement shall be an appropriate federal or state court
88 | sitting in the County of Santa Clara, State of California. If LinkedIn does
89 | not act to enforce a breach of this Agreement, that does not mean that
90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does
91 | not create a partnership, agency relationship, or joint venture between the
92 | parties. Neither party has the power or authority to bind the other or to
93 | create any obligation or responsibility on behalf of the other. You may not,
94 | without LinkedIn’s prior written consent, assign or delegate any rights or
95 | obligations under these terms, including in connection with a change of
96 | control. Any purported assignment and delegation shall be ineffective. The
97 | Agreement shall bind and inure to the benefit of the parties, their respective
98 | successors and permitted assigns. If any provision of the Agreement is
99 | unenforceable, that provision will be modified to render it enforceable to the
100 | extent possible to give effect to the parties’ intentions and the remaining
101 | provisions will not be affected. This Agreement is the only agreement between
102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior
103 | agreements relating to the Licensed Materials.
104 |
105 | Last Updated: March 2019
106 |
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 | Copyright 2022 LinkedIn Corporation
2 | All Rights Reserved.
3 |
4 | Licensed under the LinkedIn Learning Exercise File License (the "License").
5 | See LICENSE in the project root for license information.
6 |
7 | Please note, this project may automatically load third party code from external
8 | repositories (for example, NPM modules, Composer packages, or other dependencies).
9 | If so, such third party code may be subject to other license terms than as set
10 | forth above. In addition, such third party code may also depend on and load
11 | multiple tiers of dependencies. Please review the applicable licenses of the
12 | additional dependencies.
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # C#: Delegates, Events, and Lambdas
2 | This is the repository for the LinkedIn Learning course C#: Delegates, Events, and Lambdas. The full course is available from [LinkedIn Learning][lil-course-url].
3 |
4 | ![C#: Delegates, Events, and Lambdas][lil-thumbnail-url]
5 |
6 | C# provides different function types to fit a variety of real-world development scenarios. In this course, Joe Marini explores three important features of C#: Delegates, events, and lambdas. First, Joe shows how delegates are used to provide interchangeable functions during the lifetime of a program, which makes it easy to modify the functionality of an app on the fly. He then covers events, which are used to handle messages from both within and outside the program and can be turned on and off dynamically. Finally, he shows how lambdas provide a concise, efficient way of writing simple expressions without having all the associated syntax of a full function. These features help keep C# code readable, efficient, and maintainable, so join Joe to learn how to adapt this flexible set of programming tools to your own C# programming needs.
7 |
8 | ## Instructions
9 | This repository has the starting and finished example code for the C# Delegates, Events, and Lambdas course. Use the starting point code to follow along with the course and build towards the finished examples. Use the finished examples to learn more about how each of the various features of C# work.
10 |
11 | ## Installing
12 | 1. To use these exercise files, you must have the following installed:
13 | - A text editor or IDE, such as Visual Studio, Visual Studio Code, Atom, etc
14 | - The .NET SDK, version 5
15 | 2. Clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree.
16 |
17 |
18 | ### Instructor
19 |
20 | Joe Marini
21 |
22 | Senior Director of Product and Engineering
23 |
24 |
25 |
26 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/joe-marini).
27 |
28 | [lil-course-url]: https://www.linkedin.com/learning/c-sharp-delegates-events-and-lambdas-14503458
29 | [lil-thumbnail-url]: https://cdn.lynda.com/course/3006906/3006906-1642535780479-16x9.jpg
30 |
31 |
32 |
33 |
34 |
35 |
36 | [0]: # (Replace these placeholder URLs with actual course URLs)
37 |
38 | [lil-course-url]: https://www.linkedin.com/learning/
39 | [lil-thumbnail-url]: http://
40 |
41 |
--------------------------------------------------------------------------------
/Start/Delegates/AnonymousDelegates/AnonymousDelegates.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Delegates/AnonymousDelegates/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace AnonymousDelegates
4 | {
5 | public delegate string MyDelegate(int arg1, int arg2);
6 |
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | // TODO: Implement an anonymous delegate
12 |
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Start/Delegates/BasicDelegates/BasicDelegates.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Delegates/BasicDelegates/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BasicDelegates
4 | {
5 | // TODO: declare the delegate type
6 |
7 |
8 | class MyClass
9 | {
10 | // Delegates can be bound to instance members as well as
11 | // static class functions
12 | public string instanceMethod1(int arg1, int arg2)
13 | {
14 | return ((arg1 + arg2) * arg1).ToString();
15 | }
16 | }
17 |
18 | class Program
19 | {
20 | // TODO: Create functions to serve as delegate implementations
21 |
22 |
23 | static void Main(string[] args)
24 | {
25 | // TODO: exercise each delegate function
26 |
27 |
28 | // TODO: Use an instance function of a class as a delegate
29 |
30 |
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Start/Delegates/Composable/Composable.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Delegates/Composable/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Composable
4 | {
5 | // declare the delegate type
6 | public delegate void MyDelegate(int arg1, int arg2);
7 |
8 | class Program
9 | {
10 | static void func1(int arg1, int arg2)
11 | {
12 | string result = (arg1 + arg2).ToString();
13 | Console.WriteLine("The number from func1 is: " + result);
14 | }
15 | static void func2(int arg1, int arg2)
16 | {
17 | string result = (arg1 * arg2).ToString();
18 | Console.WriteLine("The number from func2 is: " + result);
19 | }
20 | static void Main(string[] args)
21 | {
22 | MyDelegate f1 = func1;
23 | MyDelegate f2 = func2;
24 | // Create a composed delegate from f1 and f2
25 |
26 | int a=10;
27 | int b=20;
28 |
29 | // call each delegate and then the chain
30 | Console.WriteLine("Calling the first delegate");
31 | f1(a, b);
32 | Console.WriteLine("Calling the second delegate");
33 | f2(a, b);
34 | // TODO: Call the composed delegate
35 | Console.WriteLine("\nCalling the chained delegates");
36 |
37 |
38 | // TODO: subtract off one of the delegates
39 | Console.WriteLine("\nCalling the unchained delegates");
40 |
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Start/Delegates/Composable2/Composable2.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Delegates/Composable2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Composable2
4 | {
5 | // declare the delegate type
6 | public delegate void MyDelegate(int arg1, int arg2);
7 |
8 | class Program
9 | {
10 | static void func1(int arg1, int arg2)
11 | {
12 | string result = (arg1 + arg2).ToString();
13 | Console.WriteLine("The number is: " + result);
14 | }
15 | static void func2(int arg1, int arg2)
16 | {
17 | string result = (arg1 * arg2).ToString();
18 | Console.WriteLine("The number is: " + result);
19 | }
20 | static void Main(string[] args)
21 | {
22 | MyDelegate f1 = func1;
23 | MyDelegate f2 = func2;
24 | // Create a composed delegate from f1 and f2
25 | MyDelegate f1f2 = f1 + f2;
26 |
27 | int a=10;
28 | int b=20;
29 |
30 | // call each delegate and then the chain
31 | Console.WriteLine("Calling the first delegate");
32 | f1(a, b);
33 | Console.WriteLine("Calling the second delegate");
34 | f2(a, b);
35 | // Call the composed delegate
36 | Console.WriteLine("\nCalling the chained delegates");
37 | f1f2(a, b);
38 |
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Start/Delegates/DelegatesSolution/DelegatesSolution.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Delegates/DelegatesSolution/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DelegatesSolution
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Start/Events/BasicEvents/BasicEvents.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Events/BasicEvents/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BasicEvents
4 | {
5 | // define the delegate for the event handler
6 | public delegate void myEventHandler(string value);
7 |
8 | class EventPublisher
9 | {
10 | private string theVal;
11 |
12 | // TODO: declare the event
13 |
14 | public string Val
15 | {
16 | set {
17 | this.theVal = value;
18 | // TODO: when the value changes, fire the event
19 |
20 | }
21 | }
22 | }
23 |
24 | class Program
25 | {
26 | static void Main(string[] args)
27 | {
28 | // TODO: use a named function as an event handler
29 | EventPublisher obj = new EventPublisher();
30 |
31 | // TODO: use an anonymous delegate as an event handler
32 |
33 |
34 | string str;
35 | do {
36 | Console.WriteLine("Enter a value: ");
37 | str = Console.ReadLine();
38 | if (!str.Equals("exit")) {
39 | obj.Val = str;
40 | }
41 | } while (!str.Equals("exit"));
42 | Console.WriteLine("Goodbye!");
43 | }
44 |
45 | // This function will be called when the value changes in the EventPublisher class
46 | static void obj_valueChanged(string value)
47 | {
48 | Console.WriteLine("The value changed to {0}", value);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Start/Events/ChainedEvents/ChainedEvents.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Events/ChainedEvents/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ChainedEvents
4 | {
5 | // define the delegate for the event handler
6 | public delegate void myEventHandler(string value);
7 |
8 | class EventPublisher
9 | {
10 | private string theVal;
11 | // declare the event handler
12 | public event myEventHandler valueChanged;
13 | // TODO4: Use the EventArgs class
14 |
15 |
16 | public string Val
17 | {
18 | set {
19 | this.theVal = value;
20 | // when the value changes, fire the event
21 | this.valueChanged(theVal);
22 | // TODO5: Use the custom event handler
23 |
24 | }
25 | }
26 | }
27 |
28 | // TODO3: Create a subclass of EventArgs for our use
29 |
30 | class Program
31 | {
32 | static void Main(string[] args)
33 | {
34 | // create the test class
35 | EventPublisher obj = new EventPublisher();
36 | // TODO1: Connect multiple event handlers
37 |
38 |
39 | // TODO2: Use an anonymous delegate as the event handler
40 |
41 |
42 | // TODO6: Listen for the custom event we defined with EventArgs
43 |
44 |
45 | string str;
46 | do {
47 | Console.WriteLine("Enter a value: ");
48 | str = Console.ReadLine();
49 | if (!str.Equals("exit")) {
50 | obj.Val = str;
51 | }
52 | } while (!str.Equals("exit"));
53 | Console.WriteLine("Goodbye!");
54 | }
55 |
56 | static void changeListener1(string value)
57 | {
58 | Console.WriteLine("The value changed to {0}", value);
59 | }
60 | static void changeListener2(string value)
61 | {
62 | Console.WriteLine("I also listen to the event, and got {0}", value);
63 | }
64 | }
65 | }
--------------------------------------------------------------------------------
/Start/Events/EventsSolution/EventsSolution.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Events/EventsSolution/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace EventsSolution
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Start/Lambdas/BasicLambdas/BasicLambdas.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Lambdas/BasicLambdas/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BasicLambdas
4 | {
5 | // define a few delegate types
6 | public delegate int MyDelegate(int x);
7 | public delegate void MyDelegate2(int x, string prefix);
8 | public delegate bool ExprDelegate(int x);
9 |
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | // TODO: Create a basic delegate that squares a number
15 |
16 | // TODO: Dynamically change the delegate to something else
17 |
18 | // TODO: Create a delegate that takes multiple arguments
19 |
20 | // TODO: Define an expression delegate
21 |
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/Start/Lambdas/LambdaDelegates/LambdaDelegates.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Lambdas/LambdaDelegates/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace LambdaDelegates
4 | {
5 | // define a delegate
6 | public delegate void myEventHandler(string value);
7 |
8 | class MyClass
9 | {
10 | private string theVal;
11 | public event myEventHandler valueChanged;
12 |
13 | public string Val
14 | {
15 | set
16 | {
17 | this.theVal = value;
18 | // when the value changes, fire the event
19 | this.valueChanged(theVal);
20 | }
21 | }
22 | }
23 |
24 | class Program
25 | {
26 | static void Main(string[] args)
27 | {
28 | MyClass obj = new MyClass();
29 |
30 | // TODO: Use a Lambda expression to define an event handler
31 | // Note that this is a statement lambda, due to use of { }
32 |
33 |
34 | string str;
35 | do {
36 | str = Console.ReadLine();
37 | if (!str.Equals("exit"))
38 | {
39 | obj.Val = str;
40 | }
41 | } while (!str.Equals("exit"));
42 |
43 | Console.WriteLine("Goodbye!");
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Start/Lambdas/LambdasSolution/LambdasSolution.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Start/Lambdas/LambdasSolution/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | // Use this file to create your solution to the Lambdas challenge
4 |
5 | namespace LambdasSolution
6 | {
7 | public delegate void BalanceEventHandler(decimal theValue);
8 |
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------