c#

C# call by value, call by reference

fd27 2021. 4. 13. 17:46
internal class Program
    {
        public static void SwapCallByValue(int a, int b)
        {
            int temp = b;
            b = a;
            a = temp;
        }
        
        public static void SwapCallByRef(ref int a, ref int b)
        {
            int temp = b;
            b = a;
            a = temp;
        }
        
        public static void Main(string[] args)
        {
            int x = 3;
            int y = 4;
            Console.WriteLine("x:{0}, y{1}",x, y);
            // SwapCallByValue(x, y);
            SwapCallByRef(ref x, ref y);
            Console.WriteLine("x:{0}, y{1}",x, y);
        }
    }

x: 3, y: 4

x: 4, y: 3