Browsing 7239 questions and answers with Jon Skeet

How to take year 2016 onwards?

I have this code below working fine w/o the condition in the line payrollEndDate = p.PayrollEndDate.Value.Year >= 2016 because it gives me an error Invalid cast from 'Boolean'...
Jon Skeet
people
quotationmark

select is for projections, not filtering. It sounds like you want a where clause instead. I'd suggest not using query expressions here, given that you're only doing simple things: var query = db.Periods .Where(p =>... more 10/17/2016 5:58:11 AM

people

Invalid cast exception when passed as object parameter

Let's say I have two classes where one is convertible into the other: public class Foo { } public class Bar { public static implicit operator Foo(Bar bar) => new...
Jon Skeet
people
quotationmark

Why can't the runtime use the user-defined type conversion operator in the second method call? Because the compiler isn't executing at execution time... user-defined conversions are noticed by the compiler, not the runtime. The cast... more 10/16/2016 6:50:49 PM

people

Byte array to file in java without overwriting,

The code below gets a byte array from an HTTP request and saves it in bytes[], the final data will be saved in message[]. I check to see if it contains a header by converting...
Jon Skeet
people
quotationmark

You're opening a new FileOutputStream on each iteration of the loop. Don't do that. Open it outside the loop, then loop and write as you are doing, then close at the end of the loop. (If you use a try-with-resources statement with your... more 10/14/2016 8:59:29 PM

people

Java Creating string with constant value inside function

Which one is better? public class A { private static final String DOSOMETHING_METRICS = "doSomethingmetrics"; private static final String SAYSOMETHING_METRICS =...
Jon Skeet
people
quotationmark

In both cases, these are constant variables as defined in JLS 4.12.4. So not only are the strings "doSomethingmetrics" and "saySomethingmetrics" interned, but so are "Metrics for doSomethingmetricsis something" and "Metrics for... more 10/12/2016 8:55:31 PM

people

How to remove unicode.OtherSymbol from a string

I'm trying to remove characters like ✅🔮⛱😂⛄ from a given string. These characters belong to UnicodeCategory.OtherSymbol, but char.GetUnicodeCategory returns...
Jon Skeet
people
quotationmark

.NET isn't terribly good when it comes to iterating over Unicode characters instead of UTF-16 code units. All the relevant code is there, but it's not terribly easy to use. It's possible that Regex can be made to understand surrogate... more 10/12/2016 8:33:06 PM

people

Get offset minutes from timezone (string) with NodaTime

What's the easiest way to get the minutes if I only have a string that represents the timezone? (for example "Europe/London") So far I have: public static int...
Jon Skeet
people
quotationmark

You need DateTimeZone.GetUtcOffset(Instant): public static int ConvertFromTimeZoneToMinutesOffset(string timeZone, IClock clock) { DateTimeZone zone = DateTimeZoneProviders.Tzdb[timeZone]; Offset offset =... more 10/12/2016 8:06:54 PM

people

C# = operator to unsubscribe from a single event multiple times

In C# 5, what is the behavior of the -= operator when unsubscribing from events. Assume subscribing to the same event multiple times is valid for this application logic, such as...
Jon Skeet
people
quotationmark

Two are left after that. Each -= only removes one subscription. At least, that's the case if it's using just a regular delegate to back the event. You can see this easily without really involving events: using System; public class... more 10/12/2016 6:43:13 PM

people

ASP.NET Core API Controller: Response.Body.WriteAsync base64 string not working

I'm trying to return a base64 string representing a jpeg image from an API Controller and set it as the src of an <img> but all my attempts failed. Here is the very simple...
Jon Skeet
people
quotationmark

You're still returning the data as base64 text, basically - you're getting the UTF-8 encoded form of it, but that's all. If you really only have the base64 version of the image, you just need to decode that: byte[] image =... more 10/12/2016 9:16:51 AM

people

File Reader not working properly?

I have the following code and for some reason the end of file is never reached so the while loop never does end. public class Temp { FileReader fr; Temp() { try { ...
Jon Skeet
people
quotationmark

You're comparing c - a char, which is an unsigned type - with -1. Don't do that. It's promoting c to int, so you'll end up with 65535 instead. Instead, make c an int and do the cast to char inside the body of the loop. int c; while ((c =... more 10/11/2016 2:27:47 PM

people

INotifyPropertyChanged Delegate

I saw a implementation of INotifyPropertyChanged like public event PropertyChangedEventHandler PropertyChanged = delegate { }; I usually implement it like protected void...
Jon Skeet
people
quotationmark

The difference is simply that by initializing PropertyChanged with a no-op delegate to start with, you don't need to worry about whether or not the delegate is null due to there being no subscribers. Before C# 6, the "check whether or not... more 10/10/2016 11:57:30 AM

people