简体   繁体   English

对Func,Delegates和Anonymous类型感到困惑

[英]Confused about Func, Delegates and Anonymous types

Reading this line of code example from a book: 从书中读取以下代码示例:

Func<string,int> returnLength;
returnLength = delegate (string text) { return text.Length; };

Console.WriteLine(returnLength("Hello"));

It says 它说

Func<string,double,int> is equivalent to a delegate type of the form public delegate int SomeDelegate(string arg1, double arg2) Func<string,double,int>等效于public delegate int SomeDelegate(string arg1, double arg2)形式的委托类型public delegate int SomeDelegate(string arg1, double arg2)

So Func is a delegate? Func是代表吗? Then what is that delegate we have defined again in the code example? 然后我们在代码示例中再次定义了什么代理? We define a variable from a Func that is like a delegate and then assign it again to another delegate ? 我们从Func定义一个类似delegate的变量,然后再将它分配给另一个delegate I am super confused and can't understand this concept. 我非常困惑,无法理解这个概念。 :( :(

Can someone explain it? 有人可以解释一下吗?

So Func is a delegate? Func是代表吗?

Yes, it's defined as follows: 是的,它的定义如下:

public delegate TResult Func<in T, out TResult>(T arg)

It's described as: 描述为:

Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter. 封装具有一个参数的方法,并返回由TResult参数指定的类型的值。

Read Func Delegate for more. 阅读Func Delegate了解更多。

Then what is that delegate we have defined again in the code example? 然后我们在代码示例中再次定义了什么代理?

That's the anonymous method we create to assign it to the Func -typed variable. 这是我们创建的匿名方法 ,用于将其分配给Func -typed变量。 You could use named method or lambda expression as well, as long as the input and return types match the Func generic parameters. 只要输入和返回类型与Func泛型参数匹配,您也可以使用命名方法或lambda表达式

We define a variable from a Func that is like a delegate and then assign it again to another delegate? 我们从Func定义一个类似委托的变量,然后再将它分配给另一个委托?

No, we create a variable typed as Func<string, int> and then create a delegate matching that type and assign it to the variable. 不,我们创建一个类型为 Func<string, int>的变量,然后创建与该类型匹配的委托并将其分配给该变量。


Additional sources from MSDN: 来自MSDN的其他来源:

the same code you could just write 您可以编写相同的代码

Func<string,int> returnLength = (s) => {return s.Length;}

assigning delegate to a Func is just like assigning a variable to another.. 将委托分配给Func就像将变量分配给另一个变量一样。

Your code can also be expressed like this: 您的代码也可以这样表示:

Func<string,int> returnLength = delegate (string text) { return text.Length; };

Console.WriteLine(returnLength("Hello"));

A func is a delegate. 函数是委托。

  1. Func<string, int> is the declaration, Func<string, int>是声明,
  2. delegate (string text) { return text.Length; } delegate (string text) { return text.Length; } is the function that you are assigning. delegate (string text) { return text.Length; }是您要分配的功能。
    1. This delegate (function) accepts a string ( string text ) and returns an integer ( return text.Length; ). 该委托(函数)接受字符串( string text )并返回整数( return text.Length; )。

This would return 5 as the text.Length of "Hello" is 5. It is not as you are creating 2 delegates in this example, rather you are just assigning one. 这将返回5作为文本。“Hello”的长度为5.这不是因为您在此示例中创建了2个委托,而是您只是分配一个。

Yes a Func is equivalent to a delegate type with a return type. 是的, Func等效于具有返回类型的delegate类型。 The delegate you have defined is the delegate that is executed when the method is called returning the int type. delegate您已经定义是delegate时,调用该方法返回时执行int类型。

This can be shortcutted down to 这可以缩短到

Func<string, int> returnLength = (text) => { return text.length; };

In this situation the delegate returns a value type of int (Int32) 在这种情况下,委托返回的值类型为int(Int32)

Now Action is also a delegate type without a return value such as. 现在,Action也是没有返回值的委托类型。

Action<string> action = delegate(string text) { text = ""; };

or 要么

Action<string> action = (text) => { text = ""; };

Now anonymous types are much different than a delegate , Action or Func . 现在,匿名类型与delegateActionFunc有很大不同。 Now Func & Action were implemented in .Net 3.5 after delegates were introduced. 在引入代表之后,.net 3.5中实现了FuncAction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM