Browsing 7239 questions and answers with Jon Skeet

HashSet.IntersectWIth using a custom IEqualityComparer produces wrong results in Mono

I'm having in issue where a certain piece of code is running as expected in .NET 4.0 but not in Mono 2.6 (in Unity3D). Please have a look: void Test() { ...
Jon Skeet
people
quotationmark

I suspect that it's just a matter of MemberInfo.Equals not working as you expect. That's easy to test: var m1 = get("x"); var m2 = get("x"); Console.WriteLine(m1.Equals(m2)); If that prints False, then that's the issue, and you should... more 7/8/2014 1:05:12 PM

people

Accessing yield return collection

Is there any way to access the IEnumerable<T> collection being build up by yield return in a loop from within the method building the IEnumerable itself? Silly...
Jon Skeet
people
quotationmark

There is no collection being built up. The sequence that is returned is evaluated lazily, and unless the caller explicitly copies the data to another collection, it will be gone as soon as it's been fetched. If you want to ensure... more 7/8/2014 11:57:00 AM

people

How to tell JUnit to ignore nested exceptions?

If I have 2 classes as such: public class A { public A(boolean bool){ if(bool){ // currently broken // would throw RuntimeException ...
Jon Skeet
people
quotationmark

No - the result of calling the method is a RuntimeException, and that's what you're testing. How is JUnit meant to tell the difference between a RuntimeException from the exact method you're calling, and one from a dependent call? One of... more 7/8/2014 11:02:40 AM

people

Lambda expression to count distinct rows in a table

I'm trying to switch from Linq to lambda expression. I want the count of distinct rows in my table. Is there a better way to get it that the one below? var count = _db.myTable ...
Jon Skeet
people
quotationmark

I'm trying to switch from Linq to lambda expression. It's not clear what you mean by that, but the lambda form is still using LINQ - if you mean you had a query expression before, it's worth being aware that query expressions are just... more 7/8/2014 10:41:26 AM

people

Protect ArrayList from write access

Consider the following class: public class Cars extends Observable{ private ArrayList<String> carList = new ArrayList<String>(); public void...
Jon Skeet
people
quotationmark

You can return an unmodifiable view of it, changing the return type to List<String> instead of ArrayList<String>: public List<String> getCars() { return Collections.unmodifiableList(carList); } Note that as... more 7/8/2014 8:55:53 AM

people

Given a hash code, check if a Set contains an object

I have overriden equals and hashCode for a custom object Foo. Now, I have the hash code of an object and would like to check if the object is contained in the Set<Foo>...
Jon Skeet
people
quotationmark

You basically can't determine it reliably based on a hash code. You can check that a value definitely isn't in the set, but even if you find a matching hash code that doesn't mean that the value is in the set. This is rarely useful, which... more 7/8/2014 7:22:18 AM

people

Read dynamic json property values using JObject c#

Part of my Json structre is dynacmic array and i want to read it using Json.Net . The json structre is { "objects": [{ "id": "521daea47288c7794c88c923", "name": "Rass", ...
Jon Skeet
people
quotationmark

It's hard to know exactly what you need based on the somewhat vague requirements, but here's some code which will get you a Dictionary<string, JToken> of the preferences: JArray preferences =... more 7/8/2014 6:23:24 AM

people

Deserializing json in c# for the nest api

I am trying trying to Deserialize the Devices json structure below with c#, newtonsoft, and .net 3.5, put have been unable to produce code that works as the "qxdddh" and "q0tL6au"...
Jon Skeet
people
quotationmark

Just change your Devices class to use a Dictionary<,> instead of an array: public class Devices { public Dictionary<string, TstatDetails> thermostats { get; set; } } JSON.NET will interpret each property of the... more 7/8/2014 6:14:53 AM

people

Java Computing Time Difference Not Working

I am trying to compute the difference in time between two date/times. This is what I have imported: import java.util.concurrent.TimeUnit; import java.text.*; import...
Jon Skeet
people
quotationmark

That looks correct to me... you've got just under two months, so 60.5 days (because it starts at ~8pm and ends at ~8am). A day contains 1440 minutes. 1440 * 60.5 is 87120, which is very close to the answer you've received. It's not clear... more 7/7/2014 8:56:15 PM

people

JPEG bytes encryption returns bad image when the file is decrypted

I'm working on a simple code for encrypting the pixels of a JPEG image file. The code works well for encryption, generating a pseudo random image but when I try to decrypt it...
Jon Skeet
people
quotationmark

You're encrypting, but then saving the result as a JPEG. That's a lossy format - after loading it again, you'll end up with an image which is visually similar to the original, but may well have some different pixels... which means it won't... more 7/7/2014 7:59:09 PM

people