GitSharp/Non Trivial Java To CSharp Conversions

From eqqon

< GitSharp(Difference between revisions)
Jump to: navigation, search
(New page: This page lists some commonly required analogues between Java and C# that are not so trivial. This collection has been compiled while porting GitSharp. === Unsigned right shift operat...)
m (synchronized)
 
(5 intermediate revisions not shown)
Line 4: Line 4:
Java:  
Java:  
-
{{code|int a, b;
+
{{code|<nowiki>int a, b;
-
int i = (a + b) >>> 5;}}
+
int i = (a + b) >>> 5;</nowiki>}}
C#:
C#:
-
{{code|int a, b;
+
{{code|<nowiki>int a, b;
-
int i = (int)(((uint)(a + b)) >> 5)}}
+
int i = (int)(((uint)(a + b)) >> 5)</nowiki>}}
-
 
+
=== string.Substring===  
=== string.Substring===  
-
Java:
+
Java
{{code|someString.substring(int, int)}}
{{code|someString.substring(int, int)}}
 +
C#<br>
An extension method that resembles the Java version is available in GitSharp.Util:
An extension method that resembles the Java version is available in GitSharp.Util:
-
 
-
C#:
 
{{code|someString.Slice(int, int)}}
{{code|someString.Slice(int, int)}}
Line 27: Line 25:
Java:
Java:
-
{{code|int i = 0100644;}}
+
{{code|<nowiki>int i = 0100644;</nowiki>}}
C#
C#
-
{{code|int i = 33188;}}
+
{{code|<nowiki>int i = 33188;</nowiki>}}
===  synchronized===  
===  synchronized===  
Java:
Java:
-
{{code|synchronized void a_method() { ... }}}
+
{{code|<nowiki>synchronized void a_method() { ... }</nowiki>}}
C#
C#
-
{{code|[MethodImpl(MethodImplOptions.Synchronized)]
+
{{code|<nowiki>[MethodImpl(MethodImplOptions.Synchronized)]
-
public void a_method() { ... }}}
+
public void a_method() { ... }</nowiki>}}
-
When inlined, synchronized(this) is of course equivalent to lock(this)
+
When inlined, synchronized(this) is of course equivalent to the c# keyword lock(this)
===  Streams and Buffers===  
===  Streams and Buffers===  
Line 49: Line 47:
C#
C#
{{code|new BinaryWriter(new MemoryStream());}}
{{code|new BinaryWriter(new MemoryStream());}}
 +
 +
=== Integer.parseInt( string s, int radix) ===
 +
 +
This parses a string s using the base radix and converts it to decimal.
 +
 +
Java
 +
{{code|Integer.parseInt( "FF", 16)}}
 +
 +
C#
 +
{{code|NB.BaseToDecimal("FF", 16)}}
 +
 +
Note, NB is a class in the namespace GitSharp.Util.

Latest revision as of 15:33, 22 August 2009

This page lists some commonly required analogues between Java and C# that are not so trivial. This collection has been compiled while porting GitSharp.

Contents

Unsigned right shift operator (>>>)

Java:

int a, b; int i = (a + b) >>> 5;

C#:

int a, b; int i = (int)(((uint)(a + b)) >> 5)

string.Substring

Java

someString.substring(int, int)

C#
An extension method that resembles the Java version is available in GitSharp.Util:

someString.Slice(int, int)

Octal literals (i.e. 0001)

C# does not support octal literals (starting with a leading zero in java) and interprets them wrongly as decimal. You need to replace them by the corresponding decimal or hex literals.

Java:

int i = 0100644;

C#

int i = 33188;

synchronized

Java:

synchronized void a_method() { ... }

C#

[MethodImpl(MethodImplOptions.Synchronized)] public void a_method() { ... }

When inlined, synchronized(this) is of course equivalent to the c# keyword lock(this)

Streams and Buffers

Java

new ByteArrayOutputStream();

C#

new BinaryWriter(new MemoryStream());

Integer.parseInt( string s, int radix)

This parses a string s using the base radix and converts it to decimal.

Java

Integer.parseInt( "FF", 16)

C#

NB.BaseToDecimal("FF", 16)

Note, NB is a class in the namespace GitSharp.Util.