kunkunwoo@blog:~$ 거누권의 생각깜지

ref, out 예제

· 배운 것 · 2분 읽기

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Diagnostics.CodeAnalysis;

namespace ConsoleApp1
{  
    class Program
    {
        //out도 call by reference다.
        //ref와 다른점은 값을 할당만 가능, 무조건 사용해야됨
        static void Swap(ref int a, ref int b)
        {
            int tmp = a;
            a = b;
            b = tmp;
        }
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            Swap(ref a, ref b);
            Console.WriteLine("a = {0}, b = {1}", a, b);
        }
    }
}