This post originated from an RSS feed registered with .NET Buzz
by Eric Gunnerson.
Original Post: Fixed statement and null input...
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
In this class, I've fixed a byte[] array so I can pass it to an unmanaged method. I've included “UnmanagedFunc()“ in my program to illustrate what it looks like, but I would normally be calling a C function through P/Invoke in this scenario.
The problem with this code is that some C APIs accept null values as arguments. It would be nice to be able to pass a null byte[] and have that translate to a null pointer, but the fixed statement throws if it gets a null array.
Thr problem with the workarounds is that they are ugly, and they get fairly complicated if there is more than one parameter involved.
The language spec (section 18.6) says that the behavior of fixed is implementation defined if the array expression is null, so we could change our behavior so that fixing a null array would result in a null pointer rather than an exception.
Questions:
Have you written code where this would be useful?
Are there situations where the change of behavior would cause you problems?
Are the workarounds simple enough that this isn't an issue?