Browsing 15980 questions and answers with Jon Skeet

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

Why is my display() method not printing out all ArrayList elements?

I have a class Container where a user should be able to input any number of words until he types nothing. I have addWord(Word) method where each input is added to an ArrayList...
Jon Skeet
people
quotationmark

Look at your addWord method: public void addWord(String word) { words = new ArrayList<String>(); words.add(word); ... more 12/3/2017 8:18:21 PM

people

Check if a string consists only of letters and / or hyphens with matches?

Good evening from Cologne. In a programming task I have to check if the given string consists only of letters and / or hyphens. Now I have an approach with matches. In the test...
Jon Skeet
people
quotationmark

Currently you're checking whether it's all letters, or all hyphens. You just need to check whether it matches letters or hyphens: public static boolean istName(String a) { return a.matches("[a-zA-Z-]+"); } The - at the end means "a... more 12/3/2017 6:38:55 PM

people

PadRight in string of arrays doesn't add chars

I created array of strings which includes strings with Length from 4 to 6. I am trying to PadRight 0's to get length for every element in array to 6. string[] array1 = { ...
Jon Skeet
people
quotationmark

The first argument to PadRight is the total length you want. You've specified 6 - array1[i].Length - and as all your strings start off with at least 3 characters, you're padding to at most 3 characters, so it's not doing anything. You... more 12/3/2017 6:08:31 PM

people