SCENARIO
I wrote these extension methods to initialize and / or delete all elements of an array in an elegant / simplified way to avoid code repeating:
<HideModuleName>
Public Module ArrayExtensions
<Extension>
Public Sub InitializeAll(Of T As New)(ByVal sender As T())
For index As Integer = 0 To (sender.Length - 1)
sender(index) = New T
Next index
End Sub
<Extension>
Public Sub InitializeAll(Of T As IDisposable)(ByVal sender As T())
ArrayExtensions.DisposeAll(sender)
For index As Integer = 0 To (sender.Length - 1)
sender(index) = Activator.CreateInstance(Of T)()
Next index
End Sub
<Extension>
Public Sub DisposeAll(Of T As IDisposable)(ByVal sender As T())
For index As Integer = 0 To (sender.Length - 1)
If (sender(index) IsNot Nothing) Then
sender(index).Dispose()
sender(index) = Nothing
End If
Next index
End Sub
End Module
The goal is to use it in common scenarios like this:
Dim myCollection As MyDisposableType() = New MyDisposableType(100) {}
myCollection.InitializeAll()
myCollection.DisposeAll()
PROBLEM
I found that the compiler shows this error:
'Public Sub InitializeAll (Of T As New) (sender () As T)' has several definitions with identical signatures.
... I really do not understand this error, because IDisposable- this is the interface, I tried to delete the parameter as new , but still showing the same error.
Question
InitializeAll(), ?, , , , .
, , :
<Extension>
Public Sub InitializeAll(Of T As New)(ByVal sender As T())
For index As Integer = 0 To (sender.Length - 1)
If (sender(index) IsNot Nothing) Then
If sender(index).GetType.GetInterfaces.Contains(GetType(IDisposable)) Then
DirectCast(sender(index), IDisposable).Dispose()
sender(index) = Nothing
End If
End If
sender(index) = New T
Next index
End Sub
, , , .