Hmm ... it works in C #:
using System; class Test { delegate void Foo(params string[] args); static void Main() { Foo f = x => Console.WriteLine(x.Length); f("a", "b", "c"); } }
However, you are correct: this equivalent delegate declaration in VB is not executed:
Delegate Sub Foo(ParamArray ByVal args() As String)
gives:
error BC33009: "delegate" parameters cannot be declared as "ParamArray".
Curious. Fortunately, there is a way around this:
Imports System Public Class Test Delegate Sub Foo(<[ParamArray]()> ByVal args() As String) Public Shared Sub Main() Dim f As Foo = AddressOf PrintLength f("a", "b", "c") End Sub Private Shared Sub PrintLength(ByVal x() As String) Console.WriteLine(x.Length) End Sub End Class
Basically I just applied ParamArrayAttribute manually. This seems to be normal.
However, none of this would stop you from using existing ParamArray methods. These methods are quite capable of accepting normal arrays, so you could declare your delegate types as regular and still instantiated delegates that referenced these methods without any problems. The type of delegate only affects how you could call a delegate.
Other than declaring a delegate type with an array of parameters, I really don't understand what the problem is.
Jon skeet
source share