Browsing 7239 questions and answers with Jon Skeet

Getting Percentage of TypeOf Item in a List with LINQ

I want to be able to get the percentages of certain types of things in a list. Say I have a list of Animals, but inside that list I have Cats, Dogs, And Snakes:...
Jon Skeet
people
quotationmark

No, I don't think you need to be using OfType<> at all. I suspect you really want GroupBy: var countsByType = animals.GroupBy(x => x.GetType(), (t, g) => new { Type = t, Count = g.Count()... more 2/10/2015 9:42:16 AM

people

Why a new InputStream will still read what is left over from an old InputStream?

Well please see this question and Jon Skeet's answer first. This time I have this server: public class SimpleServer { public static void main(String[] args) throws Exception...
Jon Skeet
people
quotationmark

Your client has been set up to only send 5 characters at a time, and then flush - so even though the InputStreamReader probably asked for more data than that, it received less, and then found that it could satisfy your request for 5... more 2/9/2015 9:49:59 PM

people

How to read JSON into a JObject when the string contains double quotes ?

I have a piece of JSON that I want to get into a string for C# to use. The problem is when I escape all the double quotes it seems no longer valid. For example: string...
Jon Skeet
people
quotationmark

You're not actually escaping any of the double quotes, as far as the JSON is concerned - the string doesn't contain any backslashes. You can confirm that with Console.WriteLine(jsonString);. The problem is that you've currently got an... more 2/9/2015 9:42:41 PM

people

Why a new InputStreamReader won't read the remaining characters in the console?

So I have a very simple server written in Java: public class SimpleServer { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new...
Jon Skeet
people
quotationmark

Why is the information in the terminal lost when a new InputStreamReader object is created? When you call read() on the InputStreamReader, it's allowed to (and often will) read more data from the stream than you've actually requested,... more 2/9/2015 9:26:19 PM

people

How to print all set bits of a negative integer

while (n != 0) { System.out.print(n & 1); n = n >> 1; } The above code in Java leads to an infinite loop, if n = -1. How do I get it to print negative...
Jon Skeet
people
quotationmark

The >> operator is a "sign-extended" right-shift operator, which means that if the top bit in the original value is set, the bits that are "shifted in" are 1. That keeps the sign of the result the same, basically. You want the... more 2/9/2015 9:07:38 PM

people

JSONObject.toString Type mismatch cannot convert from JSONString to JSONObject

Per the API we should be able to do this. http://www.json.org/javadoc/org/json/JSONObject.html#toString() @Override public JSONObject buildPayload(BuildData buildData,...
Jon Skeet
people
quotationmark

Your method declares that it returns a value of type JSONObject. But this: return payload.toString(); returns a value of type String. There's no implicit conversion from String to JSONObject, hence the compile-time error. If you really... more 2/9/2015 5:52:12 PM

people

IllegalArgumentException : Comparison method violates it's general contract

Following is my Comparator : class CostComparator implements Comparator<Index> { @Override public int compare(Index itemIndex1, Index itemIndex2) { return...
Jon Skeet
people
quotationmark

Simply put, it violates the comparison if two indexes have the same cost. It should return 0, but it will return -1. As a trivial violation, that means that compare(index, index) will always return -1, when it must return 0. It's really... more 2/9/2015 5:38:10 PM

people

Immutable version of EnumSet

I can't find an Immutable version of EnumSet. Two questions: Can I use Enums in a normal Guava ImmutableSet? If I can, what are some benefits/drawbacks of using an ImmutableSet...
Jon Skeet
people
quotationmark

Are you looking for Sets.immutableEnumSet (Guava) perhaps? Returns an immutable set instance containing the given enum elements. Internally, the returned set will be backed by an EnumSet. The iteration order of the returned set... more 2/9/2015 3:53:27 PM

people

Expression bodied function members efficiency and performance in C# 6.0

In a new C# 6.0 we can define methods and properties using lambda expressions. For instance this property public string Name { get { return First + " " + Last; } } can be now...
Jon Skeet
people
quotationmark

In a new C# 6.0 we can define methods and properties using lambda expressions. No, you can't. You can define method and property bodies using syntax which looks like a lambda expression, in that it uses the token =>. However,... more 2/9/2015 1:58:15 PM

people

EventInfo access modifiers

I am stuck now at method to retrieve access modifiers of EventInfo object (reflected event field in C# .NET). By access modifiers I mean: public/private/protected/internal and...
Jon Skeet
people
quotationmark

In theory (at the IL level) there are basically three members making up the event: add remove raise You can access each of those via a separate EventInfo property (AddMethod, RemoveMethod, RaiseMethod) and check the access modifier for... more 2/9/2015 12:59:46 PM

people