Browsing 7239 questions and answers with Jon Skeet

How can I get the type of a Type?

Given the following method: public dynamic ConvertIt(dynamic source, Type dest) { return Convert.ChangeType(source, dest); } How can I get the actual...
Jon Skeet
people
quotationmark

You can use the typeof operator with your known type and compare that with the dest type: if (dest == typeof(bool)) (Reference equality is fine here, as each type only has a single Type object representing it.) It's not clear what you... more 1/29/2015 10:55:50 AM

people

Java BufferedWriter very slow 300 MB of data

I'm reading a text file consisting of fixed length records line by line and append some values and then write to another file. As usual, I used BufferedWriter, and found that it...
Jon Skeet
people
quotationmark

You've got at least two problems here: You're reopening the output file on each iteration. That's pretty much bound to be slow. Open it once at the same time you open the reader. You're not closing either the reader or the writer on... more 1/29/2015 10:32:42 AM

people

Does there exist and impossible/invalid value for `Thread.getId()`?

Is there any long value that cannot be returned by Thread.getId()? Having such a value would be very useful for indicating "no thread" or an unset value or whatever. I suppose I...
Jon Skeet
people
quotationmark

One obvious option is to keep a Long instead of a long wherever you're using this. Then you can just use a null reference for "no associated thread". That's a good general approach to this problem. However, in the specific case of... more 1/29/2015 9:38:17 AM

people

Pushing objects from an abstract class' subclass to a stack using HashMap?

I am looking for a way to instantiate a subclass from an abstract class using a token from a hashMap and then push to my stack. Any insight would be appreciated Barn class ...
Jon Skeet
people
quotationmark

Currently you can't instantiate a Cow - because doing so will create an Animal, which will in turn try to populate it map with another Cow etc. (In fact you can't create any Animal instances for the same reason.) It sounds like your map... more 1/29/2015 9:33:54 AM

people

BinarySearch Implementation does not work in certain cases

I'm a java beginner and I'm trying to code an implementation of the binary search algorithm. This is my code: protected int doSearch(List<Integer> list, int key)...
Jon Skeet
people
quotationmark

You're currently inconsistent about whether max is an inclusive lower bound, as suggested here: int max = list.size()-1; ... max = mid - 1; or an exclusive lower bound, as suggested here: while (max > min) You can make it work... more 1/28/2015 10:15:08 PM

people

How to convert Days in Year(s), Month(s), and Day(s) with Jodatime?

How to convert Days in Year(s), Month(s), and Day(s) with Jodatime? For example: days = 365; should print = 1 Year(s), 0 Month(s), and 0 Day(s) Days days = Days.days(365); ...
Jon Skeet
people
quotationmark

There's basically no such concept. After all, 365 days isn't 1 year if you start in a leap year. What you could do is take some "base" date, add your days-based period to that, and then take a year/month/day based period from the... more 1/28/2015 1:50:06 PM

people

virtual/override, new and not specified

I get the difference between virtual/override and new modifiers. But what about times when I don't specify any modifiers. For example I have Animal and Cat classes (Cat inherits...
Jon Skeet
people
quotationmark

Why does compiler won't break when I don't specify keyword. To avoid the "brittle base class" problem - where Cat originally has the Say() method, and then the author of Animal wants to add a Say() method too. With the current... more 1/28/2015 1:17:24 PM

people

Task that completes on event

I am trying to write a C# method that returns a task. The task should register to an event on another object (that I did not write and do not control) and complete when the event...
Jon Skeet
people
quotationmark

I suspect you're looking for TaskCompletionSource<TResult>. Basically, you create one of those (and remember it), and hand back the task that it provides in the Task property. When the event is triggered, set the appropriate... more 1/28/2015 12:44:43 PM

people

Add methods to Func

I have a list of Func and I want to add elements. If I add them on Start like below, no problem: public List<System.Func<bool>> conditions = new...
Jon Skeet
people
quotationmark

I strongly suspect the problem is that you're trying to access this (implicitly) within a field initializer. You're not allowed to do that. Just move the initialization into a constructor: // You don't really use public fields, do... more 1/28/2015 11:17:47 AM

people

How to properly parse DateTime string

I've got input date time strings which look like this: 2015-01-28 17:55:43. The problem is that they are in UTC+8 (or some other shift). I need those strings parsed and processed...
Jon Skeet
people
quotationmark

Given that you have a local time and an offset, I'd suggest representing that in DateTimeOffset. So: DateTime localTime = DateTime.ParseExact(...); DateTimeOffset offsetTime = new DateTimeOffset(localTime, offset); Then you still know... more 1/28/2015 10:36:57 AM

people