Presentation is loading. Please wait.

Presentation is loading. Please wait.

1) int i = int.Parse("99"); 2) delegate void IntMethodInvoker(uint x); 3) delegate double TwoLongsOp(long first, long second); 4) delegate string GetAString();

Similar presentations


Presentation on theme: "1) int i = int.Parse("99"); 2) delegate void IntMethodInvoker(uint x); 3) delegate double TwoLongsOp(long first, long second); 4) delegate string GetAString();"— Presentation transcript:

1 1) int i = int.Parse("99"); 2) delegate void IntMethodInvoker(uint x); 3) delegate double TwoLongsOp(long first, long second); 4) delegate string GetAString(); 5) public delegate string GetAString(); 6) private delegate string GetAString(); static void Main() { int x = 40; GetAString firstStringMethod = new GetAString(x.ToString) ; Console.WriteLine("Строка равна " + firstStringMethod()) ; // С firstStringMethod, инициализированным x.ToString(), // приведенный оператор эквивалентен следующему: // Console.WriteLine("Строка равна " + х.ToString()); 7) firstStringMethod(); firstStringMethod.Invoke(); 8) GetAString firstStringMethod = new GetAString(x.ToString); 9) GetAString firstStringMethod = x.ToString;

2 10) struct Currency { public uint Dollars; public ushort Cents; public Currency(uint dollars, ushort cents) { this.Dollars = dollars; this.Cents = cents; } public override string ToString() { return string.Format("${0}.{1,2:00}", Dollars,Cents); } public static string GetCurrencyUnit() { return "Доллар"; }

3 public static explicit operator Currency (float value) { checked { uint dollars = (uint)value; ushort cents = (ushort)((value-dollars)*100); return new Currency(dollars, cents); } public static implicit operator float (Currency value) { return value.Dollars + (value.Cents/100.0f); } public static implicit operator Currency (uint value) { return new Currency(value, 0); } public static implicit operator uint (Currency value) { return value.Dollars; }

4 11) private delegate string GetAString () ; static void Main() { int x = 40; GetAString firstStringMethod = x.ToString; Console.WriteLine("Строка равна {0}", firstStringMethod()); Currency balance = new Currency(34, 50); // firstStringMethod ссылается на метод экземпляра firstStringMethod = balance.ToString; Console.WriteLine("Строка равна {0}", firstStringMethod()); // firstStringMethod ссылается на статический метод firstStringMethod = new GetAString(Currency.GetCurrencyUnit); Console.WriteLine ("Строка равна {0}", firstStringMethod()) ; } 12) Строка равна 40 Строка равна $34.50 Строка равна Доллар

5 13) class MathsOperations { public static double MultiplyByTwo(double value) { return value*2; } public static double Square(double value) { return value*value; } 14) using System; namespace SimpleDelegate { delegate double DoubleOp(double x); class MainEntryPoint { static void Main() {

6 DoubleOp [] operations = { MathsOperations.MultiplyByTwo, MathsOperations.Square } for (int i=0; i<operations.Length; i++) { Console.WriteLine("Использование operations[{0}]:",i); ProcessAndDisplayNumber(operations[i], 2.0); ProcessAndDisplayNumber(operations[i], 7.94); ProcessAndDisplayNumber(operations[i], 1.414); Console.WriteLine(); } static void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); Console.WriteLine( "Значение равно {0}, результат операции равен {1}”,value,result); }

7 15) ProcessAndDisplayNumber(operations[i], 2.0); 16) - operations[i]  означает делегата (т.е. метод, представленный делегатом); - operations[i](2.0)  означает вызов метода делегата с передачей, ему параметров в скобках. 17) static void ProcessAndDisplayNumber(DoubleOp action, double value) 18) double result = action(value); 19) Использование operations[0]: Значение равно 2, результат операции равен 4 Значение равно 7.94, результат операции равен 15.88 Значение равно 1.414, результат операции равен 2.828 Использование operations[1] : Значение равно 2, результат операции равен 4 Значение равно 7.94, результат операции равен 63.0436 Значение равно 1.414, результат операции равен 1.999396 20) delegate double DoubleOp(double x); 21) Func [] operations = { MathOperations.MultiplyByTwo, MathOperations.Square } ;

8 22) double value) { double result = action(value); Console.WriteLine( "Значение равно {0}, результат операции равен (1}", value, result); } 23) 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]; sortArrayt[i] = sortArray[i + 1]; sortArray[i + 1] = temp; swapped = true; } } while (swapped);

9 24) static public void Sort (IList sortArray, Func co mparison) 25) 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); }

10 26) class Employee { public Employee(string name, decimal salary) { this.Name = name; this.Salary = salary; } public string Name { get; private set; } public decimal Salary { get; private set; ) public override string ToString() { return string.Format("{0}, {1:C)", Name, Salary); } public static bool CompareSalary(Employee e1, Employee e2) { return e1.Salary < e2.Salary; }

11 27) using System; namespace Wrox.ProCSharp.Delegates { class Program { static void Main() { Employee [] employees = { new Employee("Bugs Bunny", 20000), new Employee("Elmer Fudd", 10000), new Employee("Daffy Duck", 25000), new Employee("Wiley Coyote", 1000000.38), new Employee("Foghorn Leghorn", 23000), new Employee("RoadRunner", 50000)}; } BubbleSorter.Sort(employees, Employee.CompareSalary); foreach (var employee in employees) { Console.WriteLine(employee); }

12 28) Elmer Fudd, $10,000.00 Bugs Bunny, $20,000.00 Foghorn Leghorn, $23,000.00 Daffy Duck, $25,000.00 Road Runner, $50,000.00 Wiley Coyote, $1,000,000.38 29) class Program { static void Main() { Action operations = MathOperations.MultiplyByTwo; operations += MathOperations.Square; 30) Action operation1 = new MathOperations.MultiplyByTwo; Action operation2 = new MathOperations.Square; Action operations = operation1 + operation2;

13 31) class MathOperations { public static void MultiplyByTwo(double value) { double result = value*2; Console.WriteLine("Умножение на 2: (0} дает {1}",value,result); } public static void Square(double value) { double result = value*value; Console.WriteLine("Возведение в квадрат: {0} дает {1}", value, result); } 32) static void ProcessAndDisplayNumber(Action action, double value) { Console.WriteLine(); Console.WriteLine( "\nProcessAndDisplayNumber вызван со значением value = {0)" + value); action(value); }


Download ppt "1) int i = int.Parse("99"); 2) delegate void IntMethodInvoker(uint x); 3) delegate double TwoLongsOp(long first, long second); 4) delegate string GetAString();"

Similar presentations


Ads by Google