Browsing 7239 questions and answers with Jon Skeet

Inverse of XOR Operation

I do the following: public static byte Merge(this byte b1, byte b2) { return (byte)(b1 ^ b2); } so I can call: byteVar = byteVar1.Merge(byteVar2); Now, I am searching...
Jon Skeet
people
quotationmark

XOR is a self-inverse - so you can just use: byteVar3 = byteVar.Merge(byteVar2); (I'm not sure that "merge" is a particularly good name here, mind you... you've just got an XOR operation. It's not even clear why you'd want a separate... more 6/3/2014 6:26:14 PM

people

Performance: Is it needed to put JSON object one by one to a new class?

I am working on Android project that manipulate a big JSON Objects with many types.. In performance point of view, is it needed to put the JSON object one by one to another...
Jon Skeet
people
quotationmark

Creating 1000 objects is peanuts. It should be absolutely fine, even on a mobile device. Create the objects when you read the JSON, manipulate them (as objects) within your app, and then write them out as JSON again when you need... more 6/3/2014 12:40:35 PM

people

Object equality

Object base class has the following code which compares equality of two objects public static bool Equals(Object objA, Object objB) { if (objA==objB) { ...
Jon Skeet
people
quotationmark

The first comparison is a simple reference identity comparison - in other words, do the values of objA and objB refer to the same object (or are both null). This has two purposes in Equals(object, object): It's an optimization so that... more 6/3/2014 12:30:11 PM

people

Freeze when trying to read a file using PCLStorage

I am using PCLStorage library in my project so that I can access filesystem from my PCL lib. I am trying to read a file as follows: static async Task<T> LoadAsync<T>...
Jon Skeet
people
quotationmark

This is always a potential recipe for disaster: var task = LoadAsync<User>(UserInfoFileName); user = task.Result; If this is happening in the UI thread, then you're basically blocking the UI thread until the task has completed -... more 6/3/2014 11:00:37 AM

people

Is hashcode() a good way to compute an unique token from a list of informations?

I have an HashMap<String,AnObject> and I'd like to give the string key a value from some infos the AnObject value contains. Suppose AnObject is made this way: public...
Jon Skeet
people
quotationmark

No, absolutely not. hashCode() is not guaranteed to be unique. The rules of a hash code are simple: Two equal values must have the same hash code Two non-equal values will ideally have different hash codes, but can have the same hash... more 6/3/2014 10:34:16 AM

people

Wrong timezone in XMLGregorian calendar

I am using the below code to set a XMLGregorianCalendar field in webservice SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); Date dateOfBirth = null; try...
Jon Skeet
people
quotationmark

Your SimpleDateFormat doesn't specify a time zone, so it's using your local time zone. I would suggest: Not parsing a value at all. You've got the individual day, month and year - so use that directly with a calendar! Pass a Calendar to... more 6/3/2014 7:46:29 AM

people

How to change the value of a global variable in a function while there is a local variable of same name in C#

I want to change the global variable in a function where a local variable of same is already present. int x=10; //global variable void fun1() { fun2(5); } void fun2(int...
Jon Skeet
people
quotationmark

Just qualify it with this. It's a pretty common pattern, particularly for constructors: public class Player { private readonly string name; public Player(string name) { this.name = name; } } While I view it as... more 6/3/2014 7:26:44 AM

people

C# Virtual, Override Execution Path

class zzz { public static void Main() { yyy a = new vvv(); xxx b = new vvv(); www c = new vvv(); vvv d = new vvv(); a.pqr(); ...
Jon Skeet
people
quotationmark

The method is declared virtual in yyy, and overridden in every descendant class. It's one method with lots of implementations, as far as the CLR is concerned. The compiler emits a call to that method, and at execution time the CLR will... more 6/3/2014 5:44:45 AM

people

I am getting a Radix out of range exception on performing decryption

I am generating a keypair and converting one of the same into string which later is inserted into the database using the following code: KeyPairGenerator keyGen =...
Jon Skeet
people
quotationmark

You're getting an exception because the radix you're providing is greater than Character.MAX_RADIX (which is 36). In other words, it's entirely predictable. Don't use BigInteger as an encoding class. That's not what it's there for. There... more 6/2/2014 9:24:22 PM

people

.Net Dictionary of Type using a member as key

I've been working with dictionaries typed to my custom class, and then key them off an external value. For better encapsulation, I'd like to use one of the properties of the class...
Jon Skeet
people
quotationmark

I think you're looking for KeyedCollection<TKey, TItem> Unlike dictionaries, an element of KeyedCollection<TKey, TItem> is not a key/value pair; instead, the entire element is the value and the key is embedded within the... more 6/2/2014 8:59:47 PM

people