Browsing 7239 questions and answers with Jon Skeet

using float + Lambda Expressions C#

var distances = new Dictionary<char, float>(); var nodes = new List<char>(); I have this line to find the smallest distance nodes.Sort((x, y) => distances[x] -...
Jon Skeet
people
quotationmark

You can't convert your lambda expression into a Comparison<char> (which is what you want) because it returns a float - you've effectively got a Func<char, char, float> there, whereas Comparison<char> is closer to... more 1/22/2015 2:44:43 PM

people

Java 8 How do I get a java.sql.Timestamp that is 24 hours ago?

How do I obtain a java.sql.Timestamp that represents 24 hours ago? I'm using JDK 8 via Scala.
Jon Skeet
people
quotationmark

If you're using Java 1.8, it's as simple as: Instant instant = Instant.now().minus(24, ChronoUnit.HOURS); Timestamp timestamp = Timestamp.from(instant); You can use the overload of now() accepting a Clock to be more test-friendly (so... more 1/22/2015 1:41:50 PM

people

How to name dictionary/list with variable

I get fed JSON data and I need to create a varying amount of dictionaries to store events in. I can't seem to figure out or find and answer to something like this: Creating a...
Jon Skeet
people
quotationmark

You don't. Variable names in C# are never dynamic - it sounds like you want a map from ID to "dictionary of timestamp to item". In fact, I would probably create a separate type of ItemEvents or something similar, which contained all the... more 1/22/2015 11:27:15 AM

people

If a derived class' "reference" goes out of scope but the base class reference stays, does the derived object ever change?

(In C# 5.0) It's a pretty simple yes/no question which I can't seem to find an explicit answer for which probably means I'm looking in the wrong place or using the wrong terms in...
Jon Skeet
people
quotationmark

It's a reference to the same object. The compile-time type of the expression which has a value of that reference is irrelevant. It's important to understand that there really is only one object - it's not like there's a base class object... more 1/22/2015 10:21:57 AM

people

Arithmetic overflow exception in .net 2.0 and greater

I'm getting this error System.OverflowException: Arithmetic operation resulted in an overflow. when i ran my application on Windows Server 2008 R2 Standard compiled in .Net 2.0...
Jon Skeet
people
quotationmark

I suspect you'll find the problem is that with .NET 1.1 you're running the x86 CLR in both cases, whereas with .NET 2.0 you're probably running with x86 on your desktop an x64 on your server. IntPtr.ToInt32 is documented to throw... more 1/21/2015 9:17:07 PM

people

Json.Net Changing from JObject loops to JArray

I'm pulling a JSON feed that looks like this. { "bank": [ { "id": 35, "name": "bank 1", "endpoints": [ { ...
Jon Skeet
people
quotationmark

There's no need to explicitly loop or change how you're parsing - you can just use LINQ with the JSON: using System; using System.IO; using System.Linq; using Newtonsoft.Json.Linq; class Test { static void Main(string[] args) { ... more 1/21/2015 7:12:26 PM

people

Is .NET DateTime truncating my seconds?

I am trying to format some precise dates, converting them from a Unix timestamp to a DateTime object. I noticed that the AddSeconds method has an overload that accepts a floating...
Jon Skeet
people
quotationmark

From the documentation of DateTime.AddSeconds: The value parameter is rounded to the nearest millisecond. An alternative would be to multiply by TimeSpan.TicksPerSecond and then add that to the ticks of UnixEpoch: return new... more 1/21/2015 4:14:56 PM

people

SortedSet<T> need to use different sort and equality criteria

I have a class that was previously being used in a HashSet. This has now been changed so that the class is now used in a SortedSet, but the equality test no longer works as it...
Jon Skeet
people
quotationmark

If you mean you need to be able to handle multiple values with different IDs but the same DateTime, you could include that in your CompareTo implementation: // TODO: Implement IComparable<DatedID> as well :) int... more 1/21/2015 4:09:49 PM

people

what is the internal working of System.idendityHashCode() and hashCode() method?

I read About System.identityHashCode(Object x). You can not override it because its static method but i can override Object's hashCode method. and also this is mentioned for...
Jon Skeet
people
quotationmark

but when i am running below code by swapping the object in println method i am getting same result. You shouldn't compare the results of a hashCode in one run with the results in a different run. For example, what you may have... more 1/21/2015 1:51:22 PM

people

Adding two DateTime objects together

Is there any better way to add one DateTime object to another one, than this: DateTime first = new DateTime(2000, 1, 1); DateTime second = new DateTime(11, 2, 5, 10, 10,...
Jon Skeet
people
quotationmark

It doesn't make sense to add two DateTime values together. If you want to represent "11 years, 2 months, 5 days, 10 hours, 10 minutes and 11 seconds" then you should represent that. That's not the same as 0011-02-05T10:10:11. In... more 1/21/2015 12:30:39 PM

people