Introduce the difference between java and c# lambda expression.
UnderStanding of Lambda in Java and C#
ββIn Java and C#, if you want to use lambda expression, then the object must first be a unnamed delegate. So in essence, lambda is a anonymous delegate. As we all know that delegate is a function pointer, although there is no delegate in Java, but we can use it like this:
C#
1
2
3
4
5
6
7
8
9
10
11
12
|
class Program
{
public delegate void delType(string s);
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");
delType d = s => Console.WriteLine("Hello " + s);
//d("world!");
d.Invoke("world!");
}
}
|
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Main {
public static void main(String[] args) {
// write your code here
IDelType d = s -> System.out.println("Hello " + s);
d.test("world!");
}
}
interface IDelType{
//Interface with only one member method.
public void test(String s);
}
|