Browsing 7239 questions and answers with Jon Skeet

C# byte type and literals

The following code works. byte b = 1; But I noticed the following code doesn't work byte b = BooleanProperty ? 2 : 3; // error The compiler says Cannot convert source...
Jon Skeet
people
quotationmark

There's an implicit conversion from int constants (not just literals, but any compile-time constant expression of type int) to byte, so long as the value is in range. This is from section 6.1.9 of the C# 5 specification: An implicit... more 7/4/2015 9:21:11 PM

people

Adding a node to beginning of a list in Java?

I'm learning linked list basics via Sedgewick's Algorithms and came across adding nodes to the beginning of a linked list. I've noticed they're redefining the "first node" using...
Jon Skeet
people
quotationmark

You couldn't have Node firstNode = new Node(); later on in the code - because that would be trying to declare a new local variable with the same name as an existing one. (You can have a local variable with the same name as an instance... more 7/4/2015 7:43:01 PM

people

Parsing a Single Integer with Json Net

I am trying to parse a simple long integer with Json.Net. This is what the response looks like: header: { Transfer-Encoding: chunked Date: Fri, 03 Jul 2015 16:15:33 GMT ...
Jon Skeet
people
quotationmark

If the service just returns "1435775316000" then that isn't really JSON - or at least, it's not a JSON object, which is at least a rather more widely-used approach to returning JSON. You can just parse it with long.Parse: long value =... more 7/4/2015 3:38:04 PM

people

Why does my array contain integers I never added? C#

int gameTurns = 12; do { gameTurns -= 1; Console.WriteLine(" You have " + (gameTurns) + " attempts left."); ...
Jon Skeet
people
quotationmark

Yes, because 49 is the UTF-16 code unit for the character '1'. If you entered "ABCD", it would show 64, 65, 66, 67. Convert.ToInt16(char) is documented as: Converts the value of the specified Unicode character to the equivalent... more 7/3/2015 6:44:30 PM

people

Java setting system property using command line

While reading java man page, I found the -Dproperty=value flag which stats that by passing this flag, it will create a system property with value = value. I wrote a test java...
Jon Skeet
people
quotationmark

The -D needs to come before the class name, because anything after the class name is regarded as an argument to the Java app itself... and the Java app may choose to do anything it likes with -D, or any other JVM options. Basically, the... more 7/3/2015 4:35:36 PM

people

Multiple fields in GroupBy clause in LINQ (where one is computed field)

I have a LINQ query upon which I need to add two fields as group by clauses. While I can easily group by with as many column fields but the problem is occurring when one of the...
Jon Skeet
people
quotationmark

So just include it as you have in the commented-out code: .GroupBy(r => new { Rate = ((int)(r.Rate / BucketSize)) * BucketSize, r.ExpiryDate }) more 7/3/2015 10:11:38 AM

people

How to format a number in Java and also keep the decimals?

I'm trying to show milliseconds as seconds while also keeping the decimals e.g. I have 1234 milliseconds and I want to show this as 1.234 seconds. Decimal duration =...
Jon Skeet
people
quotationmark

It sounds like you should be using BigDecimal - create a BigDecimal from the long, and then scale it by 3 places: BigDecimal bd = new BigDecimal(duration).scaleByPowerOfTen(-3); String durationStr = formatter.format(bd); By using... more 7/3/2015 8:13:11 AM

people

How do I parse this JSON Result as an object?

I need to parse this JSON String to my object of type "WeatherJson". However I do not know how to parse the arrays inside the string such as...
Jon Skeet
people
quotationmark

You should just make the arrays in the JSON match with list or array types in your POCO. Here's a short but complete example using the JSON you've provided: using System; using System.Collections.Generic; using System.IO; using... more 7/3/2015 6:24:07 AM

people

Is there a default value for the object datatype?

I have method overload issue, which I've asked about in a previous posting. After some feedback and research I'm convinced that I need to default some of the values being passed...
Jon Skeet
people
quotationmark

The only value you can use for the default value of an optional object parameter is null - or equivalently, default(object). There are no other compile-time constants of type object. I'm slightly surprised that you can't use a string... more 7/2/2015 9:41:31 PM

people

C# How to pass a Linq ToList()

I am a newbie at LINQ and am having trouble passing a query result into a method Here is the code: using (var ctx = new AH_ODS_DBEntities1()) { var orlist =...
Jon Skeet
people
quotationmark

You're creating a list of instances of an anonymous type with three properties (AmountPaid, ApplicationDate, and Type). Your method expects a List<Collection>. Assuming that Collection is a type with the same properties, you probably... more 7/2/2015 7:36:39 PM

people