Codeporting has become a commonly used web based service by developers to convert their C# code to java which gives them the benefit of platform independence. C#2Java Engine accurately translates C# constructs to java.

Let me prove my point by taking the example of C# Indexers. For starters, Indexers creates classes or struct instances same way as of arrays but its not necessary that indexers are indexed by integer values, rather it depends on developers how he defines them.

Following examples shows how CodePorting C#2Java Engine translates C# Indexers to Java.

C# Code:

namespace CsPorter.Examples.Convert.Indexers

{

 Â Ã‚  public class Example14

 Â Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  void Method()

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //int indexer

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  int i = this[1];

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  this[2] = i;

 

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //enum, but int indexer in java

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  i = this[Example14Enum.Two];

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  this[Example14Enum.Two] = 1;

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  }

 

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //default java name: get/set

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  internal int this[int index]

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  get { return index; }

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  set { int i = value; }

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  }

 

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //java name should be changed to getByExample13Enum/setByExample13Enum

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  internal int this[Example14Enum idx]

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  get { return (int)idx; }

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  set { Example14Enum e = (Example14Enum)value; }

   Â Ã‚ Ã‚ Ã‚ Ã‚ }

 Â Ã‚  }

 

 Â Ã‚  internal class Example141

 Â Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  void Method(Example14 Example)

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //int indexer

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  int i = Example[1];

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  Example[2] = i;

 

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //enum, but int indexer in java

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  i = Example[Example14Enum.Two];

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  Example[Example14Enum.Two] = 1;

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  }

 Â Ã‚  }

 

 Â Ã‚  internal enum Example14Enum

 Â Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  One, Two, Three

 Â Ã‚  }

}

Java Code:

package CsPorter.Tests.Convert.Indexers;

 

// ********* THIS FILE IS AUTO PORTED FORM C# USING CODEPORTING.COM *********

 

import com.codeporting.csharp2java.java.Enum;

 

public class Test14

{

 Â Ã‚  private void method()

 Â Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //int indexer

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  int i = this.get(1);

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  this.set(2, i);

 

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //enum, but int indexer in java

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  i = this.getByExample14Enum(Example14Enum.TWO);

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  this.setByExample14Enum(Example14Enum.TWO, 1);

 Â Ã‚  }

 

 Â Ã‚  //default java name: get/set

 Â Ã‚  int get(int index) { return index; }

 Â Ã‚  void set(int index, int value) { int i = value; }

 

 Â Ã‚  //java name should be changed to getByExample13Enum/setByExample13Enum

 Â Ã‚  int getByExample14Enum(/*Example14Enum*/int idx) { return (int)idx; }

 Â Ã‚  void setByExample14Enum(/*Example14Enum*/int idx, int value) { /*Example14Enum*/int e = (/*Example14Enum*/int)value; }

}

 

class Example141

{

 Â Ã‚  private void method(Example14 Example)

 Â Ã‚  {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //int indexer

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  int i = Example.get(1);

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  Example.set(2, i);

 

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  //enum, but int indexer in java

 Â Ã‚ Ã‚   Â Ã‚ i = Example.getByExample14Enum(Example14Enum.TWO);

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  Example.setByExample14Enum(Example14Enum.TWO, 1);

 Â Ã‚  }

}

 

/*enum*/ final class Example14Enum extends Enum

{

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  private Example14Enum(){}

 Â Ã‚  public static final int ONE = 0; public static final int TWO = 1; public static final int THREE = 2;

 

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  static {

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  Enum.register(new Enum.SimpleEnum(Example14Enum.class, Integer.class) {{

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  addConstant(“ONE”, ONE);

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  addConstant(“TWO”, TWO);

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  addConstant(“THREE”, THREE);

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  }});

 Â Ã‚ Ã‚ Ã‚ Ã‚ Ã‚ Ã‚  }

 

}

 

It is clear from the above example that CodePorting C#2java accurately converts C# indexers to compile-able java code.

Newly added articles and documentation pages

About CodePorting C#2Java App

CodePorting helps you make your .NET applications cross platform compatible and allows migrating your .NET solutions, projects and files into Java in the cloud. Other than speed and accuracy of conversion; port your C# code directly either by uploading .cs files contained in a .zip file or import directly from popular version control repositories like GIT, Mercurial HG and SubVersion. You can also download a Microsoft Visual Studio plugin and convert C# code in the real time without leaving the development environment. You may also build your own customized code conversion applications using CodePorting APIs.

Read more about CodePorting

Contact Us

Suite 163, 79 Longueville Road

Lane Cove, NSW 2066, Australia

CodePorting ““ Your CodePorting Experts

Skype Name: CodePorting

Email: support [@] codeporting [dot] com

 

 

Â