logo

C#의 예제가 포함된 Out 매개변수

그만큼 밖으로 참조 유형으로 메서드에 인수를 전달하는 데 사용되는 C#의 키워드입니다. 일반적으로 메서드가 여러 값을 반환할 때 사용됩니다.

중요한 사항:



  • ref 키워드와 유사합니다. 그러나 주요 ref와 out의 차이 키워드는 ref가 변수를 메소드에 전달하기 전에 초기화해야 한다는 것입니다. 그러나 out 매개변수는 메소드에 전달되기 전에 변수를 초기화할 필요가 없습니다. 그러나 호출 메서드에 값을 반환하기 전에 호출된 메서드에서 변수를 초기화해야 합니다.
  • in 키워드와도 비슷하지만 ~에 키워드는 인수 값을 변경하기 위해 호출한 메서드를 허용하지 않지만 심판 허용합니다.
  • 사용을 위해 밖으로 메서드 정의와 호출 메서드 모두 매개변수로 키워드를 사용해야 합니다. 밖으로 명시적으로 키워드.
  • out 매개변수는 비동기식 메서드에서 사용할 수 없습니다.
  • out 매개변수는 반복자 메소드에서 사용할 수 없습니다.
  • 한 메소드에 out 매개변수가 두 개 이상 있을 수 있습니다.
  • 메소드 호출 시 out 매개변수를 인라인으로 선언할 수 있습니다. 그러나 인라인 출력 매개변수는 호출되는 동일한 코드 블록에서 액세스할 수 있습니다.
  • 메소드 오버로딩은 out 매개변수를 사용하여 수행할 수도 있습니다.
  • 속성은 변수가 아니기 때문에 출력 매개변수로 전달할 수 없습니다.
  • C# 6.0까지는 사용자가 먼저 변수를 선언한 다음 out 인수로만 전달할 수 있습니다. 그러나 C# 7.0부터는 별도의 변수 선언을 제외하고 사용자가 메서드 호출의 인수 목록에서 out 변수를 선언할 수도 있습니다.

out 매개변수 선언:

// No need to initialize // the variable here data_type variable_name; Method_Name(out variable_name); // you can also convert both above two // lines of codes as follows from // C# 7.0 onwards Method_Name(out data_type variable_name);>

여기서의 값은 변수_이름 값을 반환하기 전에 호출된 메서드에서 초기화되어야 합니다.

예:

씨#




// C# program to illustrate the> // concept of out parameter> using> System;> class> GFG {> >// Main method> >static> public> void> Main()> >{> >// Declaring variable> >// without assigning value> >int> i;> >// Pass variable i to the method> >// using out keyword> >Addition(>out> i);> >// Display the value i> >Console.WriteLine(>'The addition of the value is: {0}'>, i);> >}> >// Method in which out parameter is passed> >// and this method returns the value of> >// the passed parameter> >public> static> void> Addition(>out> int> i)> >{> >i = 30;> >i += i;> >}> }>

>

>

산출:

The addition of the value is: 60>

다중 출력 매개변수: C#에서는 사용자가 여러 출력 매개 변수를 메서드에 전달할 수 있으며 메서드는 여러 값을 반환합니다.

예: 아래 코드에서는 초기화하지 않고 두 개의 값 변수, 즉 int i, j;를 선언했습니다. 이제 Addition(out i, out j);와 같은 out 키워드를 사용하여 Addition 메서드에 이러한 매개 변수를 전달합니다. 이러한 변수의 값은 전달된 메서드에 할당됩니다.

씨#


.다음 자바



// C# program to illustrate the> // concept of multiple out parameter> using> System;> class> GFG {> >// Main method> >static> public> void> Main()> >{> >// Declaring variables> >// without assigning values> >int> i, j;> >// Pass multiple variable to> >// the method using out keyword> >Addition(>out> i,>out> j);> >// Display the value i and j> >Console.WriteLine(>'The addition of the value is: {0}'>, i);> >Console.WriteLine(>'The addition of the value is: {0}'>, j);> >}> >// Method in which out parameters> >// are passed and this method returns> >// the values of the passed parameters> >public> static> void> Addition(>out> int> p,>out> int> q)> >{> >p = 30;> >q = 40;> >p += p;> >q += q;> >}> }>

>

>

산출:

The addition of the value is: 60 The addition of the value is: 80>

C# 7.0의 Out 매개변수 개선: C# 7.0에는 out 매개 변수에 몇 가지 새로운 기능이 추가되었으며 그 기능은 다음과 같습니다.

  • C# 7.0에서 out 매개 변수는 선언 및 초기화 없이 전달될 수 있습니다. Out 매개변수의 인라인 선언 또는 암시적 유형 출력 매개변수 . 그 범위는 메소드 본문, 즉 로컬 범위로 제한됩니다.
  • out 매개변수를 사용할 수 있습니다. ~였다 메소드 매개변수 목록에 입력하세요.
  • out 매개변수에서는 정의와 호출 모두에서 out 매개변수의 이름이 동일할 필요는 없습니다.
  • Try Pattern에서도 사용할 수 있습니다.

예: 아래 프로그램은 Out 매개변수의 인라인 선언을 보여줍니다. 여기에 코드 줄, 즉 Area(out int length, out int width, out int Rarea); 이러한 변수는 메서드 호출 내에서 직접 선언되므로 Out 매개 변수의 인라인 선언을 포함합니다. 변수의 값은 변수가 전달된 메서드에서 초기화됩니다.

자바 비교 문자열

메모: 이 예제를 실행하려면 C# 7.0 버전이 필요합니다.

예:

씨#




// C# program to illustrate the> // concept of out parameter> using> System;> class> GFG> {> >// Main method> >static> public> void> Main()> >{> >// In-line declaring variables> >// without assigning values> >// Passing multiple variable to> >// the method using out keyword> >Area(>out> int> length,>out> int> width,>out> int> Rarea);> >// Display the value length, width, and Rarea> >System.Console.WriteLine(>'Length of the rectangle is: '>+ length);> >System.Console.WriteLine(>'Width of the rectangle is: '>+ width);> >System.Console.WriteLine(>'Area of the rectangle is: '>+ Rarea);> >Console.ReadLine();> >}> >// Method in which out parameters are passed> >// and this method returns the values of> >// the passed parameters> >public> static> void> Area(>out> int> p,>out> int> q,>out> int> Rarea)> >{> >p = 30;> >q = 40;> >Rarea = p * q;> >}> }>

>

>

산출:

Length of the rectangle is : 30 Width of the rectangle is : 40 Area of the rectangle is : 1200>