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.

528 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
  83. Pingback: Northwest Pharmacy
  84. Pingback: canadianpharmacy
  85. Pingback: rftrip.ru
  86. Pingback: dolpsy.ru
  87. Pingback: kin0shki.ru
  88. Pingback: 3o9cpydyue4s8.ru
  89. Pingback: stromectol demodex
  90. Pingback: stromectol stock
  91. Pingback: mb588.ru
  92. Pingback: buy stromectol uk
  93. Pingback: newsukraine.ru
  94. Pingback: edu-design.ru
  95. Pingback: tftl.ru
  96. Pingback: brutv
  97. Pingback: site 2023
  98. Pingback: drugs for sale
  99. Pingback: canadian pharmacys
  100. Pingback: online drug store
  101. Pingback: online drug store
  102. Pingback: pharmacy canada
  103. Pingback: sitestats01
  104. Pingback: 1c789.ru
  105. Pingback: cttdu.ru
  106. Pingback: online drug store
  107. Pingback: buy viagra usa
  108. Pingback: canada rx
  109. Pingback: 1703
  110. Pingback: hdserial2023.ru
  111. Pingback: drugstore online
  112. Pingback: serialhd2023.ru
  113. Pingback: matchonline2022.ru
  114. Pingback: bit.ly/3OEzOZR
  115. Pingback: bit.ly/3gGFqGq
  116. Pingback: bit.ly/3ARFdXA
  117. Pingback: bit.ly/3ig2UT5
  118. Pingback: bit.ly/3GQNK0J
  119. Pingback: bep5w0Df
  120. Pingback: online pharmacies
  121. Pingback: www
  122. Pingback: icf
  123. Pingback: 24hours-news
  124. Pingback: rusnewsweek
  125. Pingback: uluro-ado
  126. Pingback: irannews.ru
  127. Pingback: klondayk2022
  128. Pingback: canada pharmacy
  129. Pingback: tqmFEB3B
  130. Pingback: canada pharmacy
  131. Pingback: 2022-film
  132. Pingback: Northwest Pharmacy
  133. Pingback: mangalib
  134. Pingback: x
  135. Pingback: 9xflix
  136. Pingback: xnxx
  137. Pingback: 123movies
  138. Pingback: online drug store
  139. Pingback: kinokrad
  140. Pingback: batmanapollo
  141. Pingback: www.dibiz.comgdooc
  142. Pingback: buy viagra now
  143. Pingback: canadian drug
  144. Pingback: northwestpharmacy
  145. Pingback: vsovezdeisrazu
  146. Pingback: 2023
  147. Pingback: ipsychologos
  148. Pingback: yug-grib.ru
  149. Pingback: studio-tatuage.ru
  150. Pingback: video.vipspark.ru
  151. Pingback: vitaliy-abdulov.ru
  152. Pingback: psychophysics.ru
  153. Pingback: canada drug

Comments are closed.