Argument Unpacking in .NET

This week, our article will include two topics: we shine the spotlight on the newly implemented feature of argument unpacking and on this occasion, we would also like to introduce the new compatibility matrix in our Wiki.

Peachpie grants the PHP language access into the .NET platform while trying to achieve a reasonable level of compatibility so that existing frameworks and PHP applications would run with none or very few modifications. However, some of the features available in PHP could even be interesting for C# developers, especially in case of dynamic programming. Let’s take a look at the newly implemented feature of argument unpacking (see here for more details), which allows programmers to easily pass arguments from an array to a function call.

Argument unpacking

While we are perfectly aware that a vast majority of usecases for Peachpie involves extending PHP apps with otherwise unavailable functionalities or features only .NET offers, we always stress that the benefits can often be mutual. In .NET, for example, a method call needs to know all of its arguments in advance, as well as their count and types. A programmer could make use of PHP’s argument unpacking, which makes it easy to compose a list of arguments and call a function within a single expression. To illustrate this idea, let’s take a look at the short code sample below:

The short and simple code in line 6 has a more complicated and also slower equivalent when written in C#:

        .Invoke(null, new object[] { 10, 20 })

Additionally, in PHP, it is possible to unpack arguments from iterators (enumerators in C#), combine normal arguments and unpacking within a single call and even pass arguments by reference. It goes without saying that these cases would be even more complicated in C#. Therefore, if you need to get this done in your C# project, you could consider saving some time and performance overhead by simply using PHP’s argument unpacking. You just have to keep and open mind.

The C# language does not provide anything that would help optimize either the complexity or performance of the code. PHP, on the other hand, does. Peachpie compiler understands the argument unpacking and generates an effective form of a method call without the need of using reflection or converting values inefficently to System.Object.

Generated code

The compiler does all the dirty work for you. The sample above produces byte-code, which we can decompile into C# form using a tool like ILSpy:

var expr_19 = new List<PhpValue>();
Operators.Unpack(expr_19, new PhpArray(2){ 10, 20 }, 0uL);
PhpValue[] array = expr_19.ToArray();
Context.Echo(sum((0 >= array.Length) ? PhpValue.Null : array[0], (1 >= array.Length) ? PhpValue.Null : array[1]));

Notice that the method sum is called directly without the use of reflection. Arguments are unpacked into List first and then passed to the method by checking the list boundaries.


First and foremost, it is great that Peachpie supports another PHP construct. Any newly implemented feature moves the project forward and enhances the compatibility with standard PHP code, so more existing apps can run on .NET.

Secondly, argument unpacking is a new construct that is now available to the .NET ecosystem thanks to Peachpie. Any programmer can make use of this syntactic sugar to make their code shorter and easier to read.

Compatibility matrix

New to our Wiki on GitHub is the compatibility matrix, which gives you a quick overview of which high-level features are supported, in progress or not yet supported. The table also includes a link to the future documentation pages for each particular feature, as well as a link to any issues related to it.

Compatibility Matrix

As you can see from the matrix as of today, Peachpie is already very compatible with the PHP language. Only a handful of features are currently not yet supported or in progress.

Please note that we currently do not have issues enabled on GitHub. However, we will enable them together with the release of the stable version of Peachpie 1.0.

Share this article:Share on FacebookShare on VKPin on PinterestShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit
Posted on July 5, 2017, in category Announcement, Information, News, tags: , , , , , ,