Browsing 7239 questions and answers with Jon Skeet

Cannot return supertype in generic method

Some example code: public class Main { class SomeType { } class A { protected <T extends SomeType> T createSomething() { return...
Jon Skeet
people
quotationmark

Thanks to <T extends SomeType>, we can be sure that T is always a subtype of SomeType, right? Right. So why can't I just return the supertype SomeType? Because it might not be a T! Put it this way - imagine SomeType is... more 8/9/2014 7:27:08 AM

people

Static/Instance methods and extension questions

I'm new to C# and I began working on a project that needed a method added to a class in C#. I found myself re examining the differences between static and instance methods and...
Jon Skeet
people
quotationmark

Your two methods are extension methods, which are meant to look like instance methods when they're called. They can be called statically, but you need to supply the instance as the first argument, and specify the class which declares the... more 8/8/2014 9:09:30 PM

people

Is is allowed to have an Enum member name that start with a number?

I am trying to set up an enum to hold sheet metal gauges (thicknesses). I thought it would be most intuitive if I could write: using System.ComponentModel; public enum Gauge { ...
Jon Skeet
people
quotationmark

No. Enum members have to be valid identifiers. From section 14.3 of the C# 5 specification: The body of an enum type declaration defines zero or more enum members, which are the named constants of the enum type. No two enum members can... more 8/8/2014 8:28:56 PM

people

Error in creating BIN File using Binary File C#

I am creating a Binary File in C# here using the following code below. It is relatively simple code below. The RawData variable is a type string[]. using (BinaryWriter...
Jon Skeet
people
quotationmark

You're using BinaryWriter.Write(int). So yes, it's writing 4 bytes, as documented. If you only want to write one byte, you should use BinaryWriter.Write(byte) instead. So you probably just want: bin.Write(Convert.ToByte(data,... more 8/8/2014 3:22:59 PM

people

Can't generate NodaZoneData file using NodaTime.TzdbCompiler

When I attempt to generate a NodaZoneData file using the NodaTime.TzdbCompiler and the latest IANA tzdb download, I receive the following output: Starting compilation of...
Jon Skeet
people
quotationmark

Yes, this is due to zone1970.tab only being introduced in 2014f, and the released TzdbCompiler not knowing about it. Delete the file, and you should be fine - or you can just use the nzd file we've already put up on the web site. If... more 8/8/2014 2:20:50 PM

people

Primitive double equals to NaN

Can someone explain how does it possible that primitive double type equals to NaN after following computations. Can you tell me some cases when double could end up as Nan?...
Jon Skeet
people
quotationmark

Assuming your loop is meant to use x.size(), then it's pretty simple - if x.size() is 0, you'll be computing 0/0, which is NaN. Otherwise, it could be that logicWhichReturnsPrimitiveDouble() returns a NaN for whatever reason. It's not... more 8/8/2014 2:10:46 PM

people

Convert Timestamp to date and hour Java

Hi i'm getting a dates and hours in timestamp format from a webservice and i'm trying to convert them in String to show them in my app i've tried Timestamp ts = new...
Jon Skeet
people
quotationmark

It sounds like your timestamp format is probably in seconds since the Unix epoch, instead of the milliseconds that Java expects - so just multiply it by 1000: Date date = new Date(jsonObject.getLong("release_date") * 1000L); You should... more 8/8/2014 1:21:48 PM

people

Looking for an elegant LINQ query, alternative to SelectMany

This is a simple question but I'm struggling to find an elegant solution. Let's say I have a simple data structure, the list of groups of items. Each group has a unique ID and...
Jon Skeet
people
quotationmark

Currently your first option doesn't check for the group ID, but that's easily fixed: return groups.Where(g => g.GroupId == groupId) .SelectMany(g => g.Items) .FirstOrDefault(i => i.ItemId ==... more 8/8/2014 12:27:12 PM

people

Select from IEnumerable using LINQ

I have a method that returns an object from cache which I use to populate selectlists within the system. var r = CacheHelper.GetCacheItem(...); return new...
Jon Skeet
people
quotationmark

Ideally, you should keep the type information all the way through the system - and potentially create a named type with the relevant properties. You could then cast back to IEnumerable<SomeConcreteType> later. If you can't do that... more 8/8/2014 12:18:28 PM

people

BigDecimal to String in java?

My code is: x=new BigDecimal(x.toString()+" "+(new BigDecimal(10).pow(1200)).toString()); I am trying to concat two bigdecimals after converting them to string and then convert...
Jon Skeet
people
quotationmark

It's failing to parse because you've got a space in there for no particular reason. Look at the string it's trying to parse, and you'll see it's not a valid number. If you're just trying to add extra trailing zeroes (your question is very... more 8/8/2014 12:01:43 PM

people