What is a method group in C#?

前端 未结 5 2111
夕颜
夕颜 2020-11-22 01:46

I have often encountered an error such as \"cannot convert from \'method group\' to \'string\'\" in cases like:

var list = new List();
// ...          


        
5条回答
  •  天命终不由人
    2020-11-22 01:59

    Also, if you are using LINQ, you can apparently do something like 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 GetStringStuff()
    {
        return something.getStringsFromSomewhere.Select(str => DoSomethingToMyString(str));
    }
    

    I can just omit the name of the var:

    public List GetStringStuff()
    {
        return something.getStringsFromSomewhere.Select(DoSomethingToMyString);
    }
    

提交回复
热议问题