Advertisement
Ne-Biolog

Untitled

Jun 2nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace lab8
  6. {
  7. class MainClass
  8. {
  9. delegate void Print();
  10. delegate string Merge(int a, string b, string c);
  11. delegate void Merge2(Print x, string d);
  12.  
  13. public class MyEvent
  14. {
  15. public delegate void Get();
  16. public event Get zzz;
  17.  
  18. public void Go()
  19. {
  20. if(zzz != null)
  21. zzz();
  22. }
  23. }
  24.  
  25. public class Person : IComparable<Person>
  26. {
  27. protected int age;
  28. protected string name, surname;
  29.  
  30. public int CompareTo(Person other)
  31. {
  32. return other.age.CompareTo(this.age);
  33. }
  34.  
  35. public Person(int age, string name, string surname)
  36. {
  37. try
  38. {
  39. if (age < 0) throw new Exception();
  40. this.age = age;
  41. this.name = name;
  42. this.surname = surname;
  43. }
  44. catch
  45. {
  46. Console.WriteLine("Error : Age < 0");
  47. //Environment.Exit(0);
  48. }
  49.  
  50. }
  51.  
  52. public virtual void Print()
  53. {
  54. Merge Foo = (x, y, z) => ("Age = " + x + "\nName = " + y + "\nSurname = " + z);
  55. string ans = Foo(age, name, surname);
  56. Console.WriteLine(ans);
  57. }
  58. }
  59.  
  60. public class Student : Person
  61. {
  62. protected string university;
  63.  
  64. public Student(int age, string name, string surname, string university)
  65. : base(age, name, surname)
  66. {
  67. this.university = university;
  68. }
  69.  
  70. readonly Merge2 Foo = delegate (Print func, string x)
  71. {
  72. func();
  73. Console.WriteLine("University = " + x + "\n");
  74. };
  75.  
  76. public override void Print()
  77. {
  78. Foo(base.Print, university);
  79. }
  80.  
  81. }
  82.  
  83. public static void Main(string[] args)
  84. {
  85. List<Person> arr = new List<Person>();
  86. arr.Add(new Person(-1, "qqq", "fdfd"));
  87. arr.Add(new Person(132, "pppp", "fdjf"));
  88. arr.Add(new Student(1333, "fjkd", "qqq", "222"));
  89.  
  90. MyEvent x = new MyEvent();
  91.  
  92. foreach(var item in arr)
  93. {
  94. x.zzz += item.Print;
  95. }
  96. x.Go();
  97.  
  98. }
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement