New 연산자(Visual Basic)

새 개체 인스턴스를 만들 New 절을 도입하거나, 형식 매개 변수에 대한 생성자 제약 조건을 지정하거나, Sub 프로시저를 클래스 생성자로 식별합니다.

설명

선언 또는 할당 문에서 New 절은 인스턴스를 만들 수 있는 정의된 클래스를 지정해야 합니다. 즉, 클래스는 호출 코드에서 액세스할 수 있는 하나 이상의 생성자를 노출해야 합니다.

선언문이나 대입문에 New 절을 사용할 수 있습니다. 문이 실행되면 지정한 클래스의 적절한 생성자를 호출하여 제공한 인수를 전달합니다. 다음 예제에서는 매개 변수를 사용하지 않는 생성자와 문자열 매개 변수를 사용하는 생성자가 두 개 있는 Customer 클래스의 인스턴스를 만들어 이를 보여 줍니다.

' For customer1, call the constructor that takes no arguments.
Dim customer1 As New Customer()

' For customer2, call the constructor that takes the name of the 
' customer as an argument.
Dim customer2 As New Customer("Blue Yonder Airlines")

' For customer3, declare an instance of Customer in the first line 
' and instantiate it in the second.
Dim customer3 As Customer
customer3 = New Customer()

' With Option Infer set to On, the following declaration declares
' and instantiates a new instance of Customer.
Dim customer4 = New Customer("Coho Winery")

배열은 클래스이므로 New은 다음 예제와 같이 새 배열 인스턴스를 만들 수 있습니다.

Dim intArray1() As Integer
intArray1 = New Integer() {1, 2, 3, 4}

Dim intArray2() As Integer = {5, 6}

' The following example requires that Option Infer be set to On.
Dim intArray3() = New Integer() {6, 7, 8}

CLR(공용 언어 런타임)은 새 인스턴스를 만들기 위한 메모리가 부족한 경우 OutOfMemoryException 오류를 throw합니다.

참고 항목

New 키워드는 또한 형식 매개 변수 목록에서 제공된 형식이 액세스 가능한 매개 변수 없는 생성자를 노출하도록 지정하는 데 사용됩니다. 형식 매개 변수 및 제약 조건에 대한 자세한 내용은 형식 목록을 참조하세요.

클래스에 대한 생성자 프로시저를 만들려면 Sub 프로시저의 이름을 New 키워드로 설정합니다. 자세한 내용은 개체 수명: 개체가 만들어지고 소멸되는 방법을 참조하세요.

New 키워드는 다음 컨텍스트에서 사용할 수 있습니다.

참고 항목