Browsing 7239 questions and answers with Jon Skeet

Confusion as to initialization (or lack thereof) of object

package lab9; import java.util.Scanner; import java.util.Date; public class AccountManager { // the account private Account account; // reader for reading user...
Jon Skeet
people
quotationmark

This is the problem: AccountManager accountmanager= new AccountManager (account, t, accountmanager); You're declaring a variable, and trying to read from that variable in the same statement that gives it an initial value. Here's a... more 12/8/2017 4:49:56 PM

people

Noda Timezone issue with IDateTimeZoneSource.MapTimeZoneId

I am trying to convert Windows to IANA time zone and below is the code that works most of the time. But when the windows time id = "Turks & Caicos" then below code returns...
Jon Skeet
people
quotationmark

GetSystemDefaultId doesn't replace all the functionality of MapTimeZoneId - you don't want to call that here. Instead, use tzdbSource.WindowsMapping to get a WindowsZones that you can then use to find the TZDB ID. Here's some sample... more 12/8/2017 4:21:36 PM

people

C# Add 1 day in specific TimeZone to DateTimeOffset

I have an instance of DateTimeOffset and I need to add 1 day to it in specific TimeZone (W. Europe Standard Time) taking into account daylight saving rules (so it might result in...
Jon Skeet
people
quotationmark

TimeZoneInfo makes this reasonably simple - just add a day to the DateTime part of the value, check whether the result is skipped or ambiguous, and if not, ask the zone for the UTC offset. Here's a complete example showing all the... more 12/8/2017 9:46:26 AM

people

Parse the string "26h44m3s" to TimeSpan in C#

I need to parse the string "26h44m3s" to TimeSpan in C#. I cannot find anything implemented in .NET that can handle it. So how do I accomplish it, and in clean way? And are there...
Jon Skeet
people
quotationmark

You can use Noda Time for this, parsing as a Duration. You could then convert it to a TimeSpan - or you could use Noda Time everywhere and have a nicer experience :) Sample code: using System; using NodaTime; using NodaTime.Text; class... more 12/7/2017 7:37:37 PM

people

What is the DateTimeStyles operator in .NET?

When I add this code DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowInnerWhite Intellisense shows the following message: DateTimeStyles DateTimeStyles.operator...
Jon Skeet
people
quotationmark

DateTimeStyles isn't an operator - it's an enum, and all enums have the | operator. All it does is apply a bitwise | for the two values. It should only be used for flag-based enums. For example: public enum AccessMode { None = 0, ... more 12/7/2017 2:05:01 PM

people

NodaTime timezone parse: Etc/GMT+3 offset in negative

This is a little code sample to illustrate the issue: enter code here var offset1 = DateTimeZoneProviders.Tzdb.GetZoneOrNull("Europe/Moscow") ...
Jon Skeet
people
quotationmark

Yes, that's correct. That's because the "Etc/GMT+X" zone IDs are confusing. They match POSIX TZ strings, which for some reason represent "the offset of UTC from local time" instead of the normal "offset of local time from UTC". See the... more 12/7/2017 7:26:52 AM

people

Why bother using lambda expressions in logging APIs if the compiler can possibly inline the logging call

Many logging frameworks (e.g., log4j) allow you to pass lambda expressions instead of Strings to the logging API. The argument is that if the string is particularly expressive to...
Jon Skeet
people
quotationmark

Your optimization hasn't just introduced inlining - it's changed ordering. That's not generally valid. In particular, it wouldn't be valid to change whether methods are called, unless the JIT can prove that those methods have no other... more 12/6/2017 8:23:08 PM

people

Task does not wait up to the wait time

I have created a task and provided the wait time to the task.wait() method, but the task does not wait up to the provided time and return before the wait time with completed...
Jon Skeet
people
quotationmark

The problem isn't that Task.Wait isn't waiting long enough here - it's that you're assuming that as soon as you call Task.Factory.StartNew() (which you should almost never do, btw - use Task.Run instead), the task is started. That's not... more 12/6/2017 7:46:57 AM

people

Why is garbage collector allowed to collect seemingly referenced objects with a finalizer?

This question is basically why we need GC.KeepAlive() in the first place. Here's where we need it. We have a wrapper for some unmanaged resource public class CoolWrapper { ...
Jon Skeet
people
quotationmark

Why is this needed? Why wouldn't GC ignore the objects which have a method running at that moment and also have a finalizer? Because that's not what the GC (or the C# specification) guarantees. The guarantee is that if an object won't... more 12/5/2017 3:12:31 PM

people

Last line in console not overwrite

I want to overwrite the last line in the console for looping a specific row with changes. For example: I want to print to console . then .. then ... This my code for that: int...
Jon Skeet
people
quotationmark

You're only printing one character when you print a single dot - you're not affecting the rest of the line. If you just change this: Console.WriteLine("."); to Console.WriteLine(". "); then it'll remove any characters written by... more 12/4/2017 7:25:41 AM

people