Browsing 7239 questions and answers with Jon Skeet

C# Max or default?

How do I do IEnumerable.Max with a default value for empty collections in C#? Here is what I want to do in Java 8: public static void doTest(List<String> l) { ...
Jon Skeet
people
quotationmark

I'd just use DefaultIfEmpty: int maxLength = l.Select(s => s.Length).DefaultIfEmpty().Max(); Or to specify a default: int maxLength = l.Select(s => s.Length).DefaultIfEmpty(-1).Max(); Or use a nullable value so you can tell the... more 1/29/2014 10:45:30 PM

people

Tricky NullPointerException while sending a file to a client

I am working on a simple server in Java that should have a capability of transferring a file across computers. I am getting a NullPointerException on line 77 of Protocol.class....
Jon Skeet
people
quotationmark

You're never initializing lns in Protocol, so it's always a null reference. You may be able to get away with just changing the declaration to: private List<String> lns = new ArrayList<String>(); (I've made it private and... more 1/29/2014 10:23:27 PM

people

Neat way to find the number of significant digits in a BigDecimal?

I do not want to limit the number of significant digits in a BigDecimal. I only want to find the number of significant digits that number has. Is there a way to do this without...
Jon Skeet
people
quotationmark

I believe you want a combination of stripTrailingZeros, precision and scale, as demonstrated here: import java.math.*; public class Test { public static void main(String[] args) { test("5000"); // 4 ... more 1/29/2014 10:19:41 PM

people

How to sign an assembly in .NET 1.1

So, for some horrible reason I need to compile a very old .NET 1.1 project. Compiling it is pretty easy, but I also need it to produce signed assemblies. I noticed that .NET 1.1...
Jon Skeet
people
quotationmark

You use the AssemblyKeyFileAttribute - it's basically equivalent to the command-line flag (as far as I'm aware) but less flexible due to being part of the source code, and it leaks the keyfile path information into the assembly itself.... more 1/29/2014 9:45:15 PM

people

What is the difference between typing long x = 43 and long x = 43L in Java?

What would be the difference between long x = 43 and long x = 43L in Java? Do both of them initialize x to be have the long data type?
Jon Skeet
people
quotationmark

Do both of them initialize x to be have the long data type? Absolutely. The type of the variable is entirely determined by the declaration part, not the initialization. Your first form is logically equivalent to: long x = (long)... more 1/29/2014 8:50:26 PM

people

override default DateTime format in C#

I am working in an application built in .NET framework 2.0 but I am running it in .NET framework 4.0, with SQL server as a database. when I try to take date as a input from...
Jon Skeet
people
quotationmark

Assuming you mean this Calendar control, you should just use the SelectedDate or SelectedDates property - let the control handle the conversions. In general, you should avoid performing textual conversions unless you really need to. (This... more 1/29/2014 8:43:25 PM

people

Parallel.Invoke() without waiting on execution to finish

Please know that I'm aware that Parallel.Invoke() is meant for task synchronization. My question is this: Is there a way to call an anonymous method using something like...
Jon Skeet
people
quotationmark

If you want to wait for a piece of code to finish executing, instead of using Parallel.Invoke, why not just call the code directly? Well normally you'd call Parallel.Invoke with multiple pieces of work. If you execute those pieces of... more 1/29/2014 8:33:13 PM

people

How can I check is strings with characters that are not letters or number

I am trying to trim any characters that are before a-Z 0-9 but this doesn't work I need >&%Hell$o to become Hell$o private String removeStartingCharacters(String...
Jon Skeet
people
quotationmark

I think this is what you're after: public class Test { public static void main(String[] args) { System.out.println(trimStart("&%Hell$o")); // [ and ] are between A-Z and a-z... ... more 1/29/2014 7:54:43 PM

people

C# Compiler Optimizations

I'm wondering if someone can explain to me what exactly the compiler might be doing for me to observe such extreme differences in performance for a simple method. public static...
Jon Skeet
people
quotationmark

To look at what the C# compiler does for you, you need to look at the IL. If you want to see how that affects the JITted code, you'll need to look at the native code as described by Scott Chamberlain. Be aware that the JITted code will... more 1/29/2014 7:44:58 PM

people

Why does ByteArrayOutputStream use int?

Maybe someone can help me understand because I feel I'm missing something that will likely have an effect on how my program runs. I'm using a ByteArrayOutputStream. Unless I've...
Jon Skeet
people
quotationmark

ByteArrayOutputStream is just overriding the abstract method declared in OutputStream. So the real question is why OutputStream.write(int) is declared that way, when its stated goal is to write a single byte to the stream. The... more 1/29/2014 6:56:17 PM

people