Чем отличается ref и out - C#

Узнай цену своей работы

Формулировка задачи:

Добрый вечер. Решил потренироваться массивчики делать. Сделал парочку задач: 1
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static int[] Input()
  11. {
  12. Console.WriteLine("Введите размерность массива");
  13. int n = int.Parse(Console.ReadLine());
  14. int[] a = new int[n];
  15. for (int i = 0; i < n; ++i)
  16. {
  17. Console.Write("a[{0}]= ", i);
  18. a[i] = int.Parse(Console.ReadLine());
  19. }
  20. return a;
  21. }
  22. static void Print(int [] a)
  23. {
  24. for (int i = 0; i < a.Length; ++i)
  25. Console.Write("{0} ", a[i]);
  26. Console.WriteLine();
  27. }
  28. static void Change ( int [] a)
  29. {
  30. int g = 10;
  31. var result = a.Select(e => (e < g) ? g : e);
  32. Console.Write("Результат: ");
  33. foreach ( var t in result)
  34. {
  35. Console.Write(t + " ");
  36. }
  37. Console.ReadLine();
  38. }
  39. static void Main(string[] args)
  40. {
  41. int[] myArray = Input();
  42. Console.WriteLine("Исходный массив:");
  43. Print(myArray);
  44. Change(myArray);
  45. Console.WriteLine("Измененный массив:");
  46. Print(myArray);
  47. }
  48. }
  49. }
2
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication2
  7. {
  8. class Program
  9. {
  10. static int[] Input()
  11. {
  12. Console.WriteLine("Введите размерность массива");
  13. int n = int.Parse(Console.ReadLine());
  14. int[] a = new int[n];
  15. for (int i = 0; i < n; ++i)
  16. {
  17. Console.Write("a[{0}]= ", i);
  18. a[i] = int.Parse(Console.ReadLine());
  19. }
  20. return a;
  21. }
  22. static void Print(int[] a)
  23. {
  24. for (int i = 0; i < a.Length; ++i)
  25. Console.Write("{0} ", a[i]);
  26. Console.WriteLine();
  27. }
  28. static void Change(int[] a)
  29. {
  30. int l = 10;
  31. int j = 20;
  32. var result = a.Select((x) => x >= l && x <= j ? 0 : x);
  33. Console.Write("Результат: ");
  34. foreach (var t in result)
  35. {
  36. Console.Write(t + " ");
  37. }
  38. Console.ReadLine();
  39. }
  40. static void Main(string[] args)
  41. {
  42. int[] myArray = Input();
  43. Console.WriteLine("Исходный массив:");
  44. Print(myArray);
  45. Change(myArray);
  46. Console.WriteLine("Измененный массив:");
  47. Print(myArray);
  48. }
  49. }
  50. }
Подскажите пожалуйста, как эти же задачи можно сделать с помощью ref и out? И ещё был бы очень признателен, если бы объяснили как они работают.

Решение задачи: «Чем отличается ref и out»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.  
  12.         static int[] Input()
  13.         {
  14.             Console.WriteLine("Введите размерность массива");
  15.             int n = int.Parse(Console.ReadLine());
  16.             int[] a = new int[n];
  17.             for (int i = 0; i < n; ++i)
  18.             {
  19.                 Console.Write("a[{0}]= ", i);
  20.                 a[i] = int.Parse(Console.ReadLine());
  21.             }
  22.  
  23.             return a;
  24.         }
  25.  
  26.         static void Print(ref int[] a)
  27.         {
  28.             for (int i = 0; i < a.Length; ++i)
  29.                 Console.Write("{0} ", a[i]);
  30.             Console.WriteLine();
  31.         }
  32.  
  33.         static void Change(ref int[] a)
  34.         {
  35.             int g = 10;
  36.             a = a.Select(e => (e < g) ? g : e).ToArray<int>();
  37.  
  38.             Console.Write("Результат: ");
  39.  
  40.             foreach (var t in a)
  41.             {
  42.                 Console.Write(t + " ");
  43.             }
  44.  
  45.             Console.ReadLine();
  46.         }
  47.  
  48.         static void Main(string[] args)
  49.         {
  50.  
  51.             int[] myArray = Input();
  52.             Console.WriteLine("Исходный массив:");
  53.             Print(ref myArray);
  54.             Change(ref myArray);
  55.             Console.WriteLine("Измененный массив:");
  56.             Print(ref myArray);
  57.  
  58.         }
  59.     }
  60. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 3.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут