method-group

Method Inference does not work with method group

旧街凉风 提交于 2019-11-26 21:20:31
问题 Consider void Main() { var list = new[] {"1", "2", "3"}; list.Sum(GetValue); //error CS0121 list.Sum(s => GetValue(s)); //works ! } double GetValue(string s) { double val; double.TryParse(s, out val); return val; } The description for the CS0121 error is The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Sum<string>(System.Collections.Generic.IEnumerable<string>, System.Func<string,decimal>)' and 'System.Linq.Enumerable.Sum<string>(System.Collections

Overloaded method-group argument confuses overload resolution?

核能气质少年 提交于 2019-11-26 20:44:05
The following call to the overloaded Enumerable.Select method: var itemOnlyOneTuples = "test".Select<char, Tuple<char>>(Tuple.Create); fails with an ambiguity error (namespaces removed for clarity): The call is ambiguous between the following methods or properties: 'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>,Func<char,Tuple<char>>)' and 'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>, Func<char,int,Tuple<char>>)' I can certainly understand why not specifying the type-arguments explicitly would result in an ambiguity (both the overloads would apply), but I don't see one

Are there any benefits to using a C# method group if available?

我的未来我决定 提交于 2019-11-26 16:44:45
问题 When dealing with something like a List<string> you can write the following: list.ForEach(x => Console.WriteLine(x)); or you can use a method group to do the same operation: list.ForEach(Console.WriteLine); I prefer the second line of code because it looks cleaner to me, but are there any benefits to this? 回答1: Well, lets take a look and see what happens. static void MethodGroup() { new List<string>().ForEach(Console.WriteLine); } static void LambdaExpression() { new List<string>().ForEach(x

Overloaded method-group argument confuses overload resolution?

怎甘沉沦 提交于 2019-11-26 09:03:05
问题 The following call to the overloaded Enumerable.Select method: var itemOnlyOneTuples = \"test\".Select<char, Tuple<char>>(Tuple.Create); fails with an ambiguity error (namespaces removed for clarity): The call is ambiguous between the following methods or properties: \'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>,Func<char,Tuple<char>>)\' and \'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>, Func<char,int,Tuple<char>>)\' I can certainly understand why not specifying the type

What is a method group in C#?

心已入冬 提交于 2019-11-25 21:59:13
问题 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: var list = new List<string>(); // ... snip list.Add(someObject.ToString()); // <- notice the parentheses However I came to wonder what is a method group. Google isn\'t much of a