fork(2) download
  1. using System;
  2. using System.Numerics;
  3.  
  4. public class Test
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.WriteLine(Operation.Add(2, 2));
  11. Console.WriteLine(Operation.Add(1.2, 3.4));
  12. }
  13. }
  14.  
  15. class Operation
  16. {
  17. public static T Add<T>(T t1, T t2) => OperationImpl<T>.add(t1, t2);
  18. public static T Subtract<T>(T t1, T t2) => OperationImpl<T>.subtract(t1, t2);
  19.  
  20. class OperationImpl<T>
  21. {
  22. public static Func<T, T, T> add, subtract;
  23.  
  24. static Func<T, T, T> ForceCast<U>(Func<U, U, U> f) => (Func<T, T, T>)(object)f;
  25.  
  26. static OperationImpl()
  27. {
  28. if (typeof(T) == typeof(int))
  29. {
  30. add = ForceCast<int>((x, y) => x + y);
  31. subtract = ForceCast<int>((x, y) => x - y);
  32. }
  33. else if (typeof(T) == typeof(double))
  34. {
  35. add = ForceCast<double>((x, y) => x + y);
  36. subtract = ForceCast<double>((x, y) => x - y);
  37. }
  38. else if (typeof(T) == typeof(Complex))
  39. {
  40. add = ForceCast<Complex>((x, y) => x + y);
  41. subtract = ForceCast<Complex>((x, y) => x - y);
  42. }
  43. else
  44. {
  45. throw new NotSupportedException(
  46. $"Operations on type {typeof(T).Name} are not supported");
  47. }
  48. }
  49. }
  50. }
  51. }
Success #stdin #stdout 0.01s 131776KB
stdin
Standard input is empty
stdout
4
4.6