//handler bottone uguale private void btn_ug_Click(object sender, EventArgs e) { int n, d, op; n = Convert.ToInt32(text_n1.Text); d = Convert.ToInt32(text_d1.Text); f1 = new Frazione(n, d); n = Convert.ToInt32(text_n2.Text); d = Convert.ToInt32(text_d2.Text); f2 = new Frazione(n, d); op = cb_op.SelectedIndex; switch (op) { case 0: //+ f3 = f1 + f2; break; case 1: //- f3 = f1 - f2; break; case 2: //x f3 = f1 * f2; break; case 3: //: f3 = f1 / f2; break; } text_nris.Text = f3.n.ToString(); text_dris.Text = f3.d.ToString(); }
static void Main(string[] args) { using DisposableClass dispClass = new(); Console.WriteLine("=== Week 2 - Day 3 ==="); #region Delegate BinaryOperation somma = Add; int result = somma(3, 2); // = Add(3, 2) somma = Subtract; // somma = Reverse; // ERRORE !!! result = somma(3, 2); // = Subtract(3, 2) PrintResult(somma, 3, 2); PrintResult(Add, 3, 2); // Add รจ una BinaryOperation Func <int, int, int> add = Add; List <Person> people = new List <Person>(); WhereFunction funct = WhereName; //people.Where(funct); // al momento no :( Func <Person, bool> whereClause = WhereName; //static bool WhereName(Person p) //{ // return p.FirstName == "Roberto"; //} // EQUIVALE A whereClause = p => { return(p.FirstNameProp == "Roberto"); }; // EQUIVALE A whereClause = p => p.FirstNameProp == "Roberto"; // In-Line funct, Arrow funct, lambda expression IEnumerable <Person> selectedPeople = people.Where(whereClause); // LINQ #endregion #region Nullable Types int i = 9; int?i2 = null; int i2Value = i2.HasValue ? i2.Value : 99; i2Value = i2.GetValueOrDefault(99); Person p1 = new Person { FirstNameProp = "Mario", LastNameProp = "Rossi" }; if (p1.Father != null) { string first = p1.Father.FirstNameProp != "" ? p1.Father.FirstNameProp : "Orfano"; Console.WriteLine("{0}", first); } else { Console.WriteLine("Orfano"); } Console.WriteLine("{0}", p1.Father?.Father?.FirstNameProp ?? "Niente Nonno"); return; #endregion #region Casting Frazione es5f1 = new(3, 2); Frazione es5f2 = new(1, 6); Console.WriteLine("es5f1 > es5f2 : {0}", (es5f1.CompareTo(es5f2) == 1)); // EQUIVALE A ... Console.WriteLine("es5f1 > es5f2 : {0}", (es5f1 > es5f2)); ComplexNumber es51 = new(3, 4); ComplexNumber es52 = new(3, 4); Console.WriteLine("es51 == es52 (ref): {0}", object.ReferenceEquals(es51, es52)); Console.WriteLine("es51 == es52 (1): {0}", (es51 == es52)); es52 = new(1, 4); Console.WriteLine("es51 == es52 (2): {0}", (es51 == es52)); double dbl51 = (double)es51; Console.WriteLine("Casting Complex to double {0}", dbl51); ComplexNumber es53 = 5; Console.WriteLine("Casting double to Complex ({0}, {1})", es53.Real, es53.Immag); return; Currency c1 = new(10, "EUR"); Currency c2 = new(10, "USD"); decimal d1 = (decimal)c1 + (decimal)c2; Currency.DefaultCurrency = "USD"; Currency c3 = d1; Frazione f = new(3, 2); Frazione f99 = new(3, 2); //f.Equals(f99); //f == f99; Console.WriteLine("f == f1: {0}", (f == f99)); double d = f; int value = (int)f; Console.WriteLine("Casting Frazione to double {0}", d); Console.WriteLine("Casting Frazione to int {0}", value); return; #endregion #region Esercitazione 4 ComplexNumber cpx1 = new("(1,2)"); Console.WriteLine($"CPX2: {cpx1.Real}, {cpx1.Immag}"); ComplexNumber cpx2 = new(3, -5); Console.WriteLine($"CPX2: {cpx2.Real}, {cpx2.Immag}"); var cpx3 = -cpx2; Console.WriteLine($"CPX3: {cpx3.Real}, {cpx3.Immag}"); var cpx4 = cpx1 + cpx2; Console.WriteLine($"CPX4: {cpx4.Real}, {cpx4.Immag}"); return; #endregion #region Op Overloading Frazione f1 = new(2, 3); Frazione f2 = new(5, 9); Frazione f3 = f1.Somma(f2); // ovvero ... f3 = -f1; f3 = f1 + f2; if (f3) { } // a = b - c => operatore binario // a = -b => operatore unario // a++ #endregion #region Interfacce DataService dataSvc = new(); dataSvc.NotInInterface(); InMemoryDataService mDataSvc = new(); MainBusinessLayer bl = new(mDataSvc); #endregion #region Esercitazione 3 Product prod1 = new(); Product prod2 = new("PRD001", 100); Product prod3 = new("PRD002", "SSD 500Gb", 34.8M); if (prod2.DiscountedPrice(20, out decimal discountedPrice)) { Console.WriteLine($"Discounted prod2 (20%): {discountedPrice}"); } else { Console.WriteLine($"Discount rate out of valid range."); } Console.WriteLine(prod3.ProductAge()); if (prod3.SaveProduct(@"C:\Temp")) { Console.WriteLine("Salvato!"); } else { Console.WriteLine("Errore!"); } return; #endregion #region Ctors Person p = new("Mario", "Rossi"); Person p2 = new("Paolo"); Person p3 = new(); #endregion #region Nested types HelpDeskTicket ticket = new HelpDeskTicket { Id = 1, State = HelpDeskTicket.HelpDeskTicketState.New, Owner = new HelpDeskTicket.TicketOwner() }; HelpDeskTicket.TicketOwner owner = new(); HelpDeskTicket t1 = new HelpDeskTicket { Id = 1, State = HelpDeskTicket.HelpDeskTicketState.New, Owner = owner }; #endregion } // Dispose() ...