static void Main(string[] args) { //Day 1 Day1Contents d1 = new Day1Contents(); ///Day 2 - classes Parent p1 = new Parent("Hoon"); p1.Hello(); Child c1 = new Child("Hoon", 29); c1.Name = "HoonK"; c1.Hello(); //Day 2 - Delegate DelegateDemo del1 = new DelegateDemo(); del1.PrintWhatEver(10); del1.PrintWhatEver(10.0f); del1.PrintWhatEver("111"); del1.RunHandlers(); //Day 3 - DataStructure Ds ds = new Ds(); }
public static void RunTest2() { DelegateDemo.TestMethodDelegate(123); var res = DelegateDemo.TestFunctionDelegate(456); UnityEngine.Debug.Log("!! TestDelegate.RunTest2 res = " + res); DelegateDemo.TestActionDelegate("rrr"); }
public static void Main() { DelegateDemo del = new DelegateDemo(method1); del += method2; del(); }
static void Main(string[] args) { //TypeDemo typeDemo = new TypeDemo(); //typeDemo.Work01(); DelegateDemo delegateDemo = new DelegateDemo(); delegateDemo.Work02(); }
protected void Button6_Click(object sender, EventArgs e) { NumberChanger <int> deN1 = new NumberChanger <int>(DelegateDemo.AddNum); NumberChanger <int> deN2 = new NumberChanger <int>(DelegateDemo.MultNum); deN1(25); this.TextBox1.Text += string.Format($"\r\nValue of Num: {DelegateDemo.getNum()}"); //35 = 10+25 deN2(5); this.TextBox1.Text += string.Format($"\r\nValue of Num: {DelegateDemo.getNum()}"); //175 = 35*5 }
static void Main() { Procedure someProcs = null; someProcs += new Procedure(DelegateDemo.Method1); someProcs += new Procedure(Method2); DelegateDemo demo = new DelegateDemo(); someProcs += new Procedure(demo.Method3); someProcs(); }
static void Main() { Procedure someProcs = null; someProcs += new Procedure(DelegateDemo.Method1); someProcs += new Procedure(Method2); // Example with omitted class name DelegateDemo demo = new DelegateDemo(); someProcs += new Procedure(demo.Method3); someProcs(); }
static int __CreateInstance(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); if (LuaAPI.lua_gettop(L) == 1) { var gen_ret = new DelegateDemo(); translator.Push(L, gen_ret); return(1); } } catch (System.Exception gen_e) { return(LuaAPI.luaL_error(L, "c# exception:" + gen_e)); } return(LuaAPI.luaL_error(L, "invalid arguments to DelegateDemo constructor!")); }
private static void A02_Delegate() { var a = new DelegateDemo(); a.Demo(); }
static void Main(string[] args) { EmployeeSeparator employeeSeparator = new EmployeeSeparator(); Finance finance = new Finance(employeeSeparator); IT it = new IT(employeeSeparator); employeeSeparator.Separate(); DelegateDemo delegateDemo = new DelegateDemo(); delegateDemo.Print("Am from delegate"); //delegateDemo.DoLongOperation(UpdatePrintMessage); //CustomerPresentation customerPresentation = new CustomerPresentation(); //customerPresentation.Update(); /*AnonymouseMethodDemo anonymouseMethodDemo = new AnonymouseMethodDemo(); * anonymouseMethodDemo.CalculateAreaOfRect(5, 5); * * anonymouseMethodDemo.PrintNumber(delegate (int number) * { * Console.WriteLine("The updated number : " + number); * }, 100); * * anonymouseMethodDemo.saveEventHandler += delegate (object sender, EventArgs e) * { * Console.WriteLine("Update successfull"); * }; * anonymouseMethodDemo.Save(); * anonymouseMethodDemo.CalculateAreaOfRect(5, 5); * anonymouseMethodDemo.CalculateAreaOfRectLambda(5, 6); * * LambdaCollectionEx lambdaCollectionEx = new LambdaCollectionEx(); * lambdaCollectionEx.CheckStudentActivities();*/ // Polymorphism Example using Inheritance BusinessFacade businessFacade = new BusinessFacade(); //Normal implementaiton /*Car car = new Car(); * Ship ship = new Ship(); * businessFacade.StartCar(car); * businessFacade.StopCar(car); * businessFacade.StartShip(ship); * businessFacade.StopShip(ship);*/ //With Base class [Machine] - So initially base class methods got executed //Warning CS0108 'Car.Start()' hides inherited member 'Machine.Start()'. Use the new keyword if hiding was intended /*Machine car = new Car(); * Machine ship = new Ship(); * businessFacade.StartCar(car); * businessFacade.StopCar(car); * businessFacade.StartShip(ship); * businessFacade.StopShip(ship);*/ //Polymorphic way //A base class reference variable can point to derived class object //Derived class is specialization of base class which means the derived class has all the capabilities of base class //Derived class should not hold the base class object Machine car = new Car(); Machine ship = new Ship(); /*businessFacade.StartMachine(car); //Dynamically decide which machine needs to start. In this case it's car * businessFacade.StopMachine(car); * businessFacade.StartMachine(ship); //Dynamically decide which machine needs to start. In this case it's ship * businessFacade.StopMachine(ship); * * Machine airPlane = new Airplane(); * businessFacade.StartMachine(airPlane); //Dynamically decide which machine needs to start. In this case it's airPlane * businessFacade.StopMachine(airPlane); * businessFacade.StartMachine(ship); //Dynamically decide which machine needs to start. In this case it's ship * businessFacade.StopMachine(ship);*/ // Polymorphism Example using Interface // Polymorphism Example before using Interface // Whenever new communication mode introduced [Email mode], communication facede needs to be updated and then the client needs to be updated accordingly // This makes the code fragile and error prone. Also the complete testing needs to be done. // So to achieve this, we have to use Polymorphism using Interface CommunicationFacade communicationFacade = new CommunicationFacade(); /*VoiceMode voiceMode = new VoiceMode(); * TextMode textMode = new TextMode(); * EmailMode emailMode = new EmailMode(); * * CommunicationFacade communicationFacade = new CommunicationFacade(); * Console.WriteLine("Please enter the type of communication mode you want to start [1 - Voice, 2 - Text, 3 - Email]"); * var mode = int.Parse(Console.ReadLine()); * if(mode == 1) * { * Thread.Sleep(1000); * communicationFacade.StartCommunication(voiceMode); * } else if (mode == 1) * { * Thread.Sleep(1000); * communicationFacade.StartCommunication(textMode); * } else if (mode == 1) * { * Thread.Sleep(1000); * communicationFacade.StartCommunication(emailMode); * }*/ // Polymorphism Example after using Interface // Advantage of Polymorphism : It promotes extensibility - By allowing new sub classes and methods to be added to a class hierarchy without having to modify existing programs. // Code became more loosly coupled. When we add a new communication there is no change in facade and client code // Facade doesn't do what it's doing. It just know to call the communicate method depends upon the object /*Console.WriteLine("Please enter the type of communication mode you want to start [1 - Voice, 2 - Text, 3 - Email]"); * var mode = int.Parse(Console.ReadLine()); * ICommunication factory = CommunicationFactory.Create(mode); * communicationFacade.StartCommunication(factory);*/ //Polymorphic behaviour of toString() method /*LambdaCollectionEx lambdaCollectionEx = new LambdaCollectionEx(); * lambdaCollectionEx.CheckStudentActivities(); */ //Core Abstraction Demo /*FrothyCreamingCoffee frothyCreamingCoffee = new FrothyCreamingCoffee(2,2,2); * PrepareCoffee(frothyCreamingCoffee); * * IceCoffee iceCoffee = new IceCoffee(3, 3, 3, 3); * PrepareCoffee(iceCoffee);*/ //Adding the new type of coffee is Rigid and not maintainable if we follow the above approach. //So create the abstract class to achieve this in Polymorphic way /*IceCoffee iceCoffee = new IceCoffee(3, 3, 3, 3); * PrepareCoffee(iceCoffee); * FrothyCreamingCoffee frothyCreamingCoffee = new FrothyCreamingCoffee(2, 2, 2); * PrepareCoffee(frothyCreamingCoffee); * Expresso expresso = new Expresso(); * PrepareCoffee(expresso);*/ //Interface Implementation // Tightly Coupled Way /*CreditCardPayment creditCardPayment = new CreditCardPayment(); * DebitCardPayment debitCardPayment = new DebitCardPayment(); * GooglePay googlePay = new GooglePay(); * PaymentManager paymentManager = new PaymentManager(debitCardPayment, creditCardPayment, googlePay); * paymentManager.ManagePayment();*/ // Loosely Coupled Way /*IPaymentMode paymentMode = PaymentModeFactory.Create(PaymentMode.DebitCard); * PaymentManagerLC paymentManagerLC = new PaymentManagerLC(paymentMode); * paymentManagerLC.ManagePayment();*/ //Interface - Unit Testing // A good project may well have more test code than production code // 1. Bugs are found easily and early // 2. Saves Time and Money // 3. A safety net for refactoring or any any enhancements // Concrete class implementation /*var userName = "******"; * WithdrawalService withdrawalService = new WithdrawalService(); * var isEligible = withdrawalService.IsEligibleToWithDrawal(userName, "pwd", 123, 1000); * if(isEligible) * Console.WriteLine($"{userName} is eligible to withdraw"); * else * Console.WriteLine($"{userName} is not eligible to withdraw");*/ // Interface implementation /*var userName = "******"; * IAuthenticationService authenticationService = new AuthenticationService(); * IBalanceCheckService balanceCheckService = new BalanceCheckerService(); * WithdrawalService withdrawalService = new WithdrawalService(authenticationService, balanceCheckService); * var isEligible = withdrawalService.IsEligibleToWithDrawal(userName, "pwd", 123, 1000); * if (isEligible) * Console.WriteLine($"{userName} is eligible to withdraw"); * else * Console.WriteLine($"{userName} is not eligible to withdraw"); */ // Interface - Dependency Injection // We should avoid to creating concrete class instances using new keyword instead you should depend on abstraction perhaps using an interface where by letting the dependencies // to be created by someone else for you in an abstract manner and use them by injecting whenever required. // DI - Creates objects outside of a class and provides those objects to a class through injection mainly constrctor // Interfaces achieves - Maintainability, Testability, Flexibility, Extensibility // IOC Container - Framework for implementing automatic dependency injection // Dependencies chain can become nested and complex. IOC container centralizes the creation of your dependencies and manages its lifetime. // Interface + Dependency Injection + IOC Container = Almost Zero "New" Keyword //AuthenticationService : _dataAccessService = new DataAccessService(); //BalanceCheckerService : _dataAccessService = new DataAccessService(); //Program.cs[Client] : IAuthenticationService authenticationService = new AuthenticationService(); IBalanceCheckService balanceCheckService = new BalanceCheckerService(); /*var userName = "******"; * WithdrawalService withdrawalService = AppunityContainer.RegisterService(); * var isEligible = withdrawalService.IsEligibleToWithDrawal(userName, "pwd", 123, 1000); * if (isEligible) * Console.WriteLine($"{userName} is eligible to withdraw"); * else * Console.WriteLine($"{userName} is not eligible to withdraw");*/ // Enum implementation using SmartEnum NuGet package CustomerEnumDemo customerEnumDemo = new CustomerEnumDemo(); customerEnumDemo.Id = 123; customerEnumDemo.Type = new AbstractCustomerType.RegularType(); Console.WriteLine($"Discount for {customerEnumDemo.Type.Name} type is : {customerEnumDemo.CalculateDiscount(123)}"); Console.ReadKey(); }