Option Strict Off
Imports System.Runtime.CompilerServices
Module Module4
Sub Main()
Dim aString As String = "Initial value for aString"
aString.PrintMe()
Dim anObject As Object = "Initial value for anObject"
' The following statement causes a run-time error when Option
' Strict is off, and a compiler error when Option Strict is on.
'anObject.PrintMe()
End Sub
<Extension()>
Public Sub PrintMe(ByVal str As String)
Console.WriteLine(str)
End Sub
<Extension()>
Public Sub PrintMe(ByVal obj As Object)
Console.WriteLine(obj)
End Sub
End Module
Imports System.Runtime.CompilerServices
Module StringExtensions
<Extension()>
Public Sub Print(ByVal aString As String)
Console.WriteLine(aString)
End Sub
End Module
확장 메서드의 정의 및 사용 (C#)
Module Class1
Imports System.Runtime.CompilerServices
Module ExtensionMethods
<Extension()>
Sub Print(ByVal s As String)
Console.WriteLine(s)
End Sub
End Module
Class ExampleClass
Public Shared Sub Main(ByVal args As String())
Dim text As String = "Hello from ExtensionMethods"
'// 아래의 두 코드는 동일한 결과가 나옵니다.
"Hello from ExtensionMethods".Print()
text.Print()
End Sub
End Class