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來解決動態變量問題.以減少混用引起的混亂.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM