Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent to Java 8 "method reference"

Tags:

java

c#

java-8

I recently had the opportunity to tweak some Java code and was able to take advantage of some new Java 8 features. In one particular case I needed to get a List of (String) .Name properties from a List of objects. A simplified example of what I did was:

// sample data: <Thing> objects have a single String property called "Name"  List<Thing> thingList =     new ArrayList<>(Arrays.asList(new Thing("Thing1"), new Thing("Thing2")));  // first pass List<String> nameList = new ArrayList<>(); thingList.forEach(x -> nameList.add(x.getName()));  // refinement 1: use stream, map, and collect List<String> nameList1 =     thingList.stream().map(x -> x.getName()).collect(Collectors.toList());  // refinement 2: use "Thing::getName" method reference List<String> nameList2 =     thingList.stream().map(Thing::getName).collect(Collectors.toList()); 

I was curious to see how those approaches would translate to C#, and I got

// sample data: <Thing> objects have a single String property called "Name" var thingList = new List<Thing> { new Thing("Thing1"), new Thing("Thing2") };  // first pass var nameList = new List<String>(); thingList.ForEach(x => nameList.Add(x.Name));  // refinement 1: use Select and ToList List<String> nameList1 = thingList.Select(x => x.Name).ToList(); 

What I haven't found (yet?) is a C# equivalent of "refinement 2" to replace the Lambda expression with something (a little bit) more concise. Is there a C# equivalent to the Java 8 "method reference" in this case, given that I'm trying to get a property of each object (which in Java is done using a getProperty method)?

like image 755
Gord Thompson Avatar asked May 12 '16 00:05

Gord Thompson


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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; 
like image 108
Blorgbeard Avatar answered Sep 25 '22 22:09

Blorgbeard


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(); } 
like image 23
Adriano Moreira Avatar answered Sep 22 '22 22:09

Adriano Moreira