C艹——委托,lambda,linq(linq大失败)

一.泛型委托

例1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp44
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDele<int> deleAdd = new MyDele<int>(Add);
            int res = deleAdd(100, 200);
            Console.WriteLine(res);
            MyDele<double> deleMul = new MyDele<double>(Mul);
            double mulRes = deleMul(100.1, 100);
            Console.WriteLine(mulRes);
            Console.WriteLine(deleAdd.GetType().IsClass);//结果是true证明委托是一种类类型
        }

        static int Add(int x,int y)
        {
            return x + y;
        }

        static double Mul(double x,double y)
            {
            return x * y;
        }
        delegate T MyDele<T>(T a, T b);//泛型委托
        ///小结
        ///其实没必要自己声明泛型委托
        ///类库里已经有很多已经替我们申请好的委托了
    }
}

例2.action委托的应用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp44
{
    class Program
    {
        static void Main(string[] args)
        {
            Action action = new Action(M1);//Action封装一个方法,该方法不具有参数也没有返回值
            Action<string, string> action1 = new Action<string, string>(SayHello);//封装带参数却没有返回值的方法
           var action2 = new Action<string, int>(Hello);// Action<string, int>这个可以用var代替
            action.Invoke(); 
            action1.Invoke("Tim","Parker");
            action2.Invoke("Pop", 5);
        }

        static void M1()
        {
            Console.WriteLine("M1 is called");
        }

        static void SayHello(string name1,string name2)
        {
            Console.WriteLine($"hello,{name1} and {name2}");
        }
       
        static void Hello(string name,int round)
        {
            for (int i = 0; i < round; i++)
            {
                Console.WriteLine($"Hello,{name}");
            }
        }

        static int Add(int x,int y)
        {
            return x + y;
        }

        static double Mul(double x,double y)
            {
            return x * y;
        }
       
    }
}

例3 func委托——有返回值的就用func

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp44
{
    class Program
    {
        static void Main(string[] args)
        {
            var func = new Func<int, int, int>(Add);//前两个参数值类型为int,返回值类型也为int
            var func1 = new Func<double, double, double>(Mul);
            int res = func.Invoke(100, 100);
            Console.WriteLine(res);
            double resMul = func1.Invoke(3.0, 4.0);
            Console.WriteLine(resMul);
        }

        static void M1()
        {
            Console.WriteLine("M1 is called");
        }

        static void SayHello(string name1,string name2)
        {
            Console.WriteLine($"hello,{name1} and {name2}");
        }
       
        static void Hello(string name,int round)
        {
            for (int i = 0; i < round; i++)
            {
                Console.WriteLine($"Hello,{name}");
            }
        }

        static int Add(int x,int y)
        {
            return x + y;
        }

        static double Mul(double x,double y)
            {
            return x * y;
        }
       
    }
}

例4--lambda表达式的简单应用

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp45
{
    class Program
    {
        static void Main(string[] args)
        {
            ///Lambda
            ///1.匿名方法
            ///2.Inline方法——调用才去声明的方法叫做Inline方法,比如临时用一下那种
             var func = new Func<int, int, int>((int a,int b)=> { return a + b; });///(int a,int b)=> { return a + b; }这就是lambda表达式
            //Func<int,int,int> func = (int a, int b) => { return a + b; };//还可以这样写,直接跟lambda表达式 
            int res = func(100, 100);
            Console.WriteLine(res );
            func = new Func<int, int, int>((x, y) => { return x * y; });///int可以省略
            res = func(3, 4);
            Console.WriteLine(res);
        }
    }
}

例5——泛型委托的类型推断

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp45
{
    class Program
    {
        static void Main(string[] args)
        {
            // DoSomeCalc<int>((int a, int b) => { return a * b; },100,200);    
            DoSomeCalc((a, b) => { return a * b; }, 100, 200);//泛型委托的参数类型推断,可以吧int都省略掉
        }
        static void DoSomeCalc<T>(Func<T,T,T>func,T x,T y)
        {
            T res = func(x, y);
            Console.WriteLine(res);
        }
    }
}

