using System;
namespace ConsoleApplication25
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello\tworld\r\n");
Console.WriteLine(@"Hello
World");
double f = 42;
int i = (int) f;
var products = new[]
{
new Product("Milk", 100),
new Product("Bread", 150.8m),
new SuperProduct("!!!"),
};
Console.WriteLine(products.Length);
Console.WriteLine(products[0] == products[1]);
Console.WriteLine(products[0] == new Product("Milk", 100));
foreach (var product in products)
{
Console.WriteLine(product);
}
Console.WriteLine(new Product(null, products[0].Price).Price + "$");
var p = new SuperProduct(null);
p.SomeEvent += (sender, s) =>
{
Console.WriteLine("Hello event");
};
p.Foo().Foo();
Console.WriteLine(ReverseString("Reversed string"));
Console.WriteLine("Reversed string".MyReverse());
}
public static string ReverseString(string str)
{
if (str.Length == 0)
return str;
return str[str.Length - 1] + ReverseString(str.Substring(0, str.Length - 1));
}
class Product : IEquatable<Product>
{
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
public string Name { get; }
public decimal Price { get; }
public delegate void MyEventHandler(object sender, string arg);
public event MyEventHandler SomeEvent;
protected virtual void OnSomeEvent(string arg)
{
SomeEvent?.Invoke(this, arg);
}
public override string ToString()
{
return $"Name: {Name}, Price: {Price}";
}
public bool Equals(Product other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(Name, other.Name) && Price == other.Price;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Product) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((Name != null ? Name.GetHashCode() : 0)*397) ^ Price.GetHashCode();
}
}
public static bool operator ==(Product left, Product right)
{
return Equals(left, right);
}
public static bool operator !=(Product left, Product right)
{
return !Equals(left, right);
}
}
class SuperProduct : Product
{
public SuperProduct(string name) : base(name, 99999)
{
}
public SuperProduct Foo()
{
OnSomeEvent(null);
return this;
}
}
}
public static class MyExtension
{
public static string MyReverse(this string obj)
{
return Program.ReverseString(obj);
}
}
}