How to return value with anonymous method?

后端 未结 6 595
生来不讨喜
生来不讨喜 2020-12-07 13:26

This fails

string temp = () => {return \"test\";};

with the error

Cannot convert lambda expression to type \'str

6条回答
  •  醉话见心
    2020-12-07 13:38

    An anonymous method can return a value using a func delegate. Here is an example where I have shown how to return a value using an anonymous method.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
    
    
            static void Main(string[] args)
            {
                Func del = delegate (int x)
                  {
                      return x * x;
    
                  };
    
                int p= del(4);
                Console.WriteLine(p);
                Console.ReadLine();
            }
        }
    }
    

提交回复
热议问题