You are here

Converting Java to C#

Encog started as a Java project. However, since v1.0 every release of Encog has had both a DotNet and a Java branch. Both branches are maintained by Jeff Heaton/Heaton Research. We feel that it is important for both sides of Encog to come from the same source. This encourages a great deal of consistency between the two Encog distributions. It also ensures that no branch of Encog will get too far behind the other.

Often a class will be released in either Java or C# first. These classes will need to be translated to the other side of Encog. This translation may or may not be done by the author. When classes are translated between Java and C# we want them to remain consistent, yet we also want each to implement unique features that its language provides. For example the DotNet side of Encog should use properties, where the Java side will use get/set methods.

This document provides a checklist of what you should translate when going from Java to C#. This is not meant to be an article on the differences between Java and C# syntax. We already have such an article here. Rather, this document is a checklist of points you should have addressed while translating the class.

1. Does it compile?
Before checking anything in, make sure it compiles and there is no Java code remaining.

2. Are the JavaDoc XML comments converted to DotNet XML comments?
The Java source files will have Java doc comments in front of classes, methods and properties. JavaDoc comments look like this:

/**
 * Called to update for each number that should be checked.
 * 
 * @param actual
 *            The actual values.
 * @param ideal
 *            The ideal values.
 */

These comments should be converted to C# XML comments that look like this:

/// <summary>
/// Called to update for each number that should be checked.
/// </summary>
/// <param name="actual">The actual number.</param>
/// <param name="ideal">The ideal number.</param>

3. Have all of the Java get/set methods been replaced with properties?
Java makes use of get/set methods, like you see here:

public ORMSession getSession() {
  return this.session;
}

public void setSession(final ORMSession session) {
  this.session = session;
}

C# supports properties and thus rarely implements get/set methods. You can see a C# property here.

public ORMSession Session
{
  get
  {
    return this.session;
  }
  set
  {
    this.session = value;
  }
}

4. Do method names, variables and properties follow C# naming conventions?
Both C# and Java generally follow "camel case" for identifier names. However C# usually capitalizes the first letter of a function name or property. A function in Java might be named "insertRecord", the same function name in C# would be "InsertRecord".

5. Does each source file contain the Encog common C# header?
All C# files should have the Encog common header, this header states that each file is released under the LGPL license. This header can be found here.

6. Do the classes use indexing?
In additon to properties, C# also allows properties to take an index. This causes the property to appear as a one or two dimensional array. There are not generally a tremendous amount of places to use indexing in Encog, but it should be taken advantage of when it makes sense. Consider the following Java method that accesses elements of a Matrix.

public double get(final int row, final int col) {
  validate(row, col);
  return this.matrix[row][col];
}

Using C# indexing, this could be rewritten as follows.

public double this[int row, int col]
{
  get
  {
    Validate(row, col);
    return this.matrix[row, col];
  }
  set
  {
    Validate(row, col);
    if (double.IsInfinity(value) || double.IsNaN(value))
    {
      throw new MatrixError("Trying to assign invalud number to matrix: "
        + value);
    }
  this.matrix[row, col] = value;
  }
}

7. Are interfaces named correctly?

Does every interface start with a capital I, for instance, change

Layer

to

ILayer

.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer