简体   繁体   English

C#内联Lambda表达式

[英]C# inline lambda expression

the title is a mouthful and not even sure it's accurate (couldn't make much sense of this ), so I'll try to explain what I'd like to accomplish in C# by using equivalent javascript . 标题很拗口,甚至不知道它的准确的(不能做的多大意义这个 ),所以我会尽力解释想我在完成C#使用等效javascript Any suggestion as to what I should title this question is very much welcome. 关于我应该给这个问题加上什么标题的任何建议都非常欢迎。
In C# , say I have defined this function: C# ,说我已经定义了此函数:

Func<string, string> getKey = entity => {
    switch(entity) {
        case "a":
            return "foo";
        case "b":
            return "bar";
        default:
            return "baz";
    }
};

string key = getKey(/* "a", "b", or something else */);

Now suppose I don't want to define the getKey function explicitly, but use it anonymously as I would in this equivalent javascript snippet: 现在假设我不想显式定义getKey函数,而是像在此等效的javascript代码段中那样匿名使用它:

string key = (function(entity) {
    switch(entity) {
        case "a":
            return "foo";
        case "b":
            return "bar";
        default:
            return "baz";
    }
}(/* "a", "b", or something else */));

How would I go about writing that in C# ? 我将如何使用C#编写代码? I tried: 我试过了:

string key = (entity => {
    switch(entity) {
        case "a":
            return "foo";
        case "b":
            return "bar";
        default:
            return "baz";
    }
})(/* "a", "b", or something else */);

but I get syntax error CS0149: Method name expected . 但出现语法错误CS0149: Method name expected
Thanks in advance, cheers. 在此先感谢您的欢呼。

IMO, the nearest equivalent is this: IMO,最接近的等效项是:

var key = new Func<string, string>(entity =>
{
    switch (entity)
    {
        case "a":
            return "foo";
        case "b":
            return "bar";
        default:
            return "baz";
    }
})("a");

C# is a compiled language and thus different from javascript in a number of ways. C#是一种编译语言,因此在很多方面与javascript不同。 What you are looking for is a closure https://en.wikipedia.org/wiki/Closure_(computer_programming) . 您正在寻找的是闭包https://en.wikipedia.org/wiki/Closure_(computer_programming) And those are possible in C# (for example Linq). 这些在C#中是可能的(例如Linq)。

But I think that what you are looking for can be solved nicely with extension methods ( https://msdn.microsoft.com/en-us//library/bb383977.aspx ): 但我认为,您所寻找的内容可以通过扩展方法( https://msdn.microsoft.com/zh-cn//library/bb383977.aspx )很好地解决:

using ExtensionMethods;
using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("a".ConvertKey());
        }
    }
}

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static string ConvertKey(this String key)
        {
            switch (key)
            {
                case "a":
                    return "foo";
                case "b":
                    return "bar";
                default:
                    return "baz";
            }
        }
    }
}

EDIT: Same program using Linq: 编辑:使用Linq的相同程序:

using System;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(new[] { "a" }.Select(entity =>
            {
                switch (entity)
                {
                    case "a":
                        return "foo";
                    case "b":
                        return "bar";
                    default:
                        return "baz";
                }
            }).First());
            Console.ReadKey();
        }
    }
}

You can think of "Select" as "map" in functional terms. 您可以将“选择”视为功能上的“地图”。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM