当前位置:网站首页>Func < T, tresult > Commission - learning record
Func < T, tresult > Commission - learning record
2022-06-26 05:51:00 【self85】
a key
Namespace :System
Assembly :mscorlib.dll, System.Runtime.dll
A value must be returned
Tips : The following is the main body of this article , The following cases can be used for reference
One 、Func<T,TResult> What is a commission ?
Encapsulate a method , The method has one parameter , And returned by TResult The value of the type specified by the parameter
public delegate TResult Func<in T,out TResult>(T arg);
T The parameter types of the methods encapsulated for this delegate .
TResult The return value type of the method encapsulated for this delegate .
Two 、 Example
1. Routine usage
The following example demonstrates how to declare and use Func<T,TResult> entrust . This example declares a Func<T,TResult> Variable , And assign a lambda expression , This expression converts the characters in the string to uppercase . The delegate that encapsulates this method is then passed to Enumerable.Select Method , To change the string in the string array to uppercase .
The code is as follows ( Example ):
// Declare a Func variable and assign a lambda expression to the
// variable. The method takes a string and converts it to uppercase.
Func<string, string> selector = str => str.ToUpper();
// Create an array of strings.
string[] words = {
"orange", "apple", "Article", "elephant" };
// Query the array and select strings according to the selector method.
IEnumerable<String> aWords = words.Select(selector);
// Output the results to the console.
foreach (String word in aWords)
Console.WriteLine(word);
/* This code example produces the following output: ORANGE APPLE ARTICLE ELEPHANT */
annotation : Use this delegate to represent a method , This method can be passed as a parameter , Without explicitly declaring a custom delegate . The encapsulated method must correspond to the method signature defined by this delegate . It means , The encapsulated method must have a parameter passed to it by value , And must return a value . To reference a method , This method takes one parameter and returns void Methods Function , Use generics instead Action entrust .
2. lambda Expression assigned to Func<T,TResult> entrust
The code is as follows ( Example ):
Func<string, string> convert = s => s.ToUpper();
string name = "Dakota";
Console.WriteLine(convert(name));
// This code example produces the following output:
//
// DAKOTA
Lambda The underlying type of an expression is a generic type Func entrust . such , Can will lambda The expression is passed as an argument , Instead of explicitly assigning it to delegates . To be specific , Because there are many types of methods in the namespace System.Linq There are Func<T,TResult> Parameters , So you can pass these methods to lambda expression , Without explicit instantiation Func<T,TResult> entrust .
summary
Func<T1,T2,T3,T4,T5,T6,T7,T8,TResult> entrust
public delegate TResult Func<in T1,in T2,in T3,in T4,in T5,in T6,in T7,in T8,out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8);
The method has eight parameters , And back to TResult The value of the type specified by the parameter . also 9-16 Parameters , Simply put, a delegate with a return value , A value must be returned . In daily use , I'm familiar with it after using it twice , Don't try to remember
边栏推荐
- Mise en file d'attente des messages en utilisant jedis Listening redis stream
- 循环位移
- 电机专用MCU芯片LCM32F037系列内容介绍
- kolla-ansible部署openstack yoga版本
- [C language] deep analysis of data storage in memory
- Describe an experiment of Kali ARP in LAN
- cross entropy loss = log softmax + nll loss
- Ribbon load balancing service call
- Yamaha robot splits visual strings
- kolla-ansible部署openstack yoga版本
猜你喜欢

Implementation of third-party wechat authorized login for applet

421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)

REUSE_ ALV_ GRID_ Display event implementation (data_changed)

小程序如何关联微信小程序二维码,实现二码聚合
转帖——不要迷失在技术的海洋中

Prototype mode, Baa Baa

Bingc (inheritance)

【 langage c】 stockage des données d'analyse approfondie en mémoire

Navicat如何将当前连接信息复用另一台电脑

DOM文档
随机推荐
1212312321
Bubble sort
Household accounting procedures (the second edition includes a cycle)
Class and object learning
Could not get unknown property ‘*‘ for SigningConfig container of type org. gradle. api. internal
Describe an experiment of Kali ARP in LAN
项目中止
The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
A new explanation of tcp/ip five layer protocol model
Old love letters
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
ZigBee learning in simple terms Lecture 1
Feelings of virtual project failure
June 3 is a happy day
Machine learning 05: nonlinear support vector machines
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
About XXX management system (version C)
组合模式、透明方式和安全方式
Gram matrix
What management systems (Updates) for things like this