Difference between Java and C# lambda expression? [Resolved]

Posted by Crniranjanraj under C# on 10/3/2015 | Points: 10 | Views : 365 | Status : [Member] | Replies : 2
What is the difference between Java and C# lambda expression?




Responses

Posted by: Rajnilari2015 on: 10/5/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
A lambda expression is an anonymous function. Simply put, it's a method without a declaration, i.e., access modifier, return value declaration, and name.Lambda expression facilitates functional programming.

Following are the important characteristics of a lambda expression -

Optional type declaration - No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.

Optional parenthesis around parameter - No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.

Optional curly braces - No need to use curly braces in expression body if the body contains a single statement.

Optional return keyword - The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.

Lambda syntax :
Parameters => Executed code

In JAVA 1.8, it has been introduced. Here is an example

//Method: getFilterCandidateInfo

//Purpose: Get candidates filtered by RollNumber
public List<Candidate> getFilterCandidateInfo(String rollNumber){

return getAllCandidateInfo()
.stream() //converts the List to Stream
.filter(rn -> rn.equals(rollNumber)) //Performs the Filter Operation
.collect(Collectors.toList());//converts stream to List

}

You can figure out that inside .filter(rn -> rn.equals(rollNumber)) we are using -> operator. It is the LAMBDA way.

The same in C# will be
public List<Candidate> getFilterCandidateInfo(String rollNumber){


return getAllCandidateInfo().Where(rn=>rn.equals(rollNumber)).ToList();
}


More about Lambda:

https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

https://msdn.microsoft.com/en-IN/library/bb397687.aspx

Hope this helps.

--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Crniranjanraj on: 10/5/2015 [Member] Starter | Points: 25

Up
0
Down
thanks bro. you explain nicely

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response