Browsing 7239 questions and answers with Jon Skeet

Ternary Operator. true is empty

I was wondering how this code would look like with the ternary operator if (a) { // pass } else { b(); } We got no(thing) code to execute when a is true but in case a...
Jon Skeet
people
quotationmark

That's not what the conditional operator is for... it's not to execute one statement or another, it's to evaluate one expression or another, and make that the result of the expression. You should write your code as: if (!a) { ... more 11/11/2015 9:13:29 PM

people

why com.ExecuteNonQuery() return 1 Always . in all cases

com.ExecuteNonQuery() always returns -1 - in all cases. Why is it always = -1 ? SqlConnection conn =...
Jon Skeet
people
quotationmark

It's because you're using it with a SELECT SQL command. From the documentation for SqlCommand.ExecuteNonQuery (emphasis mine): For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.... more 11/11/2015 8:30:47 PM

people

Performance difference in two equal methods

I am reading samples for JMH framework, and I have a question about code from sample called JMHSample_12_Forking. After running this code I have following results (just as author...
Jon Skeet
people
quotationmark

During the very first test, there's one implementation of Counter. The JIT compiler is able to assume that anything calling measure(Counter) is using the same implementation, so it can inline the code from inc(). In the second test, we... more 11/11/2015 8:13:43 PM

people

Error on the MSDN page for base constructors (c#)

I think there is an error in the following page https://msdn.microsoft.com/en-us/library/ms173115.aspx about constructors and invoking base constructors right after the following...
Jon Skeet
people
quotationmark

Nope, it's showing you the source of the constructor is being called by this(weeklySalary * numberOfWeeks)... and that's the single-parameter constructor which just assigns to the salary variable. Think of the code as being like this: //... more 11/11/2015 5:50:49 PM

people

Convert ZonedDateTime to date representation with zone

I want to represent dates with associated zones, but I didn't found any type to represent the date in Noda time (something similar to the LocalDate, but with zone information, so...
Jon Skeet
people
quotationmark

I suggest you build your own class or struct that just has the pair of them - or use Tuple<LocalDate, DateTimeZone>. There's nothing specific for this in Noda Time at the moment, and I don't expect to add it unless we hear this as a... more 11/11/2015 11:20:12 AM

people

Running synchronous method async

I have a method that iterates a list of objects and for each item in the list fetches data from an external api. Sometimes this can be very slow (naturally) and I'd like to add...
Jon Skeet
people
quotationmark

Rather than using async here, you can just make GetStuff a normal synchronous method and use Task.Run to create new tasks (which will normally be run on multiple threads) so that your fetches occur in parallel. You could also consider... more 11/11/2015 8:44:48 AM

people

How do I configure Ninject to inject the NodaTime IClock

In my NinjectConfigurator I have container.Bind<IClock>().To<SystemClock>(); I have also...
Jon Skeet
people
quotationmark

I haven't used NInject myself for a while, but I believe you want to use ToConstant() to bind to the instance of SystemClock: container.Bind<IClock>().ToConstant(SystemClock.Instance); more 11/11/2015 6:57:10 AM

people

Combined null and value check

This code works fine: policy.ProviderID > 0 ? RefDataSources.LegalBodies.GetDisplayName(policy.ProviderID.Value) : null but Resharper is complaining that...
Jon Skeet
people
quotationmark

I suspect R# is complaining because it doesn't know that policy.ProviderID will return the same value on both evaluations. Consider: private readonly int? providerId; public int? EvilProviderId => DateTime.UtcNow.Second == 0 ? null :... more 11/10/2015 11:39:12 AM

people

How To Handle Covariance Limitations?

This is basically a design problem. My class structure is as follows: Interface IA interface IB<T> where T : IA abstract class AC<T> : IB<T> class D :...
Jon Skeet
people
quotationmark

It sounds like you need to make the Foo method generic: void Foo<T>(IB<T> param) where T : IA At that point, Foo(new G()) would be implicitly Foo<E>(new G()). more 11/10/2015 9:48:27 AM

people

byte[] to String returns different String

For a given byte[], always the same, I'd like to get the corresponding String. the byte[] result has always the same value. However the String returned is never the same, each...
Jon Skeet
people
quotationmark

You're using a different IV every time you encrypt - therefore you'll get different ciphertext each time you encrypt, too. Your results byte array is different each time, therefore the base64 representation is different. If you really... more 11/10/2015 7:19:46 AM

people