ParamArrays and CLS-compliance.

Tags: .NET, C#

In my previous post, I noted that when I write this code in C#:

public void MyMethod (params object[] args) { ... }

What I actually get in my IL is this:

public void MyMethod ([System.ParamArray]object[] args) { ... }

I have this suspicion due to:
1) The fact that the C# compiler tells me to use params instead of the attribute
2) The fact that a function with the same signature except for the params keyword results in a duplicate signature
3) Because I opened up Reflector and saw it happening in the IL.

The params keyword in C# (and the ParamArray keyword in VB.NET) is a shorthand for applying the ParamArray attribute to the method. I assume the sole reason for the existence of the attribute is to notify the relevant compiler that when parsing a call to that function it should wrap the arguments in an array - nothing happens at runtime at all. It's a language feature of C# and VB that is backed up by an attribute in the BCL, but does not have any representation in the CLR.

In the comments to that post, Thomas Tomiczek disagreed:

"Given that the ParamArrayAttribute is defined in System and HAS to be supported by the langauge compilers, basically, it is not a choice of the language. As such, it is not a language feature. It is a requirement.
WIth the same logic you could say that support for a lot of other things is optional - it is not. "

I am not sure I agree with this logic. Any .NET language has to support decorating the parameter with the ParamArray attribute of course, but I'm not sure that it's required to be supported by all .NET languages.

I had a brief glance at the CLS specification and found this paragraph:

Argument lists

All

The only calling convention supported by the CLS is the standard managed calling convention; variable length argument lists are not allowed. (Use the ParamArray keyword in Microsoft Visual Basic and the params keyword in C# for variable number of arguments support.)

What this seems to mean is that the concept of variable length argument lists do not exist in the CLR, and the only way to pass a variable number of arguments is through arrays. VB.NET and C#, to name particular examples, provide us with language features (in the form of the params and ParamArray keywords) that save us the effort of creating arrays and do the wrapping-up for us.

The way I see it, the fact that it uses an attribute that's defined in the System namespace, rather than language-specific namespaces (assuming one such would exist for C#) is just a result of the C# and VB teams working closely with the BCL designers.

1 Comment

  • Daniel said

    I've created a UDF for Excel using automation. I have a problem with the params as I do net get my functions call passed back to the action function. It would seem that it might be the duplicate signatures problem as you have described above. Any Idea how to solve this?

Comments have been disabled for this content.