Browsing 7239 questions and answers with Jon Skeet

Suppressing issues from Roslyn code Analyzers

Is there any way to suppress the issues from the Roslyn Analyzers? I use the instant analyzer project type. And I want to suppress the issues if the user wants it. Also it must be...
Jon Skeet
people
quotationmark

You can ignore warnings/errors from Roslyn analyzers in exactly the same ways as ignoring normal C# compiler warnings: #pragma disable within source code The Project Properties / Build / Errors and Warnings setting The [SuppressMessage]... more 1/11/2016 11:34:29 AM

people

Inconsistent Enum Casting Results in C#

I was recently revisiting some of the strange behavior of C# related to casting an integer into an existing enum where some enum integer values are assigned explicitly and some...
Jon Skeet
people
quotationmark

It's not the casting that's the problem - it's the conversion to a string representation. Lerp and Dur are the same value - you can't distinguish between them. Indeed, if you call ToString() directly on them, like... more 1/11/2016 7:08:29 AM

people

How to iterate over items of a list when I only have access to the object of that list and dont know the type parameter?

Somewhere in my code I have an object that I already know that is a list. But I don't know the type parameter of that list. I need to iterate over it's items. I tried to cast that...
Jon Skeet
people
quotationmark

You should cast to IEnumerable<object>, or even just IEnumerable. A List<string> is not a List<object> as generic variance doesn't apply to classes, and a List<string> is not an IList<object> as... more 1/11/2016 7:02:17 AM

people

Two java variables referencing same string object don't sync

From my novice perspective of Java object, if two variables referencing one object, updating one variable should do the same to the other, as the code shows below: SomeObject s1...
Jon Skeet
people
quotationmark

No, the difference is in the kind of change you make. This code: s2.setText("second") doesn't change the value of either s2 or s1. Both s1 and s2 refer to the same object as they did before... but the contents of the object has... more 1/10/2016 2:28:32 PM

people

How to create 2 depends Comparators in java?

Lets say I have a Product class in Java and 2 Comparators: 1st is price Comparator for asc order. 2nd is price Comparator for desc order. It can be that if I changed the 1st...
Jon Skeet
people
quotationmark

I would suggest only having a single comparator class for comparing by price, and a separate comparator class to compare by name (or no classes - see the end of my answer). Each class does one thing, and does it well. Then you can reverse... more 1/10/2016 1:55:27 PM

people

A way to consider the backslash as a normal character

Is there someone who knows how to consider the backslash as a normal character (not an escape character) in a string in Java? Any help will be appreciated!
Jon Skeet
people
quotationmark

No, Java doesn't have anything like the verbatim string literals of C# and other languages. Backslash is always an escape character in a Java string or character literal. Note that it's only in literals that Java cares, as a language. The... more 1/9/2016 12:30:58 PM

people

Java why is my if/else statement not working correctly?

In android studio, the variable "output" in my if/else statement shows up as gray(never used) and then on the last line when I try to use it, I get an error saying "cannot resolve...
Jon Skeet
people
quotationmark

You're declaring the variables inside the blocks. Any local variable is out of scope outside the block in which it's declared. That's why those variables are greyed out - they aren't used anywhere, because the blocks in which they're... more 1/9/2016 7:58:16 AM

people

How to set a ZonedDateTime's time component in Nodatime correctly

I have a method that is supposed to return the start time of a working day, e.g. 9AM from a ZonedDateTime that it takes as a parameter: public ZonedDateTime...
Jon Skeet
people
quotationmark

It sounds like you don't need to use AtStartOfDay at all - just use the date and add your WorkDayStartTime: public ZonedDateTime GetWorkStartTime(ZonedDateTime zdt) => (zdt.Date + WorkDayStartTime).InZone(zdt.Zone,... more 1/9/2016 7:43:42 AM

people

Trying to understand hashCode() implementation of the ArrayList

Looking through the java source code, I faced with incomprehensible for me construction in the hashCode() method of the class AbstractList. This is implementation of the hashCode...
Jon Skeet
people
quotationmark

But to which class(type) pointer this belongs? this is the list that hashCode was called on. So the compile-time type is AbstractList<E>. It's saying "for every element in this list, include that element's hash code in the... more 1/9/2016 7:40:49 AM

people

c# array outputs 0 when it isnt specified

I am trying to output the odd and even numbers of an array, i have got it all working but for some reason when i create a foreach loop and output each number that is odd/even it...
Jon Skeet
people
quotationmark

but i don't want the 0's at the beginning of them both, i don't want a 0 at all Then you shouldn't create an array with 0s in, which is what you're doing. Before you call Array.Sort(odds), your array will have all the odd numbers... more 1/8/2016 4:23:00 PM

people