Чем отличается ref и out - C#
Формулировка задачи:
Добрый вечер.
Решил потренироваться массивчики делать. Сделал парочку задач:
1
2
Подскажите пожалуйста, как эти же задачи можно сделать с помощью ref и out? И ещё был бы очень признателен, если бы объяснили как они работают.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static int[] Input()
{
Console.WriteLine("Введите размерность массива");
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; ++i)
{
Console.Write("a[{0}]= ", i);
a[i] = int.Parse(Console.ReadLine());
}
return a;
}
static void Print(int [] a)
{
for (int i = 0; i < a.Length; ++i)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
static void Change ( int [] a)
{
int g = 10;
var result = a.Select(e => (e < g) ? g : e);
Console.Write("Результат: ");
foreach ( var t in result)
{
Console.Write(t + " ");
}
Console.ReadLine();
}
static void Main(string[] args)
{
int[] myArray = Input();
Console.WriteLine("Исходный массив:");
Print(myArray);
Change(myArray);
Console.WriteLine("Измененный массив:");
Print(myArray);
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static int[] Input()
{
Console.WriteLine("Введите размерность массива");
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; ++i)
{
Console.Write("a[{0}]= ", i);
a[i] = int.Parse(Console.ReadLine());
}
return a;
}
static void Print(int[] a)
{
for (int i = 0; i < a.Length; ++i)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
static void Change(int[] a)
{
int l = 10;
int j = 20;
var result = a.Select((x) => x >= l && x <= j ? 0 : x);
Console.Write("Результат: ");
foreach (var t in result)
{
Console.Write(t + " ");
}
Console.ReadLine();
}
static void Main(string[] args)
{
int[] myArray = Input();
Console.WriteLine("Исходный массив:");
Print(myArray);
Change(myArray);
Console.WriteLine("Измененный массив:");
Print(myArray);
}
}
}Решение задачи: «Чем отличается ref и out»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static int[] Input()
{
Console.WriteLine("Введите размерность массива");
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; ++i)
{
Console.Write("a[{0}]= ", i);
a[i] = int.Parse(Console.ReadLine());
}
return a;
}
static void Print(ref int[] a)
{
for (int i = 0; i < a.Length; ++i)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
static void Change(ref int[] a)
{
int g = 10;
a = a.Select(e => (e < g) ? g : e).ToArray<int>();
Console.Write("Результат: ");
foreach (var t in a)
{
Console.Write(t + " ");
}
Console.ReadLine();
}
static void Main(string[] args)
{
int[] myArray = Input();
Console.WriteLine("Исходный массив:");
Print(ref myArray);
Change(ref myArray);
Console.WriteLine("Измененный массив:");
Print(ref myArray);
}
}
}