Browsing 7239 questions and answers with Jon Skeet

Is it safe to call the same method several times asynchronously in C# 5

I would be grateful if you could let me know opinion about calling asynchronously the same method several times. I more interested in know the safety side of this activities. I...
Jon Skeet
people
quotationmark

It depends on the method - but it's certainly not inherently unsafe. There's nothing within the state machine generated by the compiler that introduces problems. However, if your asynchronous method uses shared state, then the normal... more 9/18/2013 3:41:34 PM

people

Extend Array Class with Append method (this ref)

I'd like to extend System.Array class to do something like that: public static class Extensions { public static void Append( this byte[] dst, byte[] src) { ...
Jon Skeet
people
quotationmark

If you mean "Can I make the first parameter of an extension method a ref parameter?" then the answer is no, you can't. (Not in C#, anyway. IIRC, you can in VB - but I'd advise against it.) From section 10.6.9 of the C# spec: The first... more 9/18/2013 2:36:39 PM

people

assertion fails Check a field is greater than

Im trying to use a assert stmt to check if a value is greater than 1 but its not working as expected. public class asserttest { static void methoda(int i){ assert (i...
Jon Skeet
people
quotationmark

My guess is that you're getting confused by assertions not being enabled by default. Use the -enableassertions command line option: java -enableassertions asserttest You can also limit assertions to specific packages, and specify... more 9/18/2013 2:25:00 PM

people

DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff") resulted in something like "09/14/2013 07.20.31.371"

I have a WP8 app, which will send the current time to a web service. I get the datetime string by calling DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff") For most users it works...
Jon Skeet
people
quotationmark

Is it because some culture format issue? Yes. Your user must be in a culture where the time separator is a dot. Both ":" and "/" are interpreted in a culture-sensitive way in custom date and time formats. How can I make sure the... more 9/18/2013 1:55:52 PM

people

Byte Array to String in C#

As Im new to C# and I am stuck with a very basic problem. I am reading one text file, which contains some data(for example "hello") I am reading this data like the code mentioned...
Jon Skeet
people
quotationmark

You should use an Encoding object to specify which encoding you want to use to convert the binary data into text. It's not clear from your post what the input file actually is, or whether you'll know the encoding in advance - but it's much... more 9/18/2013 1:46:50 PM

people

Need to manually synchronize the Synchronized list while iteration when it could be avoided?

My question is about synchronizedList method Collections Class. Javadocs say: It is imperative that the user manually synchronize on the returned list when iterating over...
Jon Skeet
people
quotationmark

I think iterator method could have also handled synchronization in the same fashion as above method No, it absolutely couldn't. The iterator has no control over what your code does between calls to the individual methods on it.... more 9/18/2013 1:29:41 PM

people

write data into cache instead of disk on socket

I know "fileReader" and "fileWriter" can read and write files from the disk transfer between the socket. However, I want to receive file from socket and write them to cache...
Jon Skeet
people
quotationmark

Assuming you're talking about keeping data in memory, you can use ByteArrayOutputStream and ByteArrayInputStream for in-memory IO. You can then wrap these in an OutputStreamWriter and InputStreamReader respectively, for text-based... more 9/18/2013 1:23:02 PM

people

C# interface method declaration cannot execution implemention

I have a concrete class implementation of an interface that has a method declaration like so.... public interface IControllerContent { IEnumerable<KeyValuePair<string,...
Jon Skeet
people
quotationmark

Simply put, your implementation isn't returning a Dictionary<string, string>, so you can't cast to it. You're just returning a sequence of key/value pairs - that's not the same thing as a dictionary. Note that this has nothing to do... more 9/18/2013 11:19:45 AM

people

java.lang.UnsupportedOperationException shown when trying to Assert some values

I am using the below code to assert text in my test script. But its giving UnsupportedOperationException error every time it hits this code. public static void...
Jon Skeet
people
quotationmark

This is the problem: List verificationFailures = getVerificationFailures(); verificationFailuresMap.put(Reporter.getCurrentTestResult(), verificationFailures); verificationFailures.add(e); You're calling List.add on the result of... more 9/18/2013 6:24:09 AM

people

Find time zone and time from given string of timestamp

I need to parse a timestamp and get both the time and the timezone from a string like the following, Timestamp = "2013-09-17T14:55:00.355-08:00" From the above string, I should...
Jon Skeet
people
quotationmark

You can get a DateTimeOffset which contains both the local time and the offset from UTC, using DateTimeOffset.ParseExact. However, there may be multiple time zones observing the same offset from UTC at that time, so you can't get the... more 9/17/2013 7:29:44 PM

people