Questions tagged [method-group]
48 questions
407
votes
5 answers
What is a method group in C#?
I have often encountered an error such as "cannot convert from 'method group' to 'string'" in cases like:
var list = new List();
// ... snip
list.Add(someObject.ToString);
of course there was a typo in the last line because I forgot the…

Andrei Rînea
- 20,288
- 17
- 117
- 166
54
votes
2 answers
Cannot convert from 'method group' to 'System.Action
I have created the following function:
public void DelegatedCall(Action

Jonathan
- 543
- 1
- 4
- 5
28
votes
2 answers
C# Language Design: method group inside `is` operator
I'm interesting in some design choices of C# language.
There is a rule in C# spec that allows to use method groups as the expressions of is operator:
class Foo {
static void Main() { if (Main is Foo) Main(); }
}
Condition above is always false,…

controlflow
- 6,667
- 1
- 31
- 55
27
votes
0 answers
Applying "is" operator to method group: why compiler allows it?
Consider the following code:
var result = IDisposable.Dispose is object; //result equals false
It was surprise for me(and to my colleague that actually drew my attention to it) that this code is compiled.
First my thought was that…

alekseevi15
- 1,732
- 2
- 16
- 20
24
votes
7 answers
Are there any benefits to using a C# method group if available?
When dealing with something like a List 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…

Chris Missal
- 5,987
- 3
- 28
- 46
21
votes
6 answers
Cannot Assign because it is a method group C#?
Cannot Assign "AppendText" because it is a "method group".
public partial class Form1 : Form
{
String text = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
…

puretppc
- 3,232
- 8
- 38
- 65
20
votes
3 answers
Difference between lambda expression and method group
What's the difference between
Class1.Method1("cId", Facade.Customers.GetSingle);
and
Class1.Method1("cId", x => Facade.Customers.GetSingle(x));
?
Resharper suggests to use the first expression.

Alexandre
- 13,030
- 35
- 114
- 173
15
votes
1 answer
Delegate instance allocation with method group compared to
I started to use the method group syntax a couple of years ago based on some suggestion from ReSharper and recently I gave a try to ClrHeapAllocationAnalyzer and it flagged every location where I was using a method group in a lambda with the issue…

Amaury Levé
- 1,459
- 10
- 21
14
votes
2 answers
Using C# method group executes code
While updating my UI code (C# in a .NET 4.0 application), I ran into a strange crash due to a call to the UI being executed in the wrong thread. However, I was invoking that call on the main thread already, so the crash made no sense:…

Daniel Rose
- 17,233
- 9
- 65
- 88
13
votes
2 answers
C# method group strangeness
I discovered something very strange that I'm hoping to better understand.
var all = new List{
new int[]{1,2,3},
new int[]{4,5,6},
new int[]{7,8,9}
};
all.ForEach(n => n.ForEach(i…

Keith
- 2,618
- 4
- 29
- 44
12
votes
3 answers
Convert Method Group to Expression
I'm trying to figure out of if there is a simple syntax for converting a Method Group to an expression. It seems easy enough with lambdas, but it doesn't translate to methods:
Given
public delegate int FuncIntInt(int x);
all of the below are…

Arne Claassen
- 14,088
- 5
- 67
- 106
11
votes
2 answers
Method Inference does not work with method group
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…

Ohad Schneider
- 36,600
- 15
- 168
- 198
11
votes
3 answers
Visual Studio 2015 extension method call as method group
I have an extension method like
public static void RemoveDetail(this TMaster master, TChild child)
where TMaster : class, IMaster
where TChild : class, IDetail;
And I have two classes
public class…

dvas_dubna
- 129
- 5
10
votes
4 answers
Passing a method to a LINQ query
In a project I'm currently working on, we have many static Expressions that we have to bring in local scope with a variable when we call the Invoke method on them and pass our lambda expressions' arguments to.
Today, we declared a static method…

Sean Newell
- 1,033
- 1
- 10
- 25
9
votes
1 answer
Method group in VB.NET?
James Michael Hare recently wrote a blog post about Char static methods. He talks about using a method group to write less-wordy LINQ:
if (myString.Any(c => char.IsLower(c))) { xyzzy(); }
if (myString.Any(char.IsLower)) { xyzzy(); } // Less wordy…

Jeff B
- 8,572
- 17
- 61
- 140