Browsing 7239 questions and answers with Jon Skeet

Whats the difference between ref parameter and return value (methods)

I've been studying methods and i have stumbled across the "ref" method. However both codes seem to accomplish the same thing: class Program { static void Main(string[] args) ...
Jon Skeet
people
quotationmark

Is there an advantage that ref parameter has over return value? personally i don't see a massive difference. Well typically, ref is used when there's already something else being returned - e.g. for int.TryParse (which uses out, but... more 9/22/2016 4:37:31 PM

people

Inline functions and conditional compiling in VB.NET or C#?

Does VisualBasic.NET or C# support conditional compiling? And inline functions (macros)? When I talk about conditional compiling, I mean something like C/C++, where you...
Jon Skeet
people
quotationmark

Conditional compilation is supported by both C# and VB: C#: #if DEBUG Foo(); #else Bar(); #endif VB: #If DEBUG Then Foo #Else Bar #End If Macros are not supported in C# or VB as far as I'm aware... typically inlining is... more 9/22/2016 4:14:15 PM

people

Not able to convert string dd/MM/yyyy to Date dd/MM/yyyy in java

I have an input string of the format dd/MM/yyyy, I need to convert it into date dd/MM/yyyy. My approach is: SimpleDateFormat formatter = new...
Jon Skeet
people
quotationmark

You seem to be assuming that a java.util.Date "knows" a format. It doesn't. That's not part of its state - it's just the number of milliseconds since the Unix epoch. (There's no time zone either - the IST you're seeing there is your local... more 9/22/2016 1:00:20 PM

people

How to combine asynchronous method with synchronous method in C#?

I'm calling from the main method: public MainPage() { Text_to_Speech.changetospeech("Welcome to Nepal!", newmedia).Wait(); mytxtblck.Text="Hello from Nepal!" } What I...
Jon Skeet
people
quotationmark

It sounds like what you really want is to trigger the text change when the MediaEnded event is fired. You could do that within your ChangeToSpeech method, although it'll be a little ugly: public async static Task ChangeToSpeech(string... more 9/22/2016 9:26:47 AM

people

Not getting the expected output in Java

Input Format The first line contains an integer, . The second line contains a double, . The third line contains a string, . Output Format Print the sum of both integers on...
Jon Skeet
people
quotationmark

Scanner.next() reads the next token. By default, whitespace is used as a delimited between tokens, so you're only getting the first word of your input. It sounds like you want to read a whole line, so use Scanner.nextLine(). You need to... more 9/22/2016 6:14:11 AM

people

Is there a callback for when a task is completed in Task.WhenAll

Suppose I have the following: IEnumerable<Task<TimeSpan>> tasks = //... TimeSpan[] results = await Task.WhenAll(tasks); // Handle results By the time I can handle...
Jon Skeet
people
quotationmark

Is there a way to handle each result on demand? Yes, you use WhenAny instead of WhenAll... or call ContinueWith on each task. For example, for the WhenAny approach: ISet<Task<TimeSpan>> tasks = new... more 9/21/2016 6:54:24 PM

people

TimeZoneNames standard, daylight, generic uses

While trying to differentiate between the Mountain time zone and Arizona (I realize they are both Mountain Time) I found that GetNamesForTimeZone("America/Phoenix", "en-us")...
Jon Skeet
people
quotationmark

As far as I can tell, TimeZoneNames is all about just the time zone names themselves - it doesn't know anything about the time zone data itself. If you want to know whether America/Phoenix is currently observing daylight savings, I'd... more 9/21/2016 3:46:40 PM

people

strange method overload resolution with constrained generic arguments: overload with base class arg always called

I have some - probably really stupid - question on overload resolution. Suppose there is a constrained generic method Greet accepting parameter person of type T which must derive...
Jon Skeet
people
quotationmark

The compiler has to perform overload resolution for the call of new Greeter().SayHi(person); At that point, the type of person is just T, and all it knows is that T is implicitly convertible to Person. Therefore, the only applicable... more 9/21/2016 10:18:19 AM

people

Does a MemoryBarrier guarantee memory visibility for all memory?

If I understand correctly, in C#, a lock block guarantees exclusive access to a set of instructions, but it also guarantees that any reads from memory reflect the latest version...
Jon Skeet
people
quotationmark

Reads within the lock block see the latest versions of a variable and writes within the lock block are visible to all threads. No, that's definitely a harmful oversimplification. When you enter the lock statement, there a memory... more 9/20/2016 7:56:00 PM

people

.NET NodaTime How do I create a custom TimeZone?

I want to create a custom (non-existing) TimeZone which follows exactly the same rules as Europe/London however the only difference is that the offset should be -5 hours from...
Jon Skeet
people
quotationmark

This certainly isn't a terribly common scenario, but it's definitely supported. (Unlike creating your own calendar, for example.) You'd specify daylight saving via GetZoneInterval - that method is the key to the class. The important... more 9/20/2016 7:43:17 PM

people