method-group

Convert Method Group to Expression

房东的猫 提交于 2019-12-01 15:03:47
问题 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 valid: Func<int, int> func1 = x => x; FuncIntInt del1 = x => x; Expression<Func<int, int>> funcExpr1 = x => x; Expression<FuncIntInt> delExpr1 = x => x; But if i try the same with an instance method, it breaks down at the Expressions: Foo foo = new Foo();

Passing a method to a LINQ query

耗尽温柔 提交于 2019-12-01 06:34:53
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 whose parameter is exactly the type that the query is expecting. So, my coworker and I were messing around to see if we could get this method to do the project in the Select statement of our query, instead of invoking it on the whole object, without bringing it into local scope. And it worked! But we do not understand why. Imagine code like this // old

C# delegate contravariance with lambda expression [duplicate]

我的梦境 提交于 2019-11-29 15:12:18
This question already has an answer here: Can’t assign to delegate an anonymous method with less specific parameter type 3 answers 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 { private abstract class Animal {} private class Tiger : Animal {} private delegate Type D1(Tiger tiger); private static Type M1(Animal animal) { return animal.GetType(); } [Test] public void ContravariantDelegateWithMethod() {

C# method group strangeness

 ̄綄美尐妖づ 提交于 2019-11-29 09:24:12
I discovered something very strange that I'm hoping to better understand. var all = new List<int[]>{ new int[]{1,2,3}, new int[]{4,5,6}, new int[]{7,8,9} }; all.ForEach(n => n.ForEach(i => Console.WriteLine(i))); which can be rewritten as: ... all.ForEach(n => n.ForEach(Console.WriteLine)); How is it possible to leave out the lambda expression parameter (i=>) and still have the current item passed to console.WriteLine? Thanks for any insight. -Keith List<T>.ForEach is looking for an Action<T> . When you write n.ForEach(Console.WriteLine); what you have here is one of the members of the method

C# Language Design: method group inside `is` operator

旧巷老猫 提交于 2019-11-28 17:15:28
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, as the specification says: 7.10.10 The is operator • If E is a method group or the null literal, of if the type of E is a reference type or a nullable type and the value of E is null, the result is false. My questions: what is the purpose/point/reason of allowing to use the C# language element with no runtime representation in CLR like method groups

Method group in VB.NET?

爷,独闯天下 提交于 2019-11-28 13:34:42
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 FTW! The equivalent in VB.NET would be: If myString.Any(Function(c) Char.IsLower(c)) Then xyzzy() If myString.Any(Char.IsLower) Then xyzzy() 'Compiler error Sadly, I can't do the equivalent of C# here... the compiler tells me that Overload resolution failed because no accessible 'IsLower' accepts this number of arguments ... sadness. I thought it

C# delegate contravariance with lambda expression [duplicate]

99封情书 提交于 2019-11-28 08:56:50
问题 This question already has an answer here: Can’t assign to delegate an anonymous method with less specific parameter type 3 answers 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 { private abstract class Animal {} private class Tiger : Animal {} private delegate Type D1(Tiger tiger); private static Type

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

社会主义新天地 提交于 2019-11-27 14:20:46
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? 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 => Console.WriteLine(x)); } This gets compiled into the following IL. .method private hidebysig static void

C# Language Design: method group inside `is` operator

夙愿已清 提交于 2019-11-27 10:33:34
问题 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, as the specification says: 7.10.10 The is operator • If E is a method group or the null literal, of if the type of E is a reference type or a nullable type and the value of E is null, the result is false. My questions: what is the purpose/point/reason

Method group in VB.NET?

て烟熏妆下的殇ゞ 提交于 2019-11-27 07:45:40
问题 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 FTW! The equivalent in VB.NET would be: If myString.Any(Function(c) Char.IsLower(c)) Then xyzzy() If myString.Any(Char.IsLower) Then xyzzy() 'Compiler error Sadly, I can't do the equivalent of C# here... the compiler tells me that Overload resolution