Browsing 7239 questions and answers with Jon Skeet

Why awaiting cold Task does not throw

I was just experimenting to see what happens when a cold task (i.e. a Task which hasn't been started) is awaited. To my surprise the code just hung forever and "Finsihed" is never...
Jon Skeet
people
quotationmark

You're trying to start the task returned by the async method - that isn't the cold task that you started out with. Indeed, if you add some diagnostics to your Task.Run call, you'll see that an exception is thrown: ... more 6/16/2014 5:51:26 AM

people

Can signed bytes be read from a stream?

I'm trying to read bytes sent by a java servlet into a C# application, so far, I haven't been able to get anything more than gibberish from the servlet using normal streams in C#....
Jon Skeet
people
quotationmark

Two problems: You're using the platform-default encoding in both C# and Java. That's almost always a bad choice. Specify the encoding explicitly in Java - UTF-8 is usually a good choice. (That's also the default for most .NET... more 6/15/2014 7:59:06 PM

people

Random.range error CS0029 using floats

I'm creating a script to randomly spawn a ''zone'' into the map in which the player has to stand in to generate points. I've got most of the point system and zone system in place...
Jon Skeet
people
quotationmark

You haven't shown where you've declared random, but I suspect you've used: Random random; whereas you actually want the type of random to be float: float random; ... random = Random.Range(0f, 5f); Alternatively, declare the variable... more 6/15/2014 1:34:43 PM

people

How to unshuffle a list?

I am using Collections.shuffle(list); to shuffle a list but I don't know how to unshuffle it?I was thinking of saving the list before shuffling and then shuffle it so that a...
Jon Skeet
people
quotationmark

There's no such concept of unshuffling - after you've shuffled a deck of cards, how would you get back to the previous state, for example? If your original collection is ordered in some definitive way, just sort it again. Otherwise (e.g.... more 6/14/2014 5:01:35 PM

people

Java ArrayList avoiding IndexOutOfBoundsException

i got a short question. ArrayList<T> x = (1,2,3,5) int index = 6 if (x.get(6) == null) { return 0; } Exception in thread "main" java.lang.IndexOutOfBoundsException:...
Jon Skeet
people
quotationmark

Just use the size of the list (it's not an array): if (x.size() <= index || x.get(index) == null) { ... } Or if you want to detect a valid index with a non-null value, you'd use: if (index < x.size() && x.get(index)... more 6/14/2014 4:27:37 PM

people

JSON Unicode escape sequence lowercase or not?

I was reading RFC 4627 and I can't figure out if the following is valid JSON or not. Consider this minimalistic JSON text: ["\u005c"] The problem is the lowercase c. According...
Jon Skeet
people
quotationmark

I think it's explained by this part of RFC 4234: ABNF strings are case-insensitive and the character set for these strings is us-ascii. Hence: rulename = "abc" and: rulename = "aBc" will match "abc", "Abc",... more 6/13/2014 10:11:50 PM

people

How to sort XML elements in parent element?

I need to change the order of elements in one parent element. So it is easy to get sorted elements. myXML.Elements("Issue").OrderBy(x =>...
Jon Skeet
people
quotationmark

It sounds like you just want: // No need to specify "Issue" if that's all that's in myXML. Prefer explicit // attribute conversion over "manual" parsing var orderedIssues = myXML.Elements().OrderBy(x => (int)... more 6/13/2014 2:25:01 PM

people

java adding cdata to xml string

I need to add CDATA to xml string for sign it with certificate. String looks like: <SignedContent>someparametres</SignedContent> Result must be...
Jon Skeet
people
quotationmark

It sounds like you just want: Node cdata = doc.createCDATASection(text); parentElement.appendChild(cdata); more 6/13/2014 2:23:03 PM

people

How do I Convert [AnonymousType#1] or pre define it?

I get this Error Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<object>' Running 3.5 .net Never had this issue...
Jon Skeet
people
quotationmark

Three independent options: Ditch the anonymous type entirely. You've only got a single value, so why not use select p.PRESSRELEASEID? Cast in the select clause: select (object) new { ITEMID = p.PRESSRELEASEID } Declare the variable... more 6/13/2014 1:48:09 PM

people

String constructor

We can say, string myString = "Hello"; Which 'magically' constructs a new string object holding that value. Why can't a similar 'construction-less' approach be used for...
Jon Skeet
people
quotationmark

Basically, it's part of the C# language specification: there's syntax for string literals, numeric literals, character literals and Boolean literals, but that's all. The compiler uses these literals to generate IL, and for most of them,... more 6/13/2014 1:30:35 PM

people