Questions tagged [method-group]

48 questions
8
votes
4 answers

why delegate must be static?

In code below I must declare method MdrResponseInterpreter static otherwise I have compilation error. class.... { private StandardBuilder _mdrResponseBuilder = new StandardBuilder(MdrResponseInterpreter); public static bool…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
8
votes
2 answers

What is this ReSharper snippet 'convert to method group' actually doing?

Code before the changes: List model = brands.Select(item => Mapper.Map(item)).ToList(); Code after the improvement: List model = brands.Select(Mapper.Map
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
8
votes
2 answers

Overloaded method-group argument confuses overload resolution?

The following call to the overloaded Enumerable.Select method: var itemOnlyOneTuples = "test".Select>(Tuple.Create); fails with an ambiguity error (namespaces removed for clarity): The call is ambiguous between the following…
Ani
  • 111,048
  • 26
  • 262
  • 307
6
votes
1 answer

Why does passing a method group to an overloaded method cause ambiguity when calling the method in a lambda does not in this case?

Why can't the correct overload to be called be inferred on the line marked // Compiler Error in the code below when the type is correctly inferred in all the other cases? public static class Code { private static void Main() { …
Redwood
  • 66,744
  • 41
  • 126
  • 187
4
votes
2 answers

Error Assigning Delegate Using ? : Syntax

I've created a delegate and two matching methods. private delegate bool CharComparer(char a, char b); // Case-sensitive char comparer private static bool CharCompare(char a, char b) { return (a == b); } // Case-insensitive char…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
4
votes
1 answer

Why doesn't the C# compiler resolve Delegate types only different in return type when passing a method group as parameter?

Please consider the following code: class Program { static void Foobar() { } static void Test(Action a) { } static void Test(Func a) { } static void Main(string[] args) { Test(Foobar); } } It does not compile…
Imi
  • 1,579
  • 1
  • 12
  • 22
4
votes
2 answers

C# delegate contravariance with lambda expression

The second test method below does not compile (cannot convert lambda expression to target type D1). Does that mean that (non-generic) delegate contravariance does not work with lambda expressions? [TestFixture] public class MyVarianceTests { …
mtraudt
  • 103
  • 4
3
votes
2 answers

Why does adding a return type prevent me from using method group syntax?

I'm trying to use a method group in a lambda expression, like this: public class Foo { public void Hello(string s) { } } void Test() { // this works as long as Hello has a void return type Func> a = foo =>…
ladenedge
  • 13,197
  • 11
  • 60
  • 117
3
votes
1 answer

C# LINQ - Does Select filter nulls when used with a method group?

Today I wrote some code that takes a list of class A instances and creates a list of class B instances using a static factory method: public abstract class B { public static B CreateB(A a) { // return a new B instance or null if a B…
meir shapiro
  • 647
  • 7
  • 16
3
votes
0 answers

Method group conversions vs. boxing

I've created a struct named Point and a List of such points: var myPoints = new List { new Point() {Col = 3, Row = 2}, new Point() {Col = 2, Row = 4}, new Point() {Col = 5, Row = 4}, }; When I try to print them using ForEach()…
Mikhail Dudin
  • 459
  • 5
  • 15
3
votes
2 answers

Cannot Assign to 'Money' Because It Is a 'Method Group'

I'm doing a project for class, and I have to call the Money function from my Player class. However, I do not know how to change Money into something else that is not a Method Group. I don't know much programming, so my range of solutions is rather…
FCain 1026
  • 59
  • 2
  • 6
3
votes
3 answers

When is it valid to say a method group conversion has taken place?

So i'm not really convinced when its safe to say that a method group conversion occured. We have this multicast delegate from a previous post: public partial class MainPage : PhoneApplicationPage { public delegate void MyDelegate(int a, int b); //…
Freeman
  • 5,691
  • 3
  • 29
  • 41
3
votes
7 answers

"<" operator error

Why is the ( i < UniqueWords.Count ) expression valid in the for loop, but returns "CS0019 Operator '<' cannot be applied to operands of type 'int' and 'method group'" error when placed in my if? They are both string arrays, previously declared. for…
Nona Urbiz
  • 4,873
  • 16
  • 57
  • 84
2
votes
2 answers

Pass a C# method group?

Is it possible to pass a method group as an argument in C#? I am using a lot of mocks. I have the following: public static List InvocationsWithName( this Mock mock, string methodName) where T: class { var invocations =…
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
2
votes
0 answers

How to prevent method group from discarding nullability of generic reference type?

Consider this generic identity function: T Id(T x) => x; If I use it in a lambda mapping the elements in an array of string? values, it works as expected (i.e. it preserves the information, that the values are nullable). var xs = new[] {…