C# equivalent to Java 8 "method reference"
You would have to declare a method outside of Thing
(or a static Thing
method), then you could pass a method-group reference to it:
private string GetName(Thing thing)
{
return thing.Name;
}
...
List<String> nameList1 = thingList.Select(GetName).ToList();
In C# 6, you can also use an expression-bodied function to save a couple of lines:
private string GetName(Thing thing) => thing.Name;
c# has a equivalent, this feature is callind Method Group
see more:
What is a method group in C#?
sample:
private static int[] ParseInt(string s)
{
var t = ParseString(s);
var i = t.Select(x => int.Parse(x));
return i.ToArray();
}
with metod group:
private static int[] ParseInt(string s)
{
var t = ParseString(s);
var i = t.Select(int.Parse);
return i.ToArray();
}