C#에서 ref와 out

C# 책을 보는데 value 타입과 reference 타입에 대한게 나옴 그냥 간단하게 공부용으로 정리함.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class anotherclass{
public:
int id1;
int id2;
anotherclass() { id1 = 5; id2 = 7; }
void GetID(int &ID1, int &ID2){
ID1 = id1;
ID2 = id2;
}
}
void Main(){
anotherclass ac;
int aaa = 0;
int bbb = 0;
ac.GetID(aaa, bbb);
Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb);
}
class anotherclass{ public: int id1; int id2; anotherclass() { id1 = 5; id2 = 7; } void GetID(int &ID1, int &ID2){ ID1 = id1; ID2 = id2; } } void Main(){ anotherclass ac; int aaa = 0; int bbb = 0; ac.GetID(aaa, bbb); Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb); }
class anotherclass{
	public:
	int id1;
	int id2;
	anotherclass() { id1 = 5; id2 = 7; }
	
	void GetID(int &ID1, int &ID2){
		ID1 = id1;
		ID2 = id2;
	}
}

void Main(){
	anotherclass ac;
	int aaa = 0;
	int bbb = 0;
	ac.GetID(aaa, bbb);
	Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb);
}

위는 대충 작성한 C++코드이고 밑에는 이걸 동일효과가 나는 C#코드

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class anotherclass{
public int id1;
public int id2;
public anotherclass() { id1 = 5; id2 = 7; }
public void GetID(ref int ID1, ref int ID2){
ID1 = id1;
ID2 = id2;
}
}
namespace _12
{
class Program{
static void Main(string[] args){
anotherclass ac = new anotherclass();
int aaa = 0;
int bbb = 0;
ac.GetID(ref aaa,ref bbb);
Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb);
}
}
}
class anotherclass{ public int id1; public int id2; public anotherclass() { id1 = 5; id2 = 7; } public void GetID(ref int ID1, ref int ID2){ ID1 = id1; ID2 = id2; } } namespace _12 { class Program{ static void Main(string[] args){ anotherclass ac = new anotherclass(); int aaa = 0; int bbb = 0; ac.GetID(ref aaa,ref bbb); Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb); } } }
class anotherclass{
	public int id1;
	public int id2;

	public anotherclass() { id1 = 5; id2 = 7; }
	public void GetID(ref int ID1, ref int ID2){
		ID1 = id1;
		ID2 = id2;
	}
}

namespace _12
{
	class Program{
		static void Main(string[] args){
			anotherclass ac = new anotherclass();
			int aaa = 0;
			int bbb = 0;
			ac.GetID(ref aaa,ref bbb);
			Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb);
		}
	}
}

ref가 들어가면 거기에 물리는 객체(aaa나 bbb)는 이미 인스턴스화 되어 있어야 하고 out을 쓰면 단순 C++표현으로 하면 포인터 값만 넣어주는것이라 그 메소드 내부에서 new 해서 인스턴스화 한담에 그 객체를 반환하는것이다.

ref는 사용하기전에 인스턴스화 까지 되어 있어야 하며 out은 단순 포인터 값만 넣으면 내부에서 인스턴스화 해서 값을 반환한다.

out을 쓰면 위의 예제가 이렇게 변한다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class anotherclass{
public int id1;
public int id2;
public anotherclass() { id1 = 5; id2 = 7; }
public void GetID(out int ID1, out int ID2){
//ID1, ID2가 만일 클래스 타입이었다면 new를 해줘야 한다.
ID1 = id1;
ID2 = id2;
}
}
namespace _12
{
class Program{
static void Main(string[] args){
anotherclass ac = new anotherclass();
int aaa, bbb;
ac.GetID(out aaa,out bbb);
Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb);
}
}
}
class anotherclass{ public int id1; public int id2; public anotherclass() { id1 = 5; id2 = 7; } public void GetID(out int ID1, out int ID2){ //ID1, ID2가 만일 클래스 타입이었다면 new를 해줘야 한다. ID1 = id1; ID2 = id2; } } namespace _12 { class Program{ static void Main(string[] args){ anotherclass ac = new anotherclass(); int aaa, bbb; ac.GetID(out aaa,out bbb); Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb); } } }
class anotherclass{
	public int id1;
	public int id2;

	public anotherclass() { id1 = 5; id2 = 7; }
	public void GetID(out int ID1, out int ID2){
		//ID1, ID2가 만일 클래스 타입이었다면 new를 해줘야 한다.
		ID1 = id1;
		ID2 = id2;
	}
}

namespace _12
{
	class Program{
		static void Main(string[] args){
			anotherclass ac = new anotherclass();
			int aaa, bbb;
			ac.GetID(out aaa,out bbb);
			Console.WriteLine("aaa={0}, bbb={1}",aaa,bbb);
		}
	}
}

허겁지겁 대충 속독법으로 보고 있는 C#책이라.. 혹시 잘못이해한게 있으면 댓글 부탁드립니다.


Comments

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다