Browsing 7239 questions and answers with Jon Skeet

Operator To Return if one and ONLY One Argument is True

I'm making an if statement in C# that I would like to return true if ONLY one of the parameters is true. I'll use || in the example because that's the closest thing I can think...
Jon Skeet
people
quotationmark

For two expressions, you can just use XOR: if (a == 1 ^ b == 0) For more than two, you could do something like: if (new[] { a == 1, b == 0, c == 2 }.Count(x => x) == 1) That basically counts all the "true" elements of the array... more 6/10/2016 10:16:23 PM

people

C#: What's the behavior of the fixed statement on empty strings?

This document, which is part of the C# language spec, says that the behavior of fixed in C# is implementation-defined if it is used on a null/empty array reference. To quote...
Jon Skeet
people
quotationmark

Yes, it's guaranteed to print out 0 due to a later bit of 18.6: A char* value produced by fixing a string instance always points to a null-terminated string. Within a fixed statement that obtains a pointer p to a string instance s, the... more 6/10/2016 10:11:14 PM

people

Unable to understand the output

As per my understanding if i have used a synchronized keyword then only one thread can enter and once it leaves then only other thread.But why my below code is printing sometimes...
Jon Skeet
people
quotationmark

You're creating two separate instances of Test, and your incrementCount method is an instance method despite it incrementing a static variable. Each of your threads operates on a different instance of Test, so they won't block each other,... more 6/10/2016 3:37:51 PM

people

Reading string timestamp in UTC as UTC time using DateTime class joda time library

I have a timestamp in string, which is in UTC timezone, i want to read it as is in UTC timezone using DateTime in joda time library. Example: String utcTs =...
Jon Skeet
people
quotationmark

You can just use the overload of the DateTime constructor that takes a DateTimeZone: DateTime dtUtcTs = new DateTime(utcTs, DateTimeZone.UTC); Another option is to use a DateTimeFormatter so you can specify exactly the format you... more 6/10/2016 3:26:17 PM

people

MySQL and Java: Byte array not same when retrieved as it was when stored

I'm creating a table for users containing their hashed passwords and it's salt. The issue is that when I create the user's row in the table and save the salt and password (both...
Jon Skeet
people
quotationmark

Could there be some conversion or something that is happening that causes this problem? Yes. You're basically calling toString() on a byte array. That isn't going to do what you want. Did you look at the SQL you were executing? More... more 6/10/2016 2:16:39 PM

people

iterating over ienumerable with linq2xml

I was surprised to see that not all of my elements in the following code are being iterated: IEnumerable<XElement> dataStorageGroupElements = document.Descendants().Where(x...
Jon Skeet
people
quotationmark

The problem is described in MSDN: "Mixed declarative code / imperative code bugs (LINQ to XML)": LINQ to XML contains various methods that allow you to modify an XML tree directly. You can add elements, delete elements, change the... more 6/10/2016 1:27:55 PM

people

C#6's Improved overload resolution clarification?

Among all the new features in C#6, the most mysterious feature (to me) is the "improved overload resolution". Maybe it's because I couldn't find related info/examples/explanation...
Jon Skeet
people
quotationmark

I believe what is meant here is the "better betterness" rules which are documented in the Roslyn github repo. Sample code: using System; class Test { static void Foo(Action action) {} static void Foo(Func<int> func) {} ... more 6/10/2016 10:56:15 AM

people

Why instance block changes are not reflected when passing the changed value to constructor

I just came across a scenario, which i never experimented with before - public class InstanceBlocks { public static int i = 5; { i ++; ...
Jon Skeet
people
quotationmark

The order of execution here is: Evaluation of i inside this(i) (i is still 5 at this point) Object constructor Instance initializer (the block that increments i from 5 to 6) Execution of the body of the InstanceBlocks(int) constructor... more 6/10/2016 10:36:16 AM

people

How delegate inside loop understands difference between local and loop variable

Take a look this snippet of the code List<Action> actions = new List<Action>(); for (int variable = 0; variable < 5; ++variable) { int...
Jon Skeet
people
quotationmark

Looking at the IL code I tend to think that compiler creates only one instance of myLocalVariable. Nope. The C# language specification makes it clear that the variable is instantiated on each iteration of the loop - so there's a... more 6/9/2016 5:08:04 PM

people

2D Array vs Array of Arrays in tight loop performance C#

I had a look and couldn't see anything quite answering my question. I'm not exactly the best at creating accurate 'real life' tests, so i'm not sure if that's the problem here....
Jon Skeet
people
quotationmark

In the CLR, there are two different types of array: Vectors, which are zero-based, single-dimensional arrays Arrays, which can have non-zero bases and multiple dimensions Your "array of arrays" is a "vector of vectors" in CLR... more 6/9/2016 5:51:56 AM

people