二、LinQ

1.什么是linq

linq是语言继承查询的缩写(Language Integrated Query)。做软件开发肯定是离不开数据局的,里面有各种查询语句。linq的提出就是为了提供一种跨越各种数据源的统一查询方式——它主要包含4个组件——Linq to Objects,Linq to XML,Linq to DataSet和Linq to SQL。

Linq to SQL:可以查询给予关系数据库的数据。微软只实现了SQLServer的查询操作。其他第三方也实现了很多。

Linq to DataSet:查询DataSet中的数据,并能对数据进行增删等操作。

Linq to XML:该足迹可以查询XML文件。

Linq to Objects:可以查询集合数据,如数组或List等。

例:操作一般数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp46
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] scores = new int[] { 99, 72, 89, 90, 81 };

            IEnumerable<int> scoreQuery = from score in scores where score > 80 select score;//linq语句

            foreach (var i in  scoreQuery)
            {
                Console.WriteLine(i);
            }
        }
    }
}

这是一个最简单的linq查询的使用方式。可以很方便的对数据进行筛选。

查询表达式必须以from子句开头,并且必须以select或者group子句结尾,在其之间,可以包含一个或者多个where语句、orderby语句、join语句。形式类似于SQL语言,示例:

var queryExp=from s in colletcion select s ;

小结:

Linq:集成语言查询的缩写(为了提供各种数据源的统一查询方式)

注意:

1.以from开头

2.在where后添加筛选条件

3.以select结尾

4.返回一个接口类数组

 

例:操作List对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp46
{
    class Program
    {
        static void Main(string[] args)
        {
            List<float> f = new List<float> { 1.1f, 1.2f, 1.3f, 3.3f, 2.1f };
            IEnumerable<float> newf = from newF in f where newF < 3.0f select newF;
            foreach (var i in newf)
            {
                Console.WriteLine(i);
            }
        }
    }
}

 

Stkcd [股票代码] ShortName [股票简称] Accper [统计截止日期] Typrep [报表类型编码] Indcd [行业代码] Indnme [行业名称] Source [公告来源] F060101B [净利润现金净含量] F060101C [净利润现金净含量TTM] F060201B [营业收入现金含量] F060201C [营业收入现金含量TTM] F060301B [营业收入现金净含量] F060301C [营业收入现金净含量TTM] F060401B [营业利润现金净含量] F060401C [营业利润现金净含量TTM] F060901B [筹资活动债权人现金净流量] F060901C [筹资活动债权人现金净流量TTM] F061001B [筹资活动股东现金净流量] F061001C [筹资活动股东现金净流量TTM] F061201B [折旧摊销] F061201C [折旧摊销TTM] F061301B [公司现金流1] F061302B [公司现金流2] F061301C [公司现金流TTM1] F061302C [公司现金流TTM2] F061401B [股权现金流1] F061402B [股权现金流2] F061401C [股权现金流TTM1] F061402C [股权现金流TTM2] F061501B [公司自由现金流(原有)] F061601B [股权自由现金流(原有)] F061701B [全部现金回收率] F061801B [营运指数] F061901B [资本支出与折旧摊销比] F062001B [现金适合比率] F062101B [现金再投资比率] F062201B [现金满足投资比率] F062301B [股权自由现金流] F062401B [企业自由现金流] Indcd1 [行业代码1] Indnme1 [行业名称1] 季度数据,所有沪深北上市公司的 分别包含excel、dta数据文件格式及其说明,便于不同软件工具对数据的分析应用 数据来源:基于上市公司年报及公告数据整理,或相关证券交易所、各部委、省、市数据 数据范围:基于沪深北证上市公司 A股(主板、中小企业板、创业板、科创板等)数据整理计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值