Guest User

Untitled

a guest
Jun 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. Array.Copy(source2,2,target,3,2);
  2. source.CopyTo(target,0);
  3.  
  4. public static void Copy(Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length);
  5.  
  6. public void CopyTo(Array array, int index);
  7.  
  8. Array.Copy(source,2,target,2,2);
  9. source.CopyTo(target,0);
  10.  
  11. Array.Copy(source,2,ref target,2,2);//передача по ссылке
  12. source.CopyTo(ref target,0);//передача по ссылке
  13.  
  14. void Change(int[] array)
  15. {
  16. //изменяем элемент массива, можно без ref
  17. array[0] = 1;
  18. }
  19.  
  20. void ReasssignIncorrectly(int[] array)
  21. {
  22. //пересоздание массива, т.к. аргумент не ref, это не окажет влияния на переданную ссылку
  23. array = new[]{2};
  24. }
  25.  
  26. void ReasssignCorrectly(ref int[] array)
  27. {
  28. //пересоздается успешно, т.к. ref
  29. array = new[]{3};
  30. }
  31.  
  32. var a = new int[1];
  33. Console.WriteLine(a[0]);
  34. Change(a);
  35. Console.WriteLine(a[0]);
  36. //не изменит массив
  37. ReasssignIncorrectly(a);
  38. Console.WriteLine(a[0]);
  39. ReasssignCorrectly(ref a);
  40. Console.WriteLine(a[0]);
  41.  
  42. 0
  43. 1
  44. 1
  45. 3
Add Comment
Please, Sign In to add comment