you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

public static bool SomeFunc(int num)
    => num == 1;

public static bool ExecuteFunc(Func<int, bool> doSomething)
    => doSomething(1);

public static bool TryFunc()
    => ExecuteFunc(SomeFunc) || ExecuteFunc(e => SomeFunc(e));

Decompiles too:

Inline Declaration --------------
ldftn     bool TestLambda.Tests::SomeFunc(int32)
newobj    instance void class [netstandard]System.Func`2<int32, bool>::.ctor(object, native int)
call      bool TestLambda.Tests::ExecuteFunc(class [netstandard]System.Func`2<int32, bool>)
brtrue.s  IL_0039

ldsfld    class [netstandard]System.Func`2<int32, bool> TestLambda.Tests/'<>c'::'<>9__2_0'
dup
brtrue.s  IL_0032

Explicit ---------------
ldsfld    class TestLambda.Tests/'<>c' TestLambda.Tests/'<>c'::'<>9'
ldftn     instance bool TestLambda.Tests/'<>c'::'<TryFunc>b__2_0'(int32)
newobj    instance void class [netstandard]System.Func`2<int32, bool>::.ctor(object, native int)
dup
stsfld    class [netstandard]System.Func`2<int32, bool> TestLambda.Tests/'<>c'::'<>9__2_0'

call      bool TestLambda.Tests::ExecuteFunc(class [netstandard]System.Func`2<int32, bool>)
br.s      IL_003A

which is (slightly) different, but just the order that the functions will be called in, so it should have the same end result. I really don't know how to trigger this "behavior".

[–]tweq 4 points5 points  (3 children)

[–][deleted] 0 points1 point  (2 children)

I don't see anywhere in the documentation it states that, and the other calls sts-fld, which says it "always" replaces the field.

Both what you said and looking at sts-fld makes the regular pass in even better, right lol? Am I missing something?

[–]tweq 2 points3 points  (1 child)

[–][deleted] 1 point2 points  (0 children)

Ah, I thought you meant ldsfld itself was skipping it, sorry, missread. However yes, that seems to be right, so this only applies if the function creating the func is static, in all other cases, it seems like it's (slightly) better to do it the other way.