Browsing 7239 questions and answers with Jon Skeet

Unexpected behavior with ArrayList.remove()

I have a pizza code that iterates through a list of objects and checks whether they are colliding or not. If one is collided with, it is removed from the ArrayList. for (int i =...
Jon Skeet
people
quotationmark

I suspect the problem is that you're skipping an element after one call to remove, because you're incrementing i and everything in the list is moving up one element. (It's not really clear what the symptoms are at the moment. If this turns... more 2/21/2014 9:55:22 AM

people

What is the difference between Default and [Optional] Parameters?

What's the difference between Method(int arg0 = 0) vs Method([Optional] int arg0 = 0); whenever I'm trying to invoke this method compiler says its ambiguous situation. I know...
Jon Skeet
people
quotationmark

The OptionalAttribute is automatically applied by the compiler when you specify an optional parameter, basically. (It's a bit like ExtensionAttribute being supplied for extension methods.) In IL, it doesn't appear like other attributes -... more 2/21/2014 9:44:25 AM

people

NodaTime Interval JSON Serialization

Is there a reason why the NodaTime JSON.net serializer does not use the ISO8601 Time Interval format to express the start and end instants? Example ISO8601 Time...
Jon Skeet
people
quotationmark

Is there a reason why the NodaTime JSON.net serializer does not use the ISO8601 Time Interval format to express the start and end instants? Yes. I didn't spot it when reading ISO-8601. It's not a very good reason, but it's the correct... more 2/21/2014 9:23:22 AM

people

Why ( Math.Pow(2, x) & (2^y + 2^z + 2^i) ) is always return 0

Like the title, I don't understand why that expression is always true? Here is the detail: // x, y, z, t is always different with each other int x = 1; int y = 2; int z =...
Jon Skeet
people
quotationmark

Think of x, y, z and t as bit positions - you're doing a bitwise & which will clear any bits which aren't set in both operands, and 2t will only have the tth bit set. So if x, y and z are all different, 2x + 2y + 2z will have bits x,... more 2/21/2014 6:49:08 AM

people

Yield return and dynamic

Is possible to invoke a "IEnumerable/yield return" method when using dynamic? I'm asking this because I'm getting the error below when I call the "Test(States1.GetNames())"...
Jon Skeet
people
quotationmark

The problem is that the iterator block implementation uses explicit interface implementation to implement IEnumerable<T>... and explicit interface implementation doesn't play nicely with dynamic typing in general. (You don't need to... more 2/20/2014 6:07:09 PM

people

Why am I getting two different 'formats' of hex in my bytes while evaluating an HMAC?

I'm getting a signed payload from an authentication source that comes in a base64 encoded and URL encoded format. I'm getting confused somewhere while evaluating, and ending up...
Jon Skeet
people
quotationmark

The good news is that the hash computation appears to be working. The bad news is that you're receiving the hash in a brain-dead fashion. For some reason it seems that the authors decided it was a good idea to: Compute the hash... more 2/20/2014 5:57:10 PM

people

Use async/await on synchronous operations (mainly to stay on UI thread)

I'm trying to run some stuff asynchronous. Here's my code: private async void SearchButton_Click(object sender, EventArgs e) { await Search(); } //...
Jon Skeet
people
quotationmark

Is it illegal/bad practice to await on synchronous methods like Search()? Well it's a very bad idea, because your UI will hang, basically. You should change your Search method to SearchAsync (for the sake of convention) and use... more 2/20/2014 2:04:28 PM

people

Why can a "private" method be accessed from a different instance?

Although, this is a very basic code, it seems there is something fundamentally flawed in Java, or the JVM used by the Eclipse IDE I have used to run the code. The code runs even...
Jon Skeet
people
quotationmark

Yes, the main method belongs to class A, but it is not accessing the private method from inside the current object in the context of the "this" reference. That doesn't matter. That's just not the model of accessibility that Java... more 2/20/2014 7:09:16 AM

people

Passing object in Dlist.

The line "if (l.head.item != 9" gave me the error it said something like object is not compatible with int. I am really confused on why is that? How to fix it? /DListNode1/ /*...
Jon Skeet
people
quotationmark

As others have pointed out, the problem is that the type of l.head.item is Object, and you can't compare that with an int using != or ==. Options: Cast l.head.item to Integer or int: // This could be done in one step if you wanted int... more 2/20/2014 6:56:49 AM

people

Unpredictable JSON structure

I am receiving a JSON structure that is somewhat unpredictable from a third party API. For example, I started with a class like this: public Class UserTuple { public int uid...
Jon Skeet
people
quotationmark

If you don't care about it, it seems like you just need to set JsonSerializerSettings.MissingMemberHandling appropriately: var settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore }; var tuples... more 2/19/2014 10:35:27 PM

people