-1

Example:

Dim test2 As Func(Of String, Integer) = Function(xuy As String) xuy.Length * 2
Debug.WriteLine(test2.Method.Name)

Result: _Lambda$__22-0

How to name lambda function?

I'm creating manager for code-inline functions and i need to name them somehow. I know you can add additional parameter to parameter list as name but this is crutch.

In python you can directly name lambda func:

myfunc_l = lambda: None
myfunc_l.__name__ = 'foo'

But in .Net this property is ReadOnly.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yarogleck
  • 7
  • 1
  • Er, make a regular function, then delegate off that `Dim test2 As Func(Of String, Integer) = MyFunction` – Charlieface Jan 24 '21 at 17:37
  • You don't need to *name a Lambda*, a Lambda is not called by name. [Lambda expressions (C# reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions). What other languages do or let do - for whatever reason - is not relevant. – Jimi Jan 24 '21 at 17:52
  • @Charlieface Then the sense of using lambda functions is lost if a regular function needs to be created for each lambda function. – Yarogleck Jan 24 '21 at 17:54
  • @Jimi It is not for calling functions, it is for managing. Getting lambda func parameters, name, storing. – Yarogleck Jan 24 '21 at 17:57
  • ...and are you trying to manage these Lambdas as you would in Python? Have you tried to *manage* your `test2` object? Directly or in a collection. If you want to *name it*, you can maybe use a Dictionary where the key is a *name* you assign to a `Func<>` or `Action<>` or... Not really needed, IMO. – Jimi Jan 24 '21 at 17:59
  • An example using a `Dictionary>()` in: [How to display text with subscripts or superscripts in the Title Bar?](https://stackoverflow.com/a/59441301/7444103) – Jimi Jan 24 '21 at 18:06
  • @Jimi Yes, I can control the lambda function through Dictionary and name it there, or name it through an additional parameter. But I'm just wondering if it is possible to assign a name more canonically. Through reflexion or through a property, as in Python.. – Yarogleck Jan 24 '21 at 18:14
  • 3
    You keep on referring to Python... You don't need to name a Lambda. The Dictionary in that sample code is not used to *name* the Lambda, is used to apply the Lambda through a selector. There's no need for *names*. Concept avoided if you don't try to write code in a language that uses paradigms (or, you could read *habits*) derived from other languages. – Jimi Jan 24 '21 at 18:25
  • 2
    Maybe it would help if you were to say something more about what you want to do with the name (in more detail than "manage it"). – Craig Jan 25 '21 at 14:12

1 Answers1

0

Lambda functions, both in C# and in VB.NET are compiled to methods that are put in a hidden class. What you are seeing is the name of the method. You can't change it, you can't assign it, you can't even be sure the name will be the same between compilations (the names are autogenerated based on the "order" of the lambda functions).

SharpLab example in VB.NET:

This:

Public Class C
    Public Sub M()
        Dim test2 As Func(Of String, Integer) = Function(xuy As String) xuy.Length * 2
    End Sub
End Class

is compiled to something that if decompiled in C# is:

[Serializable]
[CompilerGenerated]
internal sealed class _Closure$__
{
    public static readonly _Closure$__ $I;

    public static Func<string, int> $I1-0;

    static _Closure$__()
    {
        $I = new _Closure$__();
    }

    internal int _Lambda$__1-0(string xuy)
    {
        return checked(xuy.Length * 2);
    }
}

public void M()
{
    if (_Closure$__.$I1-0 != null)
    {
        Func<string, int> $I1- = _Closure$__.$I1-0;
    }
    else
    {
        _Closure$__.$I1-0 = new Func<string, int>(_Closure$__.$I._Lambda$__1-0);
    }
}

And SharpLab example in C#:

This:

public class C {
    public void M() {
        Func<String, int> test2 = xuy => xuy.Length * 2;
    }
}

is compiled to this:

public class C
{
    [Serializable]
    [CompilerGenerated]
    private sealed class <>c
    {
        public static readonly <>c <>9 = new <>c();

        public static Func<string, int> <>9__0_0;

        internal int <M>b__0_0(string xuy)
        {
            return xuy.Length * 2;
        }
    }

    public void M()
    {
        if (<>c.<>9__0_0 == null)
        {
            <>c.<>9__0_0 = new Func<string, int>(<>c.<>9.<M>b__0_0);
        }
    }
}

In both cases the hidden class introduced is quite easy to see.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xanatos
  • 109,618
  • 12
  • 197
  • 280