C# Func的用法

C#中Fun和前面介绍过的Action有点类似,都是一个委托方法

不同的是Func是有返回值的,而Action没有

Fun常用有两个参数,前面的是输入参数,后面的是输出参数(意味着是在另一部分运算中产生的)恰恰是整个方法的返回值

(T arg)代表的是和输出参数类型相同的方法名称(返回值的类型和Func输出参数类型相同)

Fnc最多有16个输入参数,有且只有一个输出参数

Func<TResult> function代表function函数的返回值得类型是TResult。


//
    // 摘要:
    //     Encapsulates a method that has one parameter and returns a value of the type
    //     specified by the TResult parameter.To browse the .NET Framework source code for
    //     this type, see the Reference Source.
    //
    // 参数:
    //   arg:
    //     The parameter of the method that this delegate encapsulates.
    //
    // 类型参数:
    //   T:
    //     The type of the parameter of the method that this delegate encapsulates.This
    //     type parameter is contravariant. That is, you can use either the type you specified
    //     or any type that is less derived. For more information about covariance and contravariance,
    //     see Covariance and Contravariance in Generics.
    //
    //   TResult:
    //     The type of the return value of the method that this delegate encapsulates.This
    //     type parameter is covariant. That is, you can use either the type you specified
    //     or any type that is more derived. For more information about covariance and contravariance,
    //     see Covariance and Contravariance in Generics.
    //
    // 返回结果:
    //     The return value of the method that this delegate encapsulates.
    [TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")]
    public delegate TResult Func<in T, out TResult>(T arg);


         public void Test()
        {
            //1.
            Func<string, int> func1 = MyMethodA;
            Console.WriteLine(func1("feige"));

            //2.
            MyMethodB<string>(new Func<string, int>(MyMethodA), "feige");

            //3
            MyMethodB(new Func<string, int>(MyMethodA), "feige");
        }

        public int MyMethodA(string str)
        {
            if (string.IsNullOrEmpty(str))
                return 0;
            return 1;
        }

        public void MyMethodB<T>(Func<T, int> func, T t)
        {
            Console.WriteLine(func(t));
        }




03-21
### C# 中 `Func` 的定义与用途 `Func<T>` 是一种通用委托类型,用于表示具有指定参数并返回特定类型的函数[^1]。它允许开发者通过简洁的方式传递方法作为参数或将方法存储在变量中。 #### 特性描述 - **泛型支持**:`Func` 支持多种输入参数以及单一的返回值。 - **灵活性高**:可以用来简化 Lambda 表达式的使用场景。 - **内置实现**:无需显式声明自定义委托即可完成常见操作。 以下是不同版本的 `Func` 定义及其适用范围: | 泛型数量 | 参数说明 | |----------|------------------------------| | `Func<TResult>` | 不带任何参数,仅返回 TResult 类型的结果 | | `Func<T, TResult>` | 接受 T 类型参数,返回 TResult 结果 | | `Func<T1,T2,TResult>`| 接受两个参数 (T1 和 T2),返回 TResult | #### 使用示例 下面是一些常见的 `Func` 实现方式的例子: ```csharp // Example of using Func with no parameters but returning a value. Func<int> getRandomNumber = () => new Random().Next(10); Console.WriteLine(getRandomNumber()); // Example where the function accepts one parameter and returns an int result. Func<int, int> multiplyByTwo = number => number * 2; Console.WriteLine(multiplyByTwo(5)); // A more complex example involving multiple input arguments. Func<int, string, bool> checkIfAgeMatchesNameLength = (age, name) => age == name.Length; bool isMatched = checkIfAgeMatchesNameLength(7, "Edward"); Console.WriteLine(isMatched); // Outputs true or false based on condition match ``` 上述代码展示了如何利用 `Func` 来创建匿名方法或者 lambda 函数来处理数据转换、验证逻辑等问题。 #### 总结 `Func` 提供了一种强大而灵活的方式来封装可执行代码片段,并且能够轻松集成到 LINQ 查询以及其他高级特性当中去。这使得程序设计更加模块化和易于维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值