12.16 C#基础

一、Func介绍

Func是一种内置的泛型委托类型,可以与方法、匿名方法或lambda表达式一起使用。

Func可以包含0~16个输入参数,并且必须具有一种返回类型。

public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);

例如,此委托封装了一个具有两个参数并返回参数指定类型的值的方法Tresult。

二、Func简单实例

string GetMessage()
{
    return "你好!";
}
Func<string> sayHello = GetMessage;
Console.WriteLine(sayHello());

打印输出:

你好!

三、Func一般实例

int Sum(int x, int y) 
{ 
    return x + y; 
} 
Func<int, int, int> add = Sum; 
int res = add(150, 10); 
Console.WriteLine(res);

打印输出:

160
int Sum(int x, int y, int z) 
{ 
    return x + y + z; 
} 
Func<int, int, int, int> add = Sum; 
int res = add(150, 20, 30); 
Console.WriteLine(res);

打印输出:

200

如果没有内置Func委托,我们需要声明我们的自定义委托。

int Sum(int x, int y)
{
    return x + y;
}
Add AddTwo = Sum;
int res = AddTwo(150, 10);
Console.WriteLine(res);
delegate int Add(int x, int y);

四、带有lambda表达式的Func

插播一条:C#函数 Func(string a, string b)用 Lambda 表达式怎么写?

(a,b) => {};

C#lambda表达式简化了C#的创建Func。

Func<int, int, int> randInt = (n1, n2) => new Random().Next(n1, n2); 
Console.WriteLine(randInt(1, 100));

创建了一个返回随机整数的函数。委托接受随机范围的下限和上限的两个值。

五、Func委托列表

Func可以放入容器中。

var vals = new int[] { 1, 2, 3, 4, 5···125 }; 

Func<int, int> square = x => x * x; 
Func<int, int> cube = x => x * x * x; 
函数<int, int> inc = x => x + 1; 

var fns = new List<Func<int, int>> 
{ 
    square, cube, inc
}; 

foreach (var fn in fns) 
{ 
    var res = vals.Select(fn); 

    Console.WriteLine(string.Join(", ", res)); 
}

运行结果:

2、3、4、5、6 
1、4、9、16、25 
1、8、27、64、125

六、Func过滤器数组

record User(int id, string Name, string City, string DateOfBirth);
User[] users =
{
  new (1, "John", "London", "2001-04-01"),
  new (2, "Lenny", "New York", "1997-12-11"),
  new (3, "Andrew", "Boston", "1987-02-22"),
  new (4, "Peter", "Prague", "1936-03-24"),
  new (5, "Anna", "Bratislava", "1973-11-18"),
  new (6, "Albert", "Bratislava", "1940-12-11"),
  new (7, "Adam", "Trnava", "1983-12-01"),
  new (8, "Robert", "Bratislava", "1935-05-15"),
  new (9, "Robert", "Prague", "1998-03-14"),
};
var city = "Bratislava";
Func<User, bool> livesIn = e => e.City == city;
var res = users.Where(livesIn);
foreach (var e in res)
{
    Console.WriteLine(e);
}

运行结果:

User { id = 5 , Name = Anna , City = Bratislava , DateOfBirth = 1973 - 11 - 18 } 
User { id = 6 , Name = Albert , City = Bratislava , DateOfBirth = 1940 - 12 - 11 } 
User { id = 8 , Name = Robert , City = DateOfBirth , DateOfBirth = 1935-05-15 }

七、将Func作为参数传递

var data = new List<Person> 
{ 
    new ("John Doe", "gardener"), 
    new ("Robert Brown", "programmer"), 
    new ("Lucia Smith", "teacher"), 
    new ("Thomas Neuwirth ", "teacher") 
}; 

ShowOutput(data, r => r.Occupation == "teacher"); 

void ShowOutput(List<Person> list, Func<Person, bool> condition) 
{ 
    var data = list.Where(condition); 

    foreach (var person in data) 
    { 
        Console.WriteLine("{0}, {1}", person.Name, person.Occupation); 
    } 
}

record Person(string Name, string Occupation);

https://www.cnblogs.com/li150dan/p/17391919.html

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值