Browsing 7239 questions and answers with Jon Skeet

Storing timestamp with milliseconds in Oracle

I know how to retrieve timestamp with milliseconds: to_char(systimestamp ,'YYYY-MM-DD HH24:MI:SS,FF9') Can anyone please advise, is the timestamp data type sufficient to store...
Jon Skeet
people
quotationmark

Yes, TIMESTAMP allows precision down to nanoseconds if you want it. If you only need milliseconds, you just want a TIMESTAMP(3) column. Use a java.sql.Timestamp (which again goes down to nanoseconds, if you need it to) on the Java side.... more 6/27/2014 9:31:04 AM

people

get key and value list from unknown typed dictionary without using dynamic

I am trying to convert a dictionary into key value pairs so I can do some special parsing on it and store it in string format. I am using Unity so I cannot use the dynamic...
Jon Skeet
people
quotationmark

Dictionary<TKey, TValue> also implements the non-generic IDictionary interface, so you can use that: IDictionary d = (IDictionary) o; foreach(DictionaryEntry entry in d) { output[(string) entry.Key] = entry.Value; } Note that... more 6/26/2014 9:43:52 PM

people

SetValue property reflection Exception handling issues

When using property reflection to SetValue, the property throws a TargetInvocationException. However, since the call to SetValue is an invocation, the exception is caught and not...
Jon Skeet
people
quotationmark

You need the InnerException: catch(Exception ex) { if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); } } This isn't specific to reflection - it's the general pattern for any exception... more 6/26/2014 8:45:43 PM

people

Best way to convert a huge collection of key value pairs to C# object

I have a web service that returns an object with multiple child objects. The parent object as well as the child object has a huge array of key value pairs (keyvaluepair[]). At...
Jon Skeet
people
quotationmark

It sounds like you should just build a dictionary: var dictionary = pairs.ToDictionary(pair => pair.Key, pair => pair.Value); Then you can look up any entry by key very efficiently. Note that this requires that all the keys are... more 6/26/2014 7:01:00 PM

people

How to create contract class for class with required constructor arguments?

I have an abstract class whose constructor requires a parameter. The parameter cannot be null. // the abstract class [ContractClass(typeof(AbstractClassContract))] public...
Jon Skeet
people
quotationmark

It's been a while since I looked at Code Contracts, but I'd expect that your contract class was never actually instantiated. Instead, the contracts checker will basically suck the code from your contract class into each concrete... more 6/26/2014 4:05:25 PM

people

Get system clock/time in C# without using any objects?

I'm developing an application in C# that requires many function calls (100-1000 per second) to happen at very specific times. However, there are extremely tight specs on the...
Jon Skeet
people
quotationmark

What makes you think DateTime allocates objects? It's a value type. No need for a heap allocation, and thus no need for garbage collection. (As TomTom says, if you have hard latency requirements, you'll need a real-time operating system... more 6/26/2014 3:33:40 PM

people

java not finding class in jar after successfully compiling with javac against same jar

I was looking at this question on stackoverflow and thought I'd try to run the benchmark myself. I downloaded caliper and compiled it successfully using maven. I then compiled...
Jon Skeet
people
quotationmark

You're providing the -classpath argument after the class name - so it's just being passed to your main method. You need to provide it before the class name, and include the current directory in the classpath too: $ java -classpath... more 6/26/2014 12:42:24 PM

people

Converting <T>[][] to <T>[] by extracting 1 element from the inner array

Is there a potential 1 liner that allows me to create a new 1 dimensional array from a certain index of the inner array of the 2D? Example take the first element of each inner...
Jon Skeet
people
quotationmark

I'd just use LINQ. That won't "avoid loops" in terms of execution, but it'll avoid a loop in your source code: // 1dArray isn't a valid identifier... var singleArray = jaggedArray.Select(x => x[0]).ToArray(); Note that this relies on... more 6/26/2014 9:42:50 AM

people

String format to convert datetime causes an error

I try to convert persiandate to standard date.So my persian date has these formats (it means the user can enter these formats : 1392/1/1 1392/01/01 1392/01/1 1392/1/01 So i...
Jon Skeet
people
quotationmark

You're specifying MM and dd in your format, which require two digits. Just specify "yyyy/M/d" as the format - that should handle both 1 and 2-digit day/month values. (You can specify multiple formats instead, but in this case you don't... more 6/26/2014 6:15:18 AM

people

How to get the generic types used in a method in an array?

I've got this generic method, and I want to make sure that the types specified are enums. Now I understand I can't do something like where T:enum. But I want to make sure in...
Jon Skeet
people
quotationmark

You should just use typeof(T) and typeof(Y) - those will give you the actual generic type arguments. There's no benefit to using reflection here: it's slow and gives you the wrong answers! As far as I'm aware, the generic type arguments... more 6/25/2014 6:45:16 PM

people