how can i turn this java method to c#

  • Thread starter Thread starter Guru
  • Start date Start date
G

Guru

public String generateSP()
{
String s = "";
Iterator iterator = hmTable.keySet().iterator();
String s1 = "";
while(iterator.hasNext())
{
String s2 = (String)iterator.next();
s = s.length() != 0 ? s + "," + s2 : s2;
}
return s;
}


thx!
 
The var s1 appears never used?
C#:
public String generateSP()
{
String s = null;
foreach (String s2 in hmTable.Keys)
{
s = (s != null) ? (s + "," + s2) : s2;
}
return s;
}

Due to some error, I have to post twice. Sorry if it this reply appears
twice.

Regards,
Thi
 
Thx Truong Hong Thi

another ques.
dose c# has some type like java type Vector (java.util.Vector)
?

:)
 
C# System.Collections.ArrayList provide similar functions, but is is
not thread-safe by default. To have thread-safe array list like
java.util.Vector, use ArrayList.Synchronized method:

ArrayList theVector = ArrayList.Synchronized(new ArrayList);

Hope that helps,
Thi
 
Truong said:
C# System.Collections.ArrayList provide similar functions, but is is
not thread-safe by default. To have thread-safe array list like
java.util.Vector, use ArrayList.Synchronized method:

ArrayList theVector = ArrayList.Synchronized(new ArrayList);

However, that model of thread-safety is pretty shallow anyway - you
can't safely iterate through a synchronized ArrayList (or Vector in
Java) without locking for the whole loop rather than just each bit of
the loop.

Personally I usually avoid the synchronized wrappers, explicitly
locking the SyncRoot property (or a private lock) if a collection needs
to be shared between threads.

Jon
 
Back
Top