[Csharp]ref์ out
๐ref
ref๋ฅผ ์ด์ฉํด ๋ ๋ณ์์ ๊ฐ์ ๋ฐ๊พธ๋ Swapํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.(ref ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์์ CopySwap ํจ์๋ Main ํจ์ ์์์์ a, b๊ฐ ์๋ก ๋ฐ๋์ง ์์ ๊ฒ์ ์ ์ ์๋ค.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
namespace CSharp
{
class Program
{
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static void CopySwap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
static void Main(string[] args)
{
int a = 1;
int b = 2;
ref int c = ref a;
Program.CopySwap(a, b); // a, b ๊ฐ์ด ๋ฐ๋์ง ์์
Console.WriteLine(a);
Console.WriteLine(b);
Program.Swap(ref a, ref b);
Console.WriteLine(a);
Console.WriteLine(b);
}
}
}
๐out
out ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด quotient๋ remainder ์ฒ๋ผ ๋ณ์์ ๊ฐ์ ํ ๋นํ์ง ์์๋ ํจ์์ ์ฌ์ฉํ ์ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace CSharp
{
class Program
{
static void Divide(int a, int b, out int result1, out int result2)
{
result1 = a / b;
result2 = a % b;
}
static void Main(string[] args)
{
int a = 5;
int b = 2;
int quotient;
int remainder;
Program.Divide(a, b, out quotient, out remainder);
Console.WriteLine(quotient);
Console.WriteLine(remainder);
}
}
}
Leave a comment