Browsing 15980 questions and answers with Jon Skeet

What is the cleanest way to see if a nullable type has a value, and if it does, to compare the value to another non nullable type's value?

I have two nullable enums which I would like to compare the value of to two regular enums. If the nullable enums do not have a value, I want the comparison to evaluate to true....
Jon Skeet
people
quotationmark

It's probably simplest to use the null-coalescing operator to "default" to the value that you're comparing against: if ((nullableEnumOne ?? regularEnumOne) == regularEnumOne && (nullableEnumTwo ?? regularEnumTwo) ==... more 11/8/2017 5:52:22 PM

people

Quick way to get different node values from a file in C#?

I want to get the values of different nodes using different conditions of a xml file in different variables. Below is an example XDocument doc =...
Jon Skeet
people
quotationmark

Well you certainly don't need to use the query expressions - they're mostly just getting in the way. This code would be simpler as: XDocument doc = XDocument.Load(@"D:\MyFiles\test.xml",LoadOptions.PreserveWhitespace); var a =... more 11/7/2017 2:19:31 PM

people

How to efficiently ensure a decimal value has at least N decimal places

I want to efficiently ensure a decimal value has at least N (=3 in the example below) places, prior to doing arithmetic operations. Obviouly I could format with...
Jon Skeet
people
quotationmark

If you're nervous that the compiler will optimize out the operator (although I doubt that it would ever do so) you could just call the Add method directly. Note that you don't need to add and then subtract - you can just add 0.000m. So for... more 11/5/2017 2:07:11 PM

people

SmtpClient not working on google cloud server

I have an SmtpClient that works fine when I run it localhost. However, on google cloud server it returns: System.Net.Mail.SmtpException: Failure sending mail. --->...
Jon Skeet
people
quotationmark

I suspect the problem is that traffic on port 587 is blocked by default on Compute Engine, except for known IP addresses. From the firewall documentation: Compute Engine blocks or restricts traffic through all of the following... more 11/5/2017 8:30:49 AM

people

Double/long support for JTokenTypes?

Is there a way to designate that the Type of a JToken is of type double or long? I noticed that only integers and floats are supported via...
Jon Skeet
people
quotationmark

JSON doesn't distinguish between double and float. It doesn't even really distinguish between integers and non-integers - they're just numbers. But JsonTokenType.Float isn't really meant to indicate System.Single - it's "a floating point... more 11/1/2017 11:51:51 PM

people

How to read a Stream one by one?

The Java Stream.forEach function has the serious limitation that it's impossible for its consumer to throw checked exceptions. As such, I would like to access a Stream's elements...
Jon Skeet
people
quotationmark

To iterate over a stream element-by-element, just call the iterator() method: Iterator<String> iterator = stream.iterator(); while (iterator.hasNext()) { String element = iterator.next(); // Use element } It's not clear to... more 11/1/2017 8:01:39 PM

people

DecimalFormat removes zero after dot

I want to format user's input and it's okay, but when I try to input zero after dot DecimalFormat removes it. I use the following code: DecimalFormat df = new...
Jon Skeet
people
quotationmark

It removes zeros! Well yes, it would - you've specifically used .## which means "only include digits if they're significant". If you want to always have at least one decimal place, use DecimalFormat df = new... more 10/30/2017 8:52:38 AM

people

How is base.GetHashCode() implemented for a struct?

I saw this code recently in a struct and I was wondering what base.GetHashCode actually does. public override int GetHashCode() { var hashCode = -592410294; ...
Jon Skeet
people
quotationmark

The coreclr repo has this comment: Action: Our algorithm for returning the hashcode is a little bit complex. We look for the first non-static field and get it's hashcode. If the type has no non-static fields, we return the... more 10/30/2017 7:07:34 AM

people

How do I fix daily Limit Exceeded when using the .NET client for the google translation API?

I am trying to implement a small slackbot thing where I want to leverage the google tranlation API, but I struggle to get it to work. I did manage to run a single request, but...
Jon Skeet
people
quotationmark

Yes, TranslationClient.Create will use the default credentials - but in this case it's almost certainly the case that they're the wrong default credentials, assuming you're running this locally. If you're running this on Google Cloud... more 10/29/2017 12:27:53 PM

people

Why subsignature and unchecked rules work this way on return types when overriding a generic method with a non generic one?

public class Base { <T> List<? extends Number> f1() {return null;} List<? extends Number> f2() {return null;} <T extends Number>...
Jon Skeet
people
quotationmark

Your f1 and f3 overrides aren't generic, despite the original declarations being generic. The compiles allows the return type of the overrides to vary from the original return types as they have the same type erasure (List). I think that... more 10/29/2017 9:01:09 AM

people