using System; using System.Collections.Generic; using UIKit; namespace GateWay.Ios { public class Application { // This is the main entry point of the application. static void Main(string[] args) { // if you want to use a different Application Delegate class from "AppDelegate" // you can specify it here. UIApplication.Main(args, null, "AppDelegate"); //Study P246 int x = 40; GetAString firstStringMethod = new GetAString(x.ToString); GetAString firstStringMethod1 = x.ToString; var balance = new Currency(34, 50); firstStringMethod1 = balance.ToString; firstStringMethod1 = new GetAString(Currency.GetCurrencyUnit); #region 简单的委托 DoubleOp[] operations = { MathOperation .MultiplyByTwo , MathOperation.Square }; for (int i = 0; i < operations.Length; i++) { // System.Console.WriteLine($ "Using operations[{i}]:); var aa = $"{operations[i]}"; ProcessAndDisplayNumber(operations[i], 2.0); ProcessAndDisplayNumber(operations[i], 7.9); ProcessAndDisplayNumber(operations[i], 21.4); } #region 冒泡排序 int[] sortArray = { 0, 5, 6, 2, 1 }; bool swapped = true; do { swapped = false; for (int i = 0; i < sortArray.Length - 1; i++) { if (sortArray[i] > sortArray[i + 1]) { int temp = sortArray[i]; sortArray[i] = sortArray[i + 1]; sortArray[i - 1] = temp; swapped = true; } } } while (swapped); //第二种方法 Employee[] employees = { new Employee ("A",20000), new Employee ("B",30000), new Employee ("C",40000), }; BubbleSorter.Sort(employees, Employee.CompareSalary); foreach (var employee in employees ) { var aa = employee; } #endregion } static void ProcessAndDisplayNumber1(Func action, double value) { double result = action(value); } //使用Fun来代替自定义的DoubleOp; // [] operations1 = // { // MathOperation.MultiplyByTwo , // MathOperation.Square //}; static void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); } class MathOperation { public static double MultiplyByTwo(double value) => value * 2; public static double Square(double value) => value * value; } delegate double DoubleOp(double x); #endregion private delegate string GetAString(); struct Currency { public uint Dollers; public ushort Cents; public Currency(uint dollers, ushort cents) { this.Dollers = dollers; this.Cents = cents; } public override string ToString() => $"${Dollers}.{Cents,2:00}"; public static string GetCurrencyUnit() => "Dollers"; public static explicit operator Currency(float value) { checked { uint dollers = (uint)value; ushort cents = (ushort)((value - dollers) + 100); return new Currency(dollers, cents); } } public static implicit operator float(Currency value) => value.Dollers + (value.Cents / 100.0f); public static implicit operator Currency(uint value) => new Currency(value, 0); public static implicit operator uint(Currency value) => value.Dollers; } class BubbleSorter { static public void Sort(IList sortArray, Func comparison) { bool swapped = true; do { swapped = false; for (int i = 0; i < sortArray.Count - 1;i++){ if(comparison (sortArray [i+1],sortArray [i])) { T temp = sortArray[i]; sortArray[i] = sortArray[i + 1]; sortArray[i + 1] = temp; swapped = true; } } } while (swapped); } } class Employee { public Employee (string name,decimal salary) { Name = name; Salary = Salary; } public string Name { get; } public Decimal Salary { get; private set; } public override string ToString() => $"{Name},{Salary:C}"; public static bool CompareSalary(Employee e1, Employee e2) => e1.Salary < e2.Salary; } } }