Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: lambda implicitly converts to Delegate in VB, doesn't in C#, needs to be explicit #611

Closed
jamesmanning opened this issue Aug 16, 2020 · 1 comment
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@jamesmanning
Copy link

Input code

Public Class Class1
    Public Sub CallThing(thingToCall As [Delegate])
    End Sub

    Public Sub SomeMethod()
    End Sub

    Public Sub Foo()
        CallThing(Sub()
                    SomeMethod()
                  End Sub)
    End Sub
End Class

Erroneous output

    public class Class1
    {
        public void CallThing(Delegate thingToCall)
        {
        }

        public void SomeMethod()
        {
        }

        public void Foo()
        {
            this.CallThing(() => SomeMethod());
        }
    }

Expected output

AFAICT on the C# side extracting to a local that's specifically typed seems sufficient, but there may be a better/cleaner way.

    public class Class1
    {
        public void CallThing(Delegate thingToCall)
        {
        }

        public void SomeMethod()
        {
        }

        public void Foo()
        {
            Action thingToCall = () => SomeMethod();
            this.CallThing(thingToCall);
        }
    }

Details

VS extension 8.1.6.0

@jamesmanning jamesmanning added the VB -> C# Specific to VB -> C# conversion label Aug 16, 2020
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Aug 16, 2020

Thanks for the report. The other option that jumps to mind would be to use a constructor, or a cast:

        this.CallThing(new Action(() => SomeMethod()));
        this.CallThing((Action)(() => SomeMethod()));

The cast is my least favourite. I like the constructor for being inline (which avoids a bunch of other issues when the line is conditional). I think I dealt with some similar cases with discarding parameters for some winforms specific code, so I'll look at that for inspiration too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants