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 convert 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.
4 Answers
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)
Comments
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
Comments
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.