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 키워드는 제공된 형식이 액세스 가능한 매개 변수 없는 생성자를 노출하도록 지정하기 위해 형식 매개 변수 목록에서 사용됩니다. 형식 매개 변수와 제약 조건에 대한 자세한 내용은 형식 목록(Visual Basic)을 참조하십시오.

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

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

Dim 문(Visual Basic)

Of 절(Visual Basic)

Sub 문(Visual Basic)

참고 항목

참조

형식 목록(Visual Basic)

OutOfMemoryException

개념

Visual Basic의 제네릭 형식(Visual Basic)

개체 수명: 개체가 만들어지고 소멸되는 방법(Visual Basic)

기타 리소스

키워드(Visual Basic)