委托、拉姆达、匿名类、扩展方法

2020-02-03  本文已影响0人  杰克_王_
        // 委托的本质是一个类
        public delegate void NoReturnNoParam();
        public delegate void NoReturnWithParam(string name, int age);

        static void Show()
        {
            Console.WriteLine("调用Show方法");
        }

        static void Show(string name, int age)
        {
            Console.WriteLine($"{nameof(name)}: {name}; {nameof(age)}: {age}");
        }
public static void Run()
        {
            // 1.0
            {
                NoReturnNoParam noReturnNoParam = new NoReturnNoParam(LambdaDemo.Show);
                noReturnNoParam.Invoke();
            }
            // 2.0
            {
                string hobby = "pingpong";
                NoReturnWithParam noReturnWithParam = new NoReturnWithParam(delegate (string name, int age)
                {
                    Console.WriteLine($"{nameof(name)}: {name}; {nameof(age)}: {age}; {nameof(hobby)}: {hobby}");
                });
                noReturnWithParam.Invoke("jieke", 18);
            }
            // 3.0
            {
                // => goes to
                string hobby = "pingpong";
                NoReturnWithParam noReturnWithParam = new NoReturnWithParam((string name, int age) =>
                {
                    Console.WriteLine($"{nameof(name)}: {name}; {nameof(age)}: {age}; {nameof(hobby)}: {hobby}");
                });
                noReturnWithParam.Invoke("jieke", 18);
            }
            // lambda的本质为内嵌密封私有类中的公开方法
            {
                string hobby = "pingpong";
                NoReturnWithParam noReturnWithParam = (string name, int age) =>
                {
                    Console.WriteLine($"{nameof(name)}: {name}; {nameof(age)}: {age}; {nameof(hobby)}: {hobby}");
                };
                noReturnWithParam.Invoke("jieke", 18);
            }
            {
                string hobby = "pingpong";
                NoReturnWithParam noReturnWithParam = (name, age) => Console.WriteLine($"{nameof(name)}: {name}; {nameof(age)}: {age}; {nameof(hobby)}: {hobby}");
                noReturnWithParam.Invoke("jieke", 18);
            }
            {
                string hobby = "pingpong";
                Action action = () => Console.WriteLine(hobby);
                action();

                Func<int, int, int> func = (x, y) => x + y;
                Console.WriteLine(func.Invoke(1, 1));
            }
            {
                Func<int, bool> func = i => i > 0;
            }
            {
                Expression<Func<int, bool>> expression = i => i > 0;
                Func<int, bool> func = expression.Compile();
            }

            // 匿名类,里面的属性都是只读的
            {
                // 无法使用一般方法访问属性
                object obj = new
                {
                    Name = "jieke",
                    Age = 10
                };
            }
            {
                // 属性是只读的
                var obj = new
                {
                    Name = "jieke",
                    Age = 10
                };
                Console.WriteLine($"{nameof(obj.Name)}: {obj.Name}; {nameof(obj.Age)}: {obj.Age}");
            }
            {
                // 没有编译检查,容易出错
                dynamic obj = new
                {
                    Name = "jieke",
                    Age = 10
                };
                Console.WriteLine($"{nameof(obj.Name)}: {obj.Name}; {nameof(obj.Age)}: {obj.Age}");
            }
        }
using System;
using System.Globalization;

    // 扩展方法可用于组建式开发,在不修改原有类的情况下,添加功能;
    // 直接或间接继承至被扩展类的子类会自动继承扩展的功能
    // 当原来类型中有与扩展方法声明一致的方法时,扩展方法会被屏蔽
    public static class ExtentionMethodSample
    {
        static IFormatProvider defaultProvider = new CultureInfo("en-GB");
        public static T ConvertTo<T>(this object value, IFormatProvider formatProvider = null)
        {
            try
            {
                if (value != null)
                    return (T)Convert.ChangeType(value, typeof(T), formatProvider ?? defaultProvider);
            }
            catch { }
            return default(T);
        }

        public static void InvokeSample()
        {
            string s = "10000";
            int i = s.ConvertTo<int>();
            Console.WriteLine(i);
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读