Browsing 7239 questions and answers with Jon Skeet

Implicit conversion of a method to Func<T, Tresult>

I have a an extension class that extends Func<T, TResult> whose signature looks like this: public static ITryAndReturnValue<TResult> Try<T, TResult>(this...
Jon Skeet
people
quotationmark

No, you can't call an extension method on a method group or on an anonymous function. Section 7.6.5.2 of the C# specification requires that: An implicit identity, reference or boxing conversion exists from expr to the type of the... more 7/27/2016 9:35:59 PM

people

setGregorianChange(new Date(Long.MIN_VALUE)) does not convert the calendar to pure GregorianCalendar

GregorianCalendar test= new GregorianCalendar(); BigDecimal coef=new BigDecimal(-2113480800); test.setGregorianChange(new Date(Long.MIN_VALUE)); test.set(Calendar.DAY_OF_MONTH,...
Jon Skeet
people
quotationmark

The calendar you're using is fine - you just need to remember that a Date only stores an instant in time. It doesn't remember a calendar, time zone or text format. The Date value you've got is correct - it has an epoch millis of... more 7/27/2016 4:15:44 PM

people

FileWriter only writes first line (Append Mode enabled, Java)

I have a problem with writing incoming information from a client to this program. The data comes in and there is output every second from the System.out, but the FileWriter only...
Jon Skeet
people
quotationmark

You have this structure: while (true) { try { // Code which writes one line } finallly { out.close(); } } In other words, you're closing the output after the first line, but continuing to do work. That's not... more 7/27/2016 3:11:59 PM

people

Eclipse dead code warning, but what is wrong?

I've written a method that checks if my string is in a file, but eclipse gives dead code warning. Here is my method: private boolean KeyPresent(String key){ try{ ...
Jon Skeet
people
quotationmark

Look at the whole if/else: if (s.equals(key)) { ... } else if (s == null) { ... } If s is null, then s.equals(key) will have thrown a NullPointerException - so you'll never get into the second if block. You should use a... more 7/25/2016 8:40:41 PM

people

Type Covariance runtime errors

C# 6.0 in a Nutshell by Joseph Albahari and Ben Albahari (O’Reilly). Copyright 2016 Joseph Albahari and Ben Albahari, 978-1-491-92706-9. states, at pages 123-124, with...
Jon Skeet
people
quotationmark

What is the reason behind such error? Because it is trying to store a Camel into an array with a runtime type of Bear[]. An array of type Bear[] can only store references to instances of Bear or subclasses. The compile-time type of... more 7/25/2016 1:39:34 PM

people

Java ArrayList in Junit test filled with single value

i have the following Junit class for testing (tests are not included sinc they are not important in this case): public class BoardImplTest { List<Coords> startingCells =...
Jon Skeet
people
quotationmark

You're creating a single instance of Coords, and adding a reference to that single object every time addCell is called, after mutating it. You should create a new instance of Coords each time, e.g. // Remove the cellCoords field private... more 7/25/2016 9:23:15 AM

people

Dictionary doesn't find value when using new object as key

In Unity 3D, I am trying to build a Dictionary of PathNodes keyed by NodeCoordinates. I have overridden GetHashCode in NodeCoordinate and it should return a constant unique value....
Jon Skeet
people
quotationmark

The problem is that your NodeCoordinate class doesn't define equality in a way that the dictionary would use. You have a method like this: public bool Equals(NodeCoordinate coord) ... but you neither override... more 7/24/2016 2:53:59 PM

people

Parse string to localdatetime

I have an strange problem when parse a string to a localdatetime import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Main { public...
Jon Skeet
people
quotationmark

I don't care what month/year/day is asigned, i only want the time. That suggests you should be parsing it as a LocalTime, given that that's what the string actually represents. You can then add any LocalDate to it to get a... more 7/23/2016 1:10:15 PM

people

Cannot implicitly convert 'System.TimeSpan?' to 'System.TimeSpan'

The below code works fine : DateTime d1 = DateTime.Now; DateTime d2 = DateTime.Now.AddDays(-1); int d3 = (int)(d1 - d2).TotalDays; But what if I define DateTime as DateTime?...
Jon Skeet
people
quotationmark

Well yes, but you need to use the Value property to "un-null" it: int d3 = (int)(d1 - d2).Value.TotalDays; However, you should consider the possibility that either d1 or d2 is null - which won't happen in your case, but could in other... more 7/23/2016 12:30:59 PM

people

Proper conversion to enum

I have an enum with a bunch of values that looks like this. Notice that I use 0 for "undefined". public enum MyEnum { Apple = 1, Banana = 2, Orange = 3, Undefined =...
Jon Skeet
people
quotationmark

Just use Enum.IsDefined: public static MyEnum GetEnum(int value) => Enum.IsDefined(typeof(MyEnum), value) ? (MyEnum) value : MyEnum.Undefined; Complete example: using System; public enum MyEnum { // Moved here as it's more... more 7/23/2016 1:21:09 AM

people