开发者

Converting Java to C# issue

I am trying to convert a statsd Java client into a usable C# class. I used visual stuido 2005's convert feature on the java client and it has come up with a few errors no problem. I am not used to java and do not know how to conv开发者_开发百科ert a few of my errors into c#. For instance on of the errors in a public bool class contains (String...keys) What does that mean in java? I am not familiar with '...'. Another in a for statement contains (String stat: stats) I would assume that means String stat || stats but I am not sure.


This Java:

public void foo(int x, String... keys)

is broadly equivalent to the C#

public void Foo(int x, params string[] keys)

In Java it's called a varargs parameter; in C# it's called a parameter array.

In both cases they allow the caller to pass multiple arguments, and the compiler packages them up into an array... so this call:

Foo(5, "x", "y", "z");

is equivalent to

Foo(5, new string[] { "x", "y", "z" });

... but a bit simpler to read.

The for (String stat : stats) is the enhanced for loop (or just "for-each loop") in Java, and is broadly equivalent to a foreach loop in C#:

foreach (string stat in stats)


I guess you will have to look up each piece of syntax you don't understand individually and then post specifically which ones you need help with. Specifically, for the two instances you have mentioned here:

String... keys is how you use variable number of arguments in Java. (http://www.deitel.com/articles/java_tutorials/20060106/VariableLengthArgumentLists.html)

String stat: stats is simply the foreach loop in Java (http://leepoint.net/notes-java/flow/loops/foreach.html)

This page can be a handy reference for you: http://www.harding.edu/fmccown/java_csharp_comparison.html


It's been ages since i've used Java, but i believe

for (String stat: stats)

is Java syntax for a for-each statement. The C#-equivalent would be

foreach (String stat in stats)

I do not recognize 'String...keys'. Could you post some actual code?


The ... notation is a variable length parameter list identifier. See http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

It's basically equivalent to C#'s params object[] objects style of variable length argument lists.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