Example #1
0
        static void Main(string[] args)
        {
            PiggyBank pb = new PiggyBank();

            // Using lambda to implement the previous two delegations,
            // Note that we do NOT have to specify the data type of total. Compiler figures out.
            pb.balanceChanged += (total) => Console.WriteLine("The balance total is: {0}", total);
            pb.balanceChanged += (int total) => { if (total >= 500)
                                                  {
                                                      Console.WriteLine("You've reached your goal! " +
                                                                        "Your balance is now: {0}", total);
                                                  }
            };

            string str = "";

            while (str != "exit")
            {
                Console.Write("How much to deposit?\t");
                str = Console.ReadLine();
                if (str != "exit")
                {
                    int value = int.Parse(str);
                    pb.Balance += value;
                }
            }

            Console.WriteLine("\nPress Enter to continue...");
        }
        static void Main(string[] args)
        {
            PiggyBank      pb = new PiggyBank();
            BalanceLogger  bl = new BalanceLogger();
            BalanceWatcher bw = new BalanceWatcher();

            pb.balanceChanged += bl.balanceLog;
            pb.balanceChanged += bw.balanceWatch;

            string str = "";

            while (str != "exit")
            {
                Console.Write("How much to deposit?\t");
                str = Console.ReadLine();
                if (str != "exit")
                {
                    int value = int.Parse(str);
                    pb.Balance += value;
                }
            }

            Console.WriteLine("\nPress Enter to continue...");
        }