fork download
  1. using System;
  2.  
  3. namespace ConsoleApplication25
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Console.WriteLine("Hello\tworld\r\n");
  10. Console.WriteLine(@"Hello
  11. World");
  12.  
  13. double f = 42;
  14. int i = (int) f;
  15.  
  16. var products = new[]
  17. {
  18. new Product("Milk", 100),
  19. new Product("Bread", 150.8m),
  20. new SuperProduct("!!!"),
  21. };
  22.  
  23. Console.WriteLine(products.Length);
  24. Console.WriteLine(products[0] == products[1]);
  25. Console.WriteLine(products[0] == new Product("Milk", 100));
  26.  
  27. foreach (var product in products)
  28. {
  29. Console.WriteLine(product);
  30. }
  31.  
  32. Console.WriteLine(new Product(null, products[0].Price).Price + "$");
  33.  
  34. var p = new SuperProduct(null);
  35. p.SomeEvent += (sender, s) =>
  36. {
  37. Console.WriteLine("Hello event");
  38. };
  39. p.Foo().Foo();
  40.  
  41. Console.WriteLine(ReverseString("Reversed string"));
  42. Console.WriteLine("Reversed string".MyReverse());
  43. }
  44.  
  45. public static string ReverseString(string str)
  46. {
  47. if (str.Length == 0)
  48. return str;
  49. return str[str.Length - 1] + ReverseString(str.Substring(0, str.Length - 1));
  50. }
  51.  
  52. class Product : IEquatable<Product>
  53. {
  54. public Product(string name, decimal price)
  55. {
  56. Name = name;
  57. Price = price;
  58. }
  59.  
  60. public string Name { get; }
  61. public decimal Price { get; }
  62.  
  63. public delegate void MyEventHandler(object sender, string arg);
  64.  
  65. public event MyEventHandler SomeEvent;
  66.  
  67. protected virtual void OnSomeEvent(string arg)
  68. {
  69. SomeEvent?.Invoke(this, arg);
  70. }
  71.  
  72. public override string ToString()
  73. {
  74. return $"Name: {Name}, Price: {Price}";
  75. }
  76.  
  77. public bool Equals(Product other)
  78. {
  79. if (ReferenceEquals(null, other)) return false;
  80. if (ReferenceEquals(this, other)) return true;
  81. return string.Equals(Name, other.Name) && Price == other.Price;
  82. }
  83.  
  84. public override bool Equals(object obj)
  85. {
  86. if (ReferenceEquals(null, obj)) return false;
  87. if (ReferenceEquals(this, obj)) return true;
  88. if (obj.GetType() != this.GetType()) return false;
  89. return Equals((Product) obj);
  90. }
  91.  
  92. public override int GetHashCode()
  93. {
  94. unchecked
  95. {
  96. return ((Name != null ? Name.GetHashCode() : 0)*397) ^ Price.GetHashCode();
  97. }
  98. }
  99.  
  100. public static bool operator ==(Product left, Product right)
  101. {
  102. return Equals(left, right);
  103. }
  104.  
  105. public static bool operator !=(Product left, Product right)
  106. {
  107. return !Equals(left, right);
  108. }
  109. }
  110.  
  111. class SuperProduct : Product
  112. {
  113. public SuperProduct(string name) : base(name, 99999)
  114. {
  115. }
  116.  
  117. public SuperProduct Foo()
  118. {
  119. OnSomeEvent(null);
  120. return this;
  121. }
  122. }
  123. }
  124.  
  125. public static class MyExtension
  126. {
  127. public static string MyReverse(this string obj)
  128. {
  129. return Program.ReverseString(obj);
  130. }
  131. }
  132. }
  133.  
Success #stdin #stdout 0.01s 131840KB
stdin
Standard input is empty
stdout
Hello	world

Hello
World
3
False
True
Name: Milk, Price: 100
Name: Bread, Price: 150.8
Name: !!!, Price: 99999
100$
Hello event
Hello event
gnirts desreveR
gnirts desreveR