一.泛型委托
例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);
}
}
}
}