CodePorting Converts C# Delegates to Java Instantly

Discussion in 'Programming' started by zarfishsn, Sep 27, 2012.

  1. #1
    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.

    [B][B]C# Code:[/B][/B]
     
    using System.Collections.Generic;
    using System;
    using System.Linq;
    [B]public[/B] [B]class[/B] Delegate
    {
        [B]public[/B] [B]static[/B] [B]void[/B] Main(string[] arg)
        {
     
            Del handler = DelegateMethod;
     
            [I]// Call the delegate.[/I]
            handler("Hello World");
     
        }
       [B]public[/B] delegate [B]void[/B] Del(string message);
     
       [B]public[/B] [B]static[/B] [B]void[/B] DelegateMethod(string message)
       {
           Console.WriteLine(message);
     
       }
    }
     
    [B][B]Java Code:[/B][/B]
     
    [I]// ********* THIS FILE IS AUTO PORTED FROM C# USING CODEPORTING.COM *********[/I]
     
    [B]public[/B] [B]class[/B] Delegate
    {
        [B]public[/B] [B]static[/B] [B]void[/B] main(String[] arg)
        {
     
            Del handler = [B]new[/B] Del() {
    [B]public[/B] [B]void[/B] invoke(String message) {
     delegateMethod(message);
    }};
     
            [I]// Call the delegate.[/I]
            handler.invoke("Hello World");
     
        }
       [B]public[/B] [B]interface[/B] Del{
              [B]void[/B] invoke(String message);
    }
     
       [B]public[/B] [B]static[/B] [B]void[/B] delegateMethod(String message)
       {
           System.out.printf(message);
     
       }
    }
    Code (markup):

     
    zarfishsn, Sep 27, 2012 IP