Skip to content

VB -> C#: null coalescing operator not playing well with boolean expressions #712

@chtenb

Description

@chtenb

Input code

If Not String.IsNullOrWhiteSpace(Var1) AndAlso Not (Var1?.StartsWith("{"c) AndAlso Var1?.EndsWith("}"c)) Then
    ' ...
End If
If Not String.IsNullOrWhiteSpace(Var2) AndAlso Not Var2?.StartsWith(">") Then
    ' ...
End If

Erroneous output

error CS0019: Operator '&&' cannot be applied to operands of type 'bool?' and 'bool?'
error CS0019: Operator '&&' cannot be applied to operands of type 'bool' and 'bool?
if ((!string.IsNullOrWhiteSpace(Var1) && !((Var1?.StartsWith(Conversions.ToString('{'))) && (Var1?.EndsWith(Conversions.ToString('}'))))) == true)
{
    //...
}
if ((!string.IsNullOrWhiteSpace(Var2) && !(Var2?.StartsWith(">"))) == true)
{
    //...
}

Expected output

if (!string.IsNullOrWhiteSpace(Var1) && !(Var1?.StartsWith("{") ?? false) && (Var1?.EndsWith("}") ?? false))
{
    //...
}
if (!string.IsNullOrWhiteSpace(Var2) && !(Var2?.StartsWith(">") ?? false))
{
    //...
}

Details

  • Product in use: VS extension
    image

Metadata

Metadata

Assignees

No one assigned

    Labels

    VB -> C#Specific to VB -> C# conversioncompilation errorA bug where the converted output won't compile

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions