1

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.

1
  • 1
    I think we need more detail. Can you give the full text of some of the errors? Perhaps even a bit of the code it is gagging on? Commented Mar 24, 2011 at 20:22

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

1

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?

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.