Favicon

Void type

Peponi11/26/20241m

C#
SyntaxTypevoid

1. Introduction

void는 변수 형식으로 사용할 수 없는 형식이다. 메서드, 로컬 메서드, delegate의 반환 형식으로 지정하여 반환 값이 없는 것을 나타낸다. 또한 unsafe 컨텍스트에서 포인터 형식으로 사용 가능하다. (추천하는 방식은 아니다)

2. Example

Method
public void PrintConsole(string message) => Console.WriteLine($"{DateTime.Now} - {message}");
Local method
public void PrintConsole(string message)
{
    string printMessage = $"{DateTime.Now} - {message}";
 
    Print(printMessage);
 
    void Print(string input) => Console.WriteLine(input);
}
delegate
public delegate void PrintDelegate(string message);

3. 참조 자료