class Program { class Thing { } class Asteroid : Thing { } class Spaceship : Thing { } static void CollideWithImpl(Asteroid x, Asteroid y) { Console.WriteLine("Asteroid collides with Asteroid"); } static void CollideWithImpl(Asteroid x, Spaceship y) { Console.WriteLine("Asteroid collides with Spaceship"); } static void CollideWithImpl(Spaceship x, Asteroid y) { Console.WriteLine("Spaceship collides with Asteroid"); } static void CollideWithImpl(Spaceship x, Spaceship y) { Console.WriteLine("Spaceship collides with Spaceship"); } static void CollideWith(Thing x, Thing y) { dynamic a = x; dynamic b = y; CollideWithImpl(a, b); } static void Main(string[] args) { var asteroid = new Asteroid(); var spaceship = new Spaceship(); CollideWith(asteroid, spaceship); CollideWith(spaceship, spaceship); } }
class Program { interface ICollidable { void CollideWith(ICollidable other); } class Asteroid : ICollidable { public void CollideWith(Asteroid other) { Console.WriteLine("Asteroid collides with Asteroid"); } public void CollideWith(Spaceship spaceship) { Console.WriteLine("Asteroid collides with Spaceship"); } public void CollideWith(ICollidable other) { other.CollideWith(this); } } class Spaceship : ICollidable { public void CollideWith(ICollidable other) { other.CollideWith(this); } public void CollideWith(Asteroid asteroid) { Console.WriteLine("Spaceship collides with Asteroid"); } public void CollideWith(Spaceship spaceship) { Console.WriteLine("Spaceship collides with Spaceship"); } } static void Main(string[] args) { var asteroid = new Asteroid(); var spaceship = new Spaceship(); asteroid.CollideWith(spaceship); asteroid.CollideWith(asteroid); } }
var asteroid = new Asteroid(); ICollidable collidable = asteroid;
List<Asteroid> asteroids = new List<Asteroid>(); IEnumerable<ICollidable> collidables = asteroids;
public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); }
interface ICustomInterface<out T> { T Do(T target); //compile-time error T Do(IList<T> targets); //compile-time error }
public interface IReader<T> { T Read(T[] arr, int index); }
public interface IReader<T> where T : class { T Read(T[] arr, int index); }
public class SignedIntegersReader : IReader<Int32>, IReader<Int16>, IReader<Int64> { int IReader<int>.Read(int[] arr, int index) { return arr[index]; } short IReader<short>.Read(short[] arr, int index) { return arr[index]; } long IReader<long>.Read(long[] arr, int index) { return arr[index]; } }
public static class ReaderExtensions { public static T Read<TReader, T>(this TReader reader, T[] arr, int index) where TReader : IReader<T> { return reader.Read(arr, index); } } class Program { static void Main(string[] args) { var reader = new SignedIntegersReader(); var arr = new int[] {128, 256}; for (int i = 0; i < arr.Length; i++) { Console.WriteLine("Reader result: {0}", reader.Read(arr, i)); } } }
class Program { static void Main(string[] args) { var reader = new SignedIntegersReader(); var arr = new float[] {128.0f, 256.0f}; for (int i = 0; i < arr.Length; i++) { Console.WriteLine("Reader result: {0}", reader.Read(arr, i)); //compile-time error } } }
public interface IReader<T> { T Read(T[] arr, int index); bool Supports<TType>(); } public class SignedIntegersReader : IReader<Int32>, IReader<Int16>, IReader<Int64> { int IReader<int>.Read(int[] arr, int index) { return arr[index]; } short IReader<short>.Read(short[] arr, int index) { return arr[index]; } long IReader<long>.Read(long[] arr, int index) { return arr[index]; } public bool Supports<TType>() { return this as IReader<TType> != null; } }
public class DefaultReader<T> : IReader<T> { private IReader<T> _reader = new SignedIntegersReader() as IReader<T>; public T Read(T[] arr, int index) { if (_reader != null) { return _reader.Read(arr, index); } return default(T); } public bool Supports<TType>() { return _reader.Supports<TType>(); } }
class Program { static void Main(string[] args) { var reader = new DefaultReader<int>(); var arr = new int[] { 128, 256 }; if (reader.Supports<int>()) { for (int i = 0; i < arr.Length; i++) { Console.WriteLine("Reader result: {0}", reader.Read(arr, i)); } } } }
Source: https://habr.com/ru/post/201996/
All Articles