CodePorting C#2Java Application is a cloud based converter that has accomplished immense popularity because of its broad collection of supported C# language constructs. Its powerful engine parse and transforms thousand of C# souce code lines to java in few seconds.
CodePorting has recently imrpoved its C#2Java engine and now its capable of converting C# source code having delegates in it to java. In C# programming language, delegate is a form of type-safe function pointer which is mostly used for call backs and event listener. Unfortunately, there is no delegate construct in Java so while converting C# code to Java, C#2Java engine intelligently converts the delegates to interfaces with the invoke method call by auto generating required code.
Lets take an example of how codeporting engine converts C# delegates to java interfaces.
C# Code:
using System.Collections.Generic;
using System;
using System.Linq;
public class Delegate
{
public static void Main(string[] arg)
{
Del handler = DelegateMethod;
// Call the delegate.
handler("Hello World");
}
public delegate void Del(string message);
public static void DelegateMethod(string message)
{
Console.WriteLine(message);
}
}
Java Code:
// ********* THIS FILE IS AUTO PORTED FROM C# USING CODEPORTING.COM *********
public class Delegate
{
public static void main(String[] arg)
{
Del handler = new Del() {
public void invoke(String message) {
delegateMethod(message);
}};
// Call the delegate.
handler.invoke("Hello World");
}
public interface Del{
void invoke(String message);
}
public static void delegateMethod(String message)
{
System.out.printf(message);
}
}