GitSharp/Non Trivial Java To CSharp Conversions

From eqqon

(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 (Unsigned right shift operator (>>>))
Line 4: Line 4:
Java:  
Java:  
-
{{code|int a, b;
+
{{code|int a, b;<br>int i = (a + b) >>> 5;}}
-
int i = (a + b) >>> 5;}}
+
C#:
C#:
-
{{code|int a, b;
+
{{code|int a, b;<br>int i = (int)(((uint)(a + b)) >> 5)}}
-
int i = (int)(((uint)(a + b)) >> 5)}}
+
-
 
+
=== string.Substring===  
=== string.Substring===  

Revision as of 15:22, 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:

{{{1}}}

C#:

{{{1}}}

string.Substring

Java:

someString.substring(int, int)

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

C#:

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:

{{{1}}}

C#

{{{1}}}

synchronized

Java:

synchronized void a_method() { ...
}

C#

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

When inlined, synchronized(this) is of course equivalent to lock(this)

Streams and Buffers

Java

new ByteArrayOutputStream();

C#

new BinaryWriter(new MemoryStream());