๐Ÿ“š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