Browsing 7239 questions and answers with Jon Skeet

Execute byte array as an exe

I would like to execute a program stored in a byte array which is not necessarily a .NET executable without creating a new file. Currently I am using this code but it only works...
Jon Skeet
people
quotationmark

Assembly.Load is used to load .NET assemblies. It sounds like you just want to start a new process - so use the Process and ProcessStartInfo classes after saving the file to disk first: File.WriteAllBytes("tmp.exe",... more 5/2/2014 10:16:24 AM

people

Calling a constructor with a condition

I would like to do something like this: if (condition) super(foo.class); else super(bar.class); But the super constructor has to be the first in the constructor. Is it...
Jon Skeet
people
quotationmark

Assuming you're calling the same superconstructor in both cases and just passing in a different argument, you can just use the conditional operator: super(condition ? Foo.class : Bar.class); more 5/2/2014 8:35:54 AM

people

Transposing an array (CS 101)

I'm trying to transpose an array from 4x2 to 2x4, per these instructions" "Given a 2-D array arr1 of ints, return a 2-D array arr2, that is the transpose of arr1. A transpose is...
Jon Skeet
people
quotationmark

Does arr1[0].length access the length of the first column, where arr1.length accesses the length of the first row? It depends one exactly how you're viewing the array in terms of "rows" and "columns". The int[][] type is an array of... more 5/2/2014 6:45:52 AM

people

Powershell script getting called twice from C#.net code

I have following code which I call from my C#.net service which results in calling the powershell script twice. using (Runspace runspace = RunspaceFactory.CreateRunspace()) { ...
Jon Skeet
people
quotationmark

You're specifying the script both in CreatePipeline(scriptText) and AddScript(scriptText). I'm not a Powershell user, but I'd suggest that either you want using (Pipeline pipeline = runspace.CreatePipeline()) { ... more 5/2/2014 6:41:20 AM

people

Remove milli second from timstamp in oracle sql

I want to remove millisecond from 21-02-14 10:41:08.000000000 PM. I try select to_date('21-02-14 10:41:08.000000000 PM','DD/MM/YY HH:MI:SS:SSSSS PM') from dual; error: Error...
Jon Skeet
people
quotationmark

You're specifying fractional seconds, but your format has "seconds past midnight" - which doesn't have 9 digits. You're also specifying a colon in your format string, when your data has a dot. Try: select cast(to_timestamp('21-02-14... more 5/2/2014 6:23:51 AM

people

Non throwable code inside of a try/catch performance java

Will there be any negative performance implications on the following code due to methods that do not throw exceptions being inside of a try block? String user; try { user =...
Jon Skeet
people
quotationmark

Will there be any negative performance implications on the following code due to methods that do not throw exceptions being inside of a try block? No, it won't have any performance implications, but: I would generally put minimal... more 5/1/2014 9:33:02 PM

people

Try Catch Finally Not All Code Paths Return a Value

The following code should in an ideal circumstance execute a scalar sql command: public object AsScalar() { SqlCommand cmd = CreateSqlCommand(); try ...
Jon Skeet
people
quotationmark

With the catch clause, if the exception is thrown you're catching it and then it's not propagating out of the method... so execution will get to the end of your try/catch/finally block, reach the end of the method, and you won't be... more 5/1/2014 8:22:02 PM

people

Thread Sleep by name or some any other method

is there anyway to sleep the some particularthread by name or some any other source in case I've two threads Thread call = new Thread(() =>...
Jon Skeet
people
quotationmark

No - you can't ask another thread to sleep. Apart from anything else, at the time when you want it to sleep it may hold a lock or other resource which really shouldn't be held while sleeping. You'd have to do this cooperatively, with some... more 5/1/2014 7:03:19 PM

people

Fast way to copy an array into a List?

C#'s List< has a set of CopyTo functions that will extract the contents of its internal array into another array using a fast memory block copy. Is there a way to do this in...
Jon Skeet
people
quotationmark

The List<T>(IEnumerable<T>) constructor will use ICollection<T>.CopyTo if the collection implements ICollection<T>, which byte[] will do. That's not going to help directly if you only want to extract part of the... more 5/1/2014 5:34:43 PM

people

Java method to parse Date as MS JSON date format

Microsoft uses a datetime format that looks like this: \/Date(1399017480000-0000)\/ I would like to write a Java method that converts a Java date to Microsoft's datetime format....
Jon Skeet
people
quotationmark

Assuming you're just talking about java.util.Date, it's as simple as: public static String toJson(Date date) { return "\\/Date(" + date.getTime() + "-0000)\\/"; } (That's assuming the backslashes should really be part of the... more 5/1/2014 2:04:37 PM

people