Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public static class Program
  2. {
  3. public interface IConvertible<T>
  4. {
  5. T Convert();
  6. T Convert<T>();
  7. }
  8. public class ConvertMe : IConvertible<int>, IConvertible<float>
  9. {
  10. public double Value;
  11. int IConvertible<int>.Convert() { return (int)Value; }
  12. float IConvertible<float>.Convert() { return (float)Value; }
  13. public T Convert<T>() { return ((IConvertible<T>)this).Convert(); }
  14. }
  15. public static void Main()
  16. {
  17. ConvertMe c = new ConvertMe();
  18. c.Value = 12.3456789;
  19. Console.WriteLine(c.Convert<int>());
  20. Console.WriteLine(c.Convert<float>());
  21. Console.ReadKey();
  22. }
  23. }
  24.  
  25. public interface IConvertible<T>
  26. {
  27. T Convert();
  28. }
  29.  
  30. public sealed class ConvertMe : IConvertible<int>, IConvertible<float>
  31. {
  32. public double Value;
  33. int IConvertible<int>.Convert() { return (int)Value; }
  34. float IConvertible<float>.Convert() { return (float)Value; }
  35. }
  36.  
  37. public static class Program
  38. {
  39. public static void Main()
  40. {
  41. ConvertMe c = new ConvertMe();
  42. c.Value = 12.3456789;
  43. Console.WriteLine(((IConvertible<int>)c).Convert());
  44. Console.WriteLine(((IConvertible<float>)c).Convert());
  45. }
  46. }
  47.  
  48. public static class Specialisation
  49. {
  50. public static Specialisation<T> For<T>()
  51. {
  52. return Specialisation<T>.Instance;
  53. }
  54. }
  55.  
  56. Specialised s = new Specialised();
  57. s.Value = 12.3456789;
  58. Console.WriteLine(s.SpecialisedMethod(Specialisation.For<int>()));
  59. Console.WriteLine(s.SpecialisedMethod(Specialisation.For<float>()));
  60. Console.ReadKey();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement