English 中文(简体)
在不要求作出可变声明的情况下,要求采用一种手提语法
原标题:Calling a method on a lambda expression without requiring a variable declaration

考虑到这一推广方法

    public static void Until(this Action action, Func<bool> booleanFunc)
    {
        while (!booleanFunc())
            action();
    }

这样做是可能的。

        var x = 0;

        Action ac = () => x += 1;
        ac.Until(() => x == 5);

属于这种情况

        var x = 0;

        (() => x += 1).Until(() => x == 5);

这是一条线子?

最佳回答

甲状腺需要一种类型。 Saying => x += 1alone states no about the delegation it is due to represent. 因此,通过将斜体移至构造者来提供 type类。

new Action(() => x += 1).Until(() => x == 5);

一种作品也发挥了作用。

((Action)(() => x += 1)).Until(() => x == 5);
问题回答

Unfortunately such syntax is not possible because the compiler cannot infer the type of the anonymous expression. It could be Action but it also could be some other custom defined delegate. You will need to cast it to Action.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签