'2020/10/07'에 해당되는 글 1건

  1. 2020.10.07 c# out
Programming/c# & winform2020. 10. 7. 17:05

함수 인자로 out을 사용하면 기존의 c 언어에서 포인터로 값을 빼내오듯 사용이 가능하다.

 

OutArgExample() 함수에서도 out 을 이용하여 number 인자는 입력이 아닌 출력으로 사용한다고 명시하고

해당 함수를 호출 할때도 out initializeInMethod 라는 변수가 출력용 변수임을 명시한다.

 

 

int initializeInMethod;
OutArgExample(out initializeInMethod);
Console.WriteLine(initializeInMethod);     // value is now 44

void OutArgExample(out int number)
{
    number = 44;
}

 

[링크 : https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/keywords/out-parameter-modifier]

 

기존의 C로 보면 이걸 문법적으로 표현해주는 느낌이다.

int initializeInMethod;
OutArgExample(&initializeInMethod);
Console.WriteLine(initializeInMethod);     // value is now 44

void OutArgExample(int *number)
{
    *number = 44;
}

[링크 : https://dojang.io/mod/page/view.php?id=550]

 

+

[링크 : https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/keywords/in-parameter-modifier] in

[링크 : https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/keywords/ref] ref

 

+

2020.10.14

unsafe 옵션과 키워드를 사용해서 사용은 가능하다

using System;

class Touttest
{
	static void Main()
	{
		unsafe{
		int initializeInMethod;
		OutArgExample(&initializeInMethod);
		Console.WriteLine(initializeInMethod);     // value is now 44

		void OutArgExample(int *number)
		{
			*number = 44;
		}
		}
	}
}
$ csc /unsafe out2.cs

unsafe 옵션과 키워드를 사용해서 사용은 가능하다

 

 

+

포인터는 기본적으로 막히고..

out2.cs(8,17): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
out2.cs(8,3): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
out2.cs(13,4): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
out2.cs(13,13): error CS0266: Cannot implicitly convert type 'int' to 'int*'. An explicit conversion exists (are you missing a cast?)
out2.cs(13,4): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
out2.cs(11,22): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context

 

/unsafe 옵션을 주지 않고 빌드하면 경고가 아닌 에러가 발생한다 ㄷㄷ

out2.cs(7,3): error CS0227: Unsafe code may only appear if compiling with /unsafe

'Programming > c# & winform' 카테고리의 다른 글

c# strong type langugae  (0) 2020.10.08
c# delagate 대리자  (0) 2020.10.08
c# 타입? 변수명;  (0) 2020.10.05
c# using 키워드, 예외처리  (0) 2020.10.05
c# @문자열  (0) 2020.10.05
Posted by 구차니