S_pot
210517 C#_ref(래퍼런스) 본문
static void Main(string[] args)
{
int iNum = 100; // 본명
ref int riNum = ref iNum; // 예명, 앞에 ref를 붙여주어야 한다. riNum에 200을 넣으면 iNum도 200이 된다.
riNum = 200;
Console.WriteLine("iNum = " + iNum);
}
static void Main2(string[] args)
{
uint iNum = 64; // 0x는 뒤에 숫자가 16진수를 의미
Console.WriteLine(iNum);
}
using System;
namespace _210517_006
{
class Program
{
static void Smart(ref int Number) // ref:래퍼런스(참조), 호출되는 메서드에서 매개변수로 사용되는 변수의 값을
{ // 영구적으로 변경할 때 사용
Number = Number + 200;
}
static void Main(string[] args)
{
int iNum = 100;
Console.WriteLine("before iNum = " + iNum); // ref를 적용하지 않으면 출력값은 100, 100이 나온다.
Smart(ref iNum); // ref를 적용하면 출력값은 100, 300이 나온다.
Console.WriteLine("after iNum = " + iNum);
}
}
}
'C#' 카테고리의 다른 글
210518 C#_소수 구하기 (0) | 2021.05.18 |
---|---|
210517 C#_class, 객체생성 (0) | 2021.05.17 |
210517 C#_Swap(스왑), 출력값 위치 바꾸기 (0) | 2021.05.17 |
210517 C#_메서드를 이용한 5칙연산 (0) | 2021.05.17 |
210517 C#_return, void, 반환값 (0) | 2021.05.17 |