C# Creating and using Functions

后端 未结 11 711
暗喜
暗喜 2020-12-28 18:08

I need help with C# programming; I am new to it and I come from C background. I have a Console Application like this:

using System;
using System.Collections         


        
11条回答
  •  佛祖请我去吃肉
    2020-12-28 18:36

    Note: in C# the term "function" is often replaced by the term "method". For the sake of this question there is no difference, so I'll just use the term "function".

    Thats not true. you may read about (func type+ Lambda expressions),( anonymous function"using delegates type"),(action type +Lambda expressions ),(Predicate type+Lambda expressions). etc...etc... this will work.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
               int a;
               int b;
               int c;
    
               Console.WriteLine("Enter value of 'a':");
               a = Convert.ToInt32(Console.ReadLine());
    
               Console.WriteLine("Enter value of 'b':");
               b = Convert.ToInt32(Console.ReadLine());
    
               Func funcAdd = (x, y) => x + y;
               c=funcAdd.Invoke(a, b);
               Console.WriteLine(Convert.ToString(c));
    
            }
    
         }
    }
    

提交回复
热议问题