It isn't possible to pass null as null into a .NET method that has a parameter of type String - by Kirk Munro

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 307821 Comments
Status Closed Workarounds
Type Bug Repros 14
Opened 10/31/2007 12:04:37 PM
Access Restriction Public


I'm logging this for Roman Kuzmin because he cannot access the Connect site right now.  This comes from an issue he raised on the PowerShell newsgroup.  It is not possible, at least as far as he or I can tell, to pass null as null into a .NET method that has a string parameter.  For example, take a look at this class definition (provided by Roman):

using System;
namespace ClassLibrary1
 public static class Class1
  public static string Foo(string value)
   if (value == null)
    return "Special processing of null.";
    return "'" + value + "' has been processed.";

If you build this and then try accessing it from PowerShell, how can you call Foo such that the "Special processing of null." section is used?  If you call Foo with $null, an empty string is passed into the method.  If you call it with a variable assigned to null, you get the same result.  Anything else we have tried results in the same thing.

This isn't a problem when you own the source for the .NET class, but if you are using anything that you don't own the source to you can't trigger any special handling that would occur if null was passed in.

Is this a defect?  It certainly seems like one.  If you have a workaround or undocumented way that this can be done, it would be really appreciated.
Sign in to post a comment.
Posted by Dan [MSFT] on 2/21/2012 at 6:39 PM
This has been fixed in Windows PowerShell 3.0. However, to pass null, you need to use [System.Management.Automation.Language.NullString]::Value

PS C:\> $content = @"
>> using System;
>> namespace ClassLibrary1
>> {
>> public static class Class1
>> {
>> public static ReturnValue Foo(string value)
>> {
>>    if (value == null)
>>     return ReturnValue.Null;
>>    else
>>     return ReturnValue.EmptyString;
>> }
>> }
>> public enum ReturnValue
>> {
>>     Null = 0,
>>     EmptyString = 1
>> }
>> }
>> "@
PS C:\> Add-Type -TypeDefinition $content
PS C:\> [ClassLibrary1.Class1]::Foo($null)
PS C:\> [ClassLibrary1.Class1]::Foo([System.Management.Automation.Language.NullString]::Value)
Posted by v-tagurm on 7/20/2010 at 5:57 PM
Posted by Keith Hill MVP on 1/4/2010 at 6:02 PM
Seems like it would be nice to have some syntax for declaring to PowerShell that you don't want any automatic type coercion e.g. $obj.DoSomething($a, $b, [nocast]$null)
Posted by andrey.anastasov on 1/29/2009 at 3:16 AM
The best workaround I can think of is by using .Net reflection. For example, instead of:
    $obj.DoSomething($a,$b,$null) can do:
    $params = @($a,$b,$null)
    $obj.GetType().GetMethod("DoSomething").Invoke($obj, $params)
Posted by Matthew Hobbs on 6/13/2008 at 9:45 AM
Similarly you can't set a string property on a .NET object to null.
Posted by Roman Kuzmin on 5/20/2008 at 3:09 AM
Yet another real case:

System.IO.FileInfo.Replace Method (String, String destinationBackupFileName)
System.IO.FileInfo.Replace Method (String, String destinationBackupFileName, Boolean)

Second parameter:
The name of a file with which to create a backup of the file described by the destFileName parameter.
Pass a null reference (Nothing in Visual Basic) to the destBackupFileName parameter if you do not want to create a backup of the file being replaced.

The last case is impossible from PowerShell because null cannot be passed where string is expected.
Posted by Scott Yost [MSFT] on 11/25/2007 at 9:22 AM
This one came up on the internal DL as well - it prevents you from calling x509certificatecollection.import() with a $null password. You have to use inline C# to do it.