黄学彪
2019-11-25 5727cf0b9b54da0a191dd1e23cb5abf21320fbff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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<double, double> action, double value)
        {
            double result = action(value);
        }
 
        //使用Fun<in T,out TResult>来代替自定义的DoubleOp;
        //        <double, double>[] 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<T>(IList<T> sortArray, Func<T, T, bool> 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;
        }
 
    }
}