class Product
{
private int price = 100;
public ref int GetPrice()
{
return ref price;
}
public void PrintPrice()
{
Console.WriteLine($"Price: {price}");
}
}
internal class Program
{
public static void Main(string[] args)
{
Product carrot = new Product();
carrot.PrintPrice();
ref int ref_local_price = ref carrot.GetPrice();
int normal_local_price = carrot.GetPrice();
ref_local_price = 10;
Console.WriteLine(ref_local_price);
Console.WriteLine(normal_local_price);
carrot.PrintPrice();
}
}
/*
Price: 100
10
100
Price: 10
*/
return: 값을 반환
ref return: 참조를 반환
'c#' 카테고리의 다른 글
인덱서 (0) | 2021.04.16 |
---|---|
c# Auto properties (0) | 2021.04.15 |
가변길이 매개변수 params (0) | 2021.04.14 |
c# out 키워드 두 개 이상의 결과를 내보낼 때 사용 (0) | 2021.04.13 |
C# call by value, call by reference (0) | 2021.04.13 |