C# 7.0 – #5. reference return function

In addition to function input parameters, which can be passed by value (default) or by reference (with the keyword ref), in C# 7.0 the value return function can also be returned by reference with the same keyword; furthermore it can be stored in a local variable by reference; this local variable stores the memory address of the data and not the value, and then modifiyng this variable means that every variable that points at the same address would be modified too.

Example:

public struct Point
{
   public int X { get; set; }
   public int Y { get; set; }
}

Point[] arr = new[]
{
   new Point {X=12, Y=15 }, new Point {X=56, Y = 754}, 
   new Point {X=65, Y= 39}, new Point {X=21, Y=63}
};

var p = new Point { X = 65, Y = 39 };
ref Point place = ref Find(p, arr);
place.X = 99;
place.Y = 99;    // replaces 3rd Point element in the array
Console.WriteLine("X=" + arr[2].X + " " + "Y=" + arr[2].Y); // prints X=99 Y=99

public ref Point Find(Point p, Point[] points)
{
   for (int i = 0; i < points.Length; i++)
   {
      if (points[i].X == p.X && points[i].Y == p.Y)
      {
         return ref points[i]; // return the storage location, not the value
      }
   }
   throw new IndexOutOfRangeException($"{nameof(p)} not found");
}

In this example Point is a structure, and then it is a value type.
The variable called “arr” contains an array (a reference type) of this structure.
The Find method simply searches a value in that array, but it returns the reference of the Point structure founded, and not its value. You can notice the “ref” keyword before the return type and the “ref” keyword after the return statement. Then the code stores this reference in a local “ref” variable (called “place”) containing the reference to the Point structure searched (the third element of the array).
After that the values are modified (X=99; Y=99), and the array element is modified too, which proves that the memory address has been used instead of the value.

228 thoughts on “C# 7.0 – #5. reference return function

  1. Pingback: canadianpharmacy
  2. Pingback: canadian viagra
  3. Pingback: canadian drugstore
  4. Pingback: pharmacy canada
  5. Pingback: buy generic cialis
  6. Pingback: mazhor4sezon
  7. Pingback: canadian rx
  8. Pingback: filmfilmfilmes
  9. Pingback: gewrt.usluga.me
  10. Pingback: buy viagra usa
  11. Pingback: canadian pharmacy
  12. Pingback: canadian pharmacy
  13. Pingback: canada medication
  14. Pingback: canadianpharmacy
  15. Pingback: canadian cialis
  16. Pingback: dkyubn.bizwebs.com
  17. Pingback: Gz92uNNH
  18. Pingback: do-posle-psihologa
  19. Pingback: DPTPtNqS
  20. Pingback: qQ8KZZE6
  21. Pingback: D6tuzANh
  22. Pingback: SHKALA TONOV
  23. Pingback: chelovek-iz-90-h
  24. Pingback: drugs for sale
  25. Pingback: kwsde.zombeek.cz
  26. Pingback: 3Hk12Bl
  27. Pingback: 3NOZC44
  28. Pingback: 01211
  29. Pingback: tor-lyubov-i-grom
  30. Pingback: film-tor-2022
  31. Pingback: hd-tor-2022
  32. Pingback: hdorg2.ru
  33. Pingback: Psikholog
  34. Pingback: netstate.ru
  35. Pingback: Link
  36. Pingback: psy
  37. Pingback: bit.ly
  38. Pingback: cleantalkorg2.ru
  39. Pingback: bucha killings
  40. Pingback: War in Ukraine
  41. Pingback: Ukraine
  42. Pingback: site
  43. Pingback: stats
  44. Pingback: Ukraine-war
  45. Pingback: movies
  46. Pingback: gidonline
  47. Pingback: web
  48. Pingback: film.8filmov.ru
  49. Pingback: video
  50. Pingback: film
  51. Pingback: filmgoda.ru
  52. Pingback: rodnoe-kino-ru
  53. Pingback: stat.netstate.ru
  54. Pingback: sY5am
  55. Pingback: Dom drakona
  56. Pingback: JGXldbkj
  57. Pingback: aOuSjapt
  58. Pingback: ìûøëåíèå
  59. Pingback: psikholog moskva
  60. Pingback: Dim Drakona 2022
  61. Pingback: TwnE4zl6
  62. Pingback: psy 3CtwvjS
  63. Pingback: lalochesia
  64. Pingback: buy viagra uk
  65. Pingback: buy viagra 25mg
  66. Pingback: film onlinee
  67. Pingback: pharmacy
  68. Pingback: stromectol online
  69. Pingback: buy viagra usa
  70. Pingback: 3qAIwwN
  71. Pingback: video-2
  72. Pingback: stromectol generic
  73. Pingback: sezons.store
  74. Pingback: ivermectin
  75. Pingback: buy stromectol uk
  76. Pingback: psy-news.ru
  77. Pingback: canada rx
  78. Pingback: ivermectine
  79. Pingback: 000-1
  80. Pingback: 3SoTS32
  81. Pingback: 3DGofO7
  82. Pingback: Northwest Pharmacy

Comments are closed.