fork(1) download
  1. using System;
  2.  
  3. class Program
  4. {
  5. private interface ISomeType
  6. {
  7. }
  8.  
  9. private class SomeType : ISomeType
  10. {
  11. }
  12.  
  13.  
  14. private static void DoWorkingSwap<TItem>(TItem[] array)
  15. {
  16. var temp = array[0];
  17. array[0] = array[1];
  18. array[1] = temp;
  19. }
  20.  
  21.  
  22. private static void Swap<TItem>(ref TItem firstItem, ref TItem secondItem)
  23. {
  24. var temp = firstItem;
  25. firstItem = secondItem;
  26. secondItem = temp;
  27. }
  28.  
  29. private static void DoFaultySwap<TItem>(TItem[] array)
  30. {
  31. Swap(ref array[0], ref array[1]);
  32. }
  33.  
  34.  
  35. private static void Main(string[] args)
  36. {
  37. // ВАЖНО: обратите внимание на тип переменной под массив и тип самого массива
  38. ISomeType[] array = new SomeType[2]
  39. {
  40. new SomeType(),
  41. new SomeType()
  42. };
  43.  
  44. Console.Write("Doing working swap... ");
  45. try
  46. {
  47. DoWorkingSwap(array);
  48. Console.WriteLine("succeeded.");
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine("FAILED: {0}", ex);
  53. }
  54.  
  55. Console.Write("Doing faulty swap... ");
  56. try
  57. {
  58. DoFaultySwap(array);
  59. Console.WriteLine("succeeded.");
  60. }
  61. catch (Exception ex)
  62. {
  63. Console.WriteLine("FAILED: {0}", ex);
  64. }
  65. }
  66. }
Success #stdin #stdout 0.05s 24016KB
stdin
Standard input is empty
stdout
Doing working swap... succeeded.
Doing faulty swap... FAILED: System.ArrayTypeMismatchException: Source array type cannot be assigned to destination array type.
  at Program.DoFaultySwap[ISomeType] (ISomeType[] array) [0x00000] in <filename unknown>:0 
  at Program.Main (System.String[] args) [0x00000] in <filename unknown>:0