C#中的方法组是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/886822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a method group in C#?
提问by Andrei R?nea
I have often encountered an error such as "cannot convert from 'method group' to 'string'" in cases like:
在以下情况下,我经常遇到诸如“无法从‘方法组’转换为‘字符串’”之类的错误:
var list = new List<string>();
// ... snip
list.Add(someObject.ToString);
of course there was a typo in the last line because I forgot the invocation parentheses after ToString
. The correct form would be:
当然最后一行有错别字,因为我忘记了后面的调用括号ToString
。正确的形式是:
var list = new List<string>();
// ... snip
list.Add(someObject.ToString()); // <- notice the parentheses
However I came to wonder what is a method group. Googleisn't much of a help nor MSDN.
采纳答案by Marc Gravell
A method group is the name for a set of methods(that might be just one) - i.e. in theory the ToString
method may have multiple overloads (plus any extension methods): ToString()
, ToString(string format)
, etc - hence ToString
by itself is a "method group".
方法组是一个名称的一套方法(可能是只有一个) -即理论上的ToString
方法可以有多个重载(加任何扩展方法)ToString()
,ToString(string format)
等-因此ToString
本身是一个“方法组”。
It can usually convert a method group to a (typed) delegate by using overload resolution - but not to a string etc; it doesn't make sense.
它通常可以通过使用重载解析将方法组转换为(类型化)委托 - 但不能转换为字符串等;这没有意义。
Once you add parentheses, again; overload resolution kicks in and you have unambiguously identified a method call.
再次添加括号后;重载决议开始了,您已经明确地确定了一个方法调用。
回答by oscarkuo
The first result in your MSDN search said:
MSDN 搜索中的第一个结果是:
The method group identifies the one method to invoke or the set of overloaded methods from which to choose a specific method to invoke
方法组标识要调用的一个方法或从中选择要调用的特定方法的一组重载方法
my understanding is that basically because when you just write someInteger.ToString
, it may refer to:
我的理解是,基本上是因为你刚写的时候someInteger.ToString
,可能指的是:
Int32.ToString(IFormatProvider)
or it can refer to:
或者它可以指:
Int32.ToString()
so it is called a method group.
所以称为方法组。
回答by 1800 INFORMATION
The ToString
function has many overloads - the method group would be the group consisting of all the different overloads for that function.
该ToString
函数有许多重载 - 方法组将是由该函数的所有不同重载组成的组。
回答by Kaeles
Also, if you are using LINQ, you can apparently do something like myList.Select(methodGroup)
.
此外,如果您使用的是 LINQ,您显然可以执行类似myList.Select(methodGroup)
.
So, for example, I have:
所以,例如,我有:
private string DoSomethingToMyString(string input)
{
// blah
}
Instead of explicitly stating the variable to be used like this:
而不是像这样明确说明要使用的变量:
public List<string> GetStringStuff()
{
return something.getStringsFromSomewhere.Select(str => DoSomethingToMyString(str));
}
I can just omit the name of the var:
我可以省略 var 的名称:
public List<string> GetStringStuff()
{
return something.getStringsFromSomewhere.Select(DoSomethingToMyString);
}
回答by Hyman
You can cast a method groupinto a delegate.
您可以将方法组转换为委托。
The delegate signature selects 1 method out of the group.
委托签名从组中选择 1 个方法。
This example picks the ToString()
overload which takes a string parameter:
此示例选择ToString()
采用字符串参数的重载:
Func<string,string> fn = 123.ToString;
Console.WriteLine(fn("00000000"));
This example picks the ToString()
overload which takes no parameters:
此示例选择ToString()
不带参数的重载:
Func<string> fn = 123.ToString;
Console.WriteLine(fn());