ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# Func入门一

2022-09-08 17:34:52  阅读:195  来源: 互联网

标签:Console 入门 C# void int static Func var


这篇的主要目的是用一些例子展示如何使用Func。

Func其实是一个内置的委托,它带来了一些函数式编程特性,并有助于减少代码冗长。

Func只能包含0 ~ 16个输入参数,且必须有一个返回类型。(Func委托有16个重载。)

例子一

 //1.FuncExp1:不带参数的方法
        static string GetMessage()
        {
            return "Hello there!";
        }
        static void FuncExp1()
        {
            Func<string> sayHello = GetMessage;
            Console.WriteLine(sayHello());
        }

测试结果如下:

 

 例子二

  //2.FunExp2:带两个参数的方法
        static int Sum(int x, int y)
        {
            return x + y;
        }
        static void FuncExp2()
        {
            Func<int, int, int> Add = Sum;
            Console.WriteLine(Add(20, 30));
        }

测试结果如下:

 

 例子三

  //3.FunExp3:带三个参数的方法
        static int Sum2(int x, int y, int z)
        {
            return x + y + z;
        }
        static void FuncExp3()
        {
            Func<int, int, int, int> Add = Sum2;
            int result = Add(20, 30, 40);
            Console.WriteLine(result);
        }

测试结果如下:

 

 例子四 

Lambda表达式写法

 static void Lambda1()
        {
            Func<int, int, int, int> Add = (x, y, z) => x + y + z;
            int result = Add(20, 30, 40);
            Console.WriteLine(result);
        }

 

 例子五

 //6.c# Func Linq Where
        static void LabmdaWithWhere()
        {
            Func<string, bool> HasThree = str => str.Length == 3;
            string[] words =
        {
            "sky", "forest", "wood", "cloud", "falcon", "owl" , "ocean",
            "water", "bow", "tiny", "arc"
        };
            IEnumerable<string> threeLetterWords = words.Where(HasThree);
            //IEnumerable<string> three = words.Where(str => str.Length == 3);这种写法,看起来是不是很熟悉
            foreach (var word in threeLetterWords)
            {
                Console.WriteLine(word);
            }

        }

测试结果如下:

 

 例子六

 //7.c# list of Func delegates
        static void ListofFunDelegates1()
        {
            var vals = new int[] { 1, 2, 3, 4, 5 };
            Func<int, int> square = x => x * x;
            var res = vals.Select(square);
            //var result = vals.Select(q => q * q);这种写法,看起来是不是很熟悉
            Console.WriteLine(string.Join(",", res));
        }

测试结果如下:

 

 例子七

 static void ListofFunDelegates2()
        {
            var vals = new int[] { 1, 2, 3, 4, 5 };
            Func<int, int> square = x => x * x;
            Func<int, int> cube = x => x * x * x;
            Func<int, int> inc = x => x + 1;
            var fns = new List<Func<int, int>> { inc, square, cube };
            foreach (var fn in fns)
            {
                var res = vals.Select(fn);
                //var result = vals.Select(q => q * q);这种写法,看起来是不是很熟悉
                Console.WriteLine(string.Join(",", res));
            }
        }

 测试结果如下:

 

 参考网址如下:

https://zetcode.com/csharp/func/

本地代码在CSharpBasic\FuncTutorial

标签:Console,入门,C#,void,int,static,Func,var
来源: https://www.cnblogs.com/keeplearningandsharing/p/16670163.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有