Skip to content

VB -> C#: Properties not handled correctly for ref extension methods #1158

@gaschd

Description

@gaschd

When applying extension methods with ref parameters to properties, they should be handled in the same way as ref parameters in regular methods.

VB.Net input code

Public Class ExtensionMethodsRefPropertyParameter
    Public Property Number As Integer = 3
    Public Sub WithExtensionMethod()
        Number.NegEx()
    End Sub
    Public Sub WithMethod()
        Neg(Number)
    End Sub
End Class

Public Module MathEx
    <Extension()>
    Public Sub NegEx(ByRef num As Integer)
        num = -num
    End Sub
    Public Sub Neg(ByRef num As Integer)
        num = -num
    End Sub
End Module

Erroneous output

public partial class ExtensionMethodsRefPropertyParameter
{
    public int Number { get; set; } = 3;
    public void WithExtensionMethod()
    {
        this.Number.NegEx();
    }
    public void WithMethod()
    {
        int argnum = Number;
        MathEx.Neg(ref argnum);
        Number = argnum;
    }
}

public static partial class MathEx
{
    public static void NegEx(this ref int num)
    {
        num = -num;
    }
    public static void Neg(ref int num)
    {
        num = -num;
    }
}
1 target compilation errors:
CS0206: A property or indexer may not be passed as an out or ref parameter

Expected output

public partial class ExtensionMethodsRefPropertyParameter
{
    public int Number { get; set; } = 3;
    public void WithExtensionMethod()
    {
        int argnum = Number;
        argnum.NegEx();
        Number = argnum;
    }
    public void WithMethod()
    {
        int argnum = Number;
        MathEx.Neg(ref argnum);
        Number = argnum;
    }
}
              
public static partial class MathEx
{
    public static void NegEx(this ref int num)
    {
        num = -num;
    }    
    public static void Neg(ref int num)
    {
        num = -num;
    }
}

Details

  • Product in use: Test
  • Version in use: master abea701

Metadata

Metadata

Assignees

No one assigned

    Labels

    VB -> C#Specific to VB -> C# conversion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions