참조 ref

참조하기 위해서는 ref 를 사용해야 합니다.

using System;
using static System.Console;

namespace SwapByRef
{
    class MainApp
    {
        static void Swap(ref int a, ref int b) {
            int tmp = b;
            b = a;
            a = tmp;
        }
    
        static void Main(string[] args)
        {
            int x = 3;
            int y = 4;
            WriteLine($"바꾸기 전 값: {x}, {y}");
            Swap(ref x, ref y);
            WriteLine($"바꾸기 전 값: {x}, {y}");
        }
    }
}
using System;
using static System.Console;

namespace RefReturn
{
    class Product
    {
        private int price = 100;

        public ref int GetPrice()
        {
            return ref price;
        }

        public void PrintPrice()
        {
            WriteLine(price);
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Product carrot = new Product();
            ref int ref_local_price = ref carrot.GetPrice();
            int normal_local_price = carrot.GetPrice();

            carrot.PrintPrice();
            WriteLine(ref_local_price);
            WriteLine(normal_local_price);

            ref_local_price = 200;

            carrot.PrintPrice();
            WriteLine(ref_local_price);
            WriteLine(normal_local_price);
        }
    }
}

 

out

out 키워드는 메서드에서 값을 반환하기 위해 사용되는 매개변수로, 메서드에서 반환값 이외에 추가로 값을 반환하거나 메서드 내에서 초기화가 보장되어야 할 때 사용됩니다.

using System;

class MainApp
{
    static void GetCoordinates(out int x, out int y)
    {
        x = 10;
        y = 20;
    }

    static void Main()
    {
        int x, y;
        GetCoordinates(out x, out y);
        Console.WriteLine($"Coordinates: x={x}, y={y}");
    }
}
  • ref 와 동일한 기능을 수행하는 것 같지만 ref 는 매개변수에 결과를 저장하지 않으면 컴파일러가 경고 메시지를 나타내지 않습니다.
  • out 을 사용하면 매개변수에 결과를 저장하지 않으면 컴파일러가 에러 메시지를 출력합니다.
  • 추가로 out 으로 명시해준 변수는 따로 이전에 값을 추가하지 않아도 새로 생성됩니다, 아래 코드처럼 말이죠
using System;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int a, int b, out int quotient, out int remainder)
        {
            quotient = a / b;
            remainder = a % b;
        }

        static void Main(string[] args) 
        {
            int a = 20;
            int b = 3;

            Divide(a, b, out int c, out int d);
            WriteLine($"a:{a}, b:{b}, a/b:{c}, a%b:{d}");
        }
    }
}

 

try catch

C# 에서도 try catch 문이 존재합니다.

예외처리를 할 수 있도록 존재하며 다음과 같이 사용됩니다.

using System;
namespace AccessModifier
{
    class WaterHeater
    {
        protected int temperature;

        public void SetTemperature(int temperature)
        {
            if (temperature < -5 || temperature > 42)
            {
                throw new Exception("Out of temperature range");
            }

            this.temperature = temperature;
        }

        internal void TurnOnWater()
        {
            Console.WriteLine($"Turn on water : {temperature}");
        }
    }

    class MainApp 
    {
        static void Main(string[] args) 
        {
            try
            {
                WaterHeater heater = new WaterHeater();
                heater.SetTemperature(20);
                heater.TurnOnWater();

                heater.SetTemperature(-2);
                heater.TurnOnWater();

                heater.SetTemperature(50);
                heater.TurnOnWater();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ytw_developer