【问题标题】:How can I create anonymous method in a lambda expression using C# like I can in VB.NET?如何像在 VB.NET 中一样使用 C# 在 lambda 表达式中创建匿名方法?
【发布时间】:2013-01-25 11:52:47
【问题描述】:

在 VB.NET 中,我可以这样做:

MyArray.Select(Function(a)
                   Dim x as string
                   x = a
                   Return x
               End Function)

如何在 c# 中做到这一点?

【问题讨论】:

    标签: c# .net lambda anonymous-methods


    【解决方案1】:
    myArray.Select(a => {
        ...
    });
    

    【讨论】:

      【解决方案2】:

      看起来 VB.Net 只是在 c# 中选择 MyArray 中的每个元素

      MyArray.Select(a => (string) a);
      

      要使其成为方法,请使用大括号:

      MyArray.Select(a =>
       {
         ...
         return ...;
       });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-16
        • 1970-01-01
        • 2011-01-20
        • 1970-01-01
        • 2011-06-23
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        相关资源
        最近更新 更多