out 키워드를 사용하면 참조를 통해 인수를 전달할 수 있습니다
이 키워드는 정식 매개 변수를 위해 해당 인수의 별칭을 만드는데, 이는 반드시 변수여야 합니다.
out 매개 변수를 사용하려면 메서드 정의와 호출 메서드가 모두 명시적으로 out 키워드를 사용해야 합니다.
1.
using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
namespace Helloworld1
{
class App
{
//생성자
public App()
{
int num = 10;
OutArgs(num);
Console.WriteLine(num);
}
void OutArgs(int number)
{
number = 44;
}
}
}
2.
using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
namespace Helloworld1
{
class App
{
//생성자
public App()
{
int num = 10;
OutArgs(out num);
Console.WriteLine(num);
}
void OutArgs(out int number)
{
number = 44;
}
}
}
3.
using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
namespace Helloworld1
{
class App
{
//생성자
public App()
{
int num = 10;
OutArgs(ref num);
Console.WriteLine(num);
}
void OutArgs(ref int number)
{
number += 44;
}
}
}
다음 종류의 식 중 하나로 매개 변수의 기본값을 할당해야 합니다.
리터럴 문자열이나 숫자와 같은 상수
new ValType() 형태의 식. 여기서 ValType은 값 형식입니다. 이 경우 형식의 실제 멤버가 아닌 값 형식의 매개 변수가 없는 암시적 생성자가 호출됩니다.
default(SomeType) 형식의 식입니다. 여기서 SomeType은 값 형식 또는 참조 형식일 수 있습니다. 참조 형식인 경우 사실상 null을 지정하는 것과 같습니다.
'c# > 수업 내용' 카테고리의 다른 글
자료구조(배열) (0) | 2021.09.14 |
---|---|
자료구조 (0) | 2021.09.14 |
params 키워드 (0) | 2021.09.13 |
is, as, typeof 연산자 (0) | 2021.09.13 |
Task 클래스 작업 인스턴스화/Task<Result> 클래스 (0) | 2021.09.13 |