Browsing 7239 questions and answers with Jon Skeet

Where is c# 7.2 in visual studio project settings?

Ive seen people using and discussing c# 7.2 features but I cant seem to find it. Ive got latest updates and only up to version 7.1 is listed. why and how can I get...
Jon Skeet
people
quotationmark

Version 15.4.* of VS 2017 doesn't support C# 7.2. C# 7.2 support was introduced in VS 2017 version 15.5, which was released on December 4th. more 11/16/2017 11:36:38 AM

people

Implementing the IComparable Interface

I'm reading the book c#6.0 in a Nutshell now, the code below is on topic "Implementing the IComparable Interfaces". I don't get a few things: Why the IComparable.CompareTo is...
Jon Skeet
people
quotationmark

You could implement IComparable implicitly, yes. But fundamentally you want to try to discourage users from comparing a Note with anything other than another Note. You may have legacy usages if IComparable, but if anything knows about the... more 11/16/2017 10:23:55 AM

people

What happens if Var args method also exists with overloaded methods exists in a Java method

Here is a program with method overloading done. package com.tests; public class BasicClass { public void getMe(Object a) { System.out.println("getMe 1"); } ...
Jon Skeet
people
quotationmark

This is often done for optimization. It allows common operations (providing up to four values) to be performed without creating an array, whilst still giving the flexibility of allowing an arbitrary number of arguments. To give a... more 11/15/2017 9:26:46 AM

people

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