• Func与Action, delegate, event, var, dynamic, 匿名方法,lambda, 大量的关键都使用相同功能,大大增加C#复杂性 人工智能


    本来C#是美的,一开始引入delegate也解决部分问题,但随着版本上升,想更动态,但又不彻底,不断增加的关键字加大了C#的复杂性及.net framework类库的混乱. Func和Action的确是好东西,部分解决了C#历史问题,但不彻底,所以反而增加了更多的复杂性,尤其是.net framework类库,很多调用大量的类似参数. 直接上代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FuncTest
    {
        public delegate int func1delegate(int i);
        public delegate int func2delegate(int i, int j);
    
        class Program
        {
            static void Main(string[] args)
            {
                func1delegate func1 = TestClass.StaticSquare;
                func2delegate func2 = new TestClass().Compute;
                Func<int, int> f1 = TestClass.StaticSquare;
                Func<int, int, int> f2 = new TestClass().Compute;
                TestClass.Run1(func1, func2);
                TestClass.Run2(f1, f2);
                Console.ReadKey();
            }
    
            //期待的模式,引入function函数变量, 返回动态类型
            //static void DreamMain(string[] args)
            //{
            // function f (x, y)=> x * y; 
            // Console.WriteLine(DreamClass.Compute(f));
            //}
        }
    
        //public class DreamClass
        //{
        // //期待的模式,接收function函数变量, 返回动态类型
        // public var static Compute(function f)
        // {
        // int i = 5;
        // int j = 6;
        // return f(i * j);
        // }
        //}
    
        public class TestClass
        {
            public static int StaticSquare(int i)
            {
                return i * i;
            }
    
            public int Compute(int i, int j)
            {
                return i * j;
            }
    
            public void Test1()
            {
                Func<int, int> functionStatic = TestClass.StaticSquare;
                Func<int, int, int> function = new TestClass().Compute;
            }
    
            public Func<int, int> Test2()
            {
                return delegate(int i)
                {
                    return i;
                };
            }
    
            public dynamic Test3(Func<int, int, int> func, int i, int j)
            {
                return func(i, j);
            }
    
            public Func<int, int> Test4(int j)
            {
                return delegate(int i)
                {
                    return i * j;
                };
            }
    
            public func1delegate Test5(int j)
            {
                return i => i * j;
            }
    
            public int Test5(func1delegate func1, int j)
            {
                return func1(j);
            }
    
            public static void Run1(func1delegate func1, func2delegate func2)
            {
                Console.WriteLine(func1(5));
                Console.WriteLine(func2(5, 6));
                TestClass tc = new TestClass();
                int i = tc.Test5(func1, 6);
                Console.WriteLine(i);
                var v = tc.Test5(6)(5);
                Console.WriteLine(v);
            }
    
            public static void Run2(Func<int, int> func, Func<int, int, int> func1)
            {
                Console.WriteLine(func(5));
                Console.WriteLine(func1(5, 6));
    
                Func<int, int> func2 = new TestClass().Test2();
                Console.WriteLine(func2(5));
    
                dynamic d = new TestClass().Test3(new TestClass().Compute, 5, 6);
                Console.WriteLine(d);
            }
        }
    }

    ---- 结论,C#搞这么多花样,无非就是想解决两个问题,动态类型变量,函数编程,这并非什么犀利的新技术, javascript, Python等语言,早几十年就有. 如果直接加var关键字做动态变量,加function关键字作函数变量,则天下太平,动态就该动态. 当然抱怨解决不了问题. 在现有条件下,建议尽量用Func和lambda解决函数变量问题,用var, dynamic来解决动态变量问题.以减少混用引起的混乱.

  • 相关阅读:
    SQLSERVER 远程登录18456错误
    谁用掉了我的数据库空间?
    Zabbix-微信报警
    Mailx安装与使用
    Redis-集群操作
    Redis-集群部署
    十、Zabbix-自动关联模板
    九、Zabbix-触发器
    八、Zabbix-应用集、监控项
    七、Zabbix-模板,应用集,监控项,触发器
  • 原文地址:https://www.cnblogs.com/DSharp/p/3000559.html
Copyright © 2020-2023  润新知