Browsing 15528 questions and answers with Jon Skeet

Unexpected result with Where()

I have this code, which is supposed to return a value type, applying at each step the transformations specified in steps. private static T Transformed<T>(T x, params...
Jon Skeet
people
quotationmark

The Where is lazily evaluated - you're never using the result of it, so the predicates are never being evaluated. You could force iteration by counting the results or similar: var ignored steps.Where(f => (x =... more 2/21/2017 3:36:25 PM

people

Java (Android) BufferOverflowException on putInt

I try this code : byte arr[] = ByteBuffer.allocate(2).putInt(1).array() But it fails with a BufferOverflowException. Is 1 too big to be stored in 2 bytes ? Or is my problem...
Jon Skeet
people
quotationmark

Is 1 too big to be stored in 2 bytes ? Well, an int is... putInt always writes 4 bytes. From the documentation for ByteBuffer.putInt Throws: BufferOverflowException - If there are fewer than four bytes remaining in this... more 2/21/2017 10:25:29 AM

people

C# / LINQ fastest way of comparing two lists and assigning value

I have made a code which basically compares two lists in C#. First list contains properties like this: ItemID TotalViews First list lacks values for TotalViews so I'm...
Jon Skeet
people
quotationmark

This is like jdweng's answer, but slightly simpler and it won't throw an exception for missing item IDs: var hitCountsById = HitCountItemIDS.ToDictionary(x => x.ItemID, x => x.HitCount); foreach (var item in parsedMerchantData) { ... more 2/20/2017 7:01:32 PM

people

Second iteration of XDocument.Save() throwing "Access to Path Denied"

I'm writing a small class to manage configuration XML for an in-house application. The following code is called each time a new list of t is loaded, and attempts to serialize...
Jon Skeet
people
quotationmark

I'm surprised it's working on the first iteration - because you're not checking a useful file. Instead of calling Path.Combine with multiple arguments, you're concatenating TaskData.xml with the current directory. You want: if... more 2/20/2017 6:09:47 PM

people

How do I deserialise different NodaTime LocalDate patterns within the same JSON object

I'm trying to use NodaTime to interpret dates retrieved from a third-party API. The dates come in an annoying array of formats in the same response, one that I'm particularly...
Jon Skeet
people
quotationmark

This does indeed seem to be a use case we hadn't considered. For "normal" usage, sealing NodaPatternConverter feels like the right approach - but when a JsonConverter has to be specified by type rather than instantiated, the sealing is... more 2/20/2017 6:02:50 PM

people

Why addLast() not working properly

I created a DeQueue as follws Deque<Integer> deque = new ArrayDeque<Integer>(8); // use add() method to add elements in the...
Jon Skeet
people
quotationmark

addLast doesn't somehow maintain a notion of "this should be last" forever - it just adds to the end of the current dequeue. It's equivalent to add, as documented. So if we look at the values at each step, we have: deque.add(20); //... more 2/19/2017 9:37:59 AM

people

JAVA right bit shift return wrond value

I try to use right binary shifting in JAVA. Please look in code below: int val = 180000; val >>>= 8; // val == 703, ok val >>>= 8; // val == 2 , lok val...
Jon Skeet
people
quotationmark

Why last operation in second example compute wrong value? The value is computed according to the specification. It's your expectations that are wrong. From JLS 15.19: If the promoted type of the left-hand operand is int, then only... more 2/19/2017 9:19:41 AM

people

Getting the actual this reference to the task state machine

I'm playing with the idea of tampering with the state of a tasks internal state machine, but i'm having trouble finding a way to actually access the state machine reference within...
Jon Skeet
people
quotationmark

It's nasty, and it's not guaranteed to work (it depends on implementation details) - but this works for me... it basically provokes the state machine to pass a continuation to an awaiter. We can then get the state machine out of the... more 2/17/2017 2:57:44 PM

people

What is the "Name=value" in [MyAttribute(Name=value)]

I don't know what phrasing to use to google this. Consider this attribute: [MyAttribute(MyOption=true,OtherOption=false)] What is the Name=value part? And how can I implement...
Jon Skeet
people
quotationmark

It's specifying a property when creating an instance of the attribute. Attributes can have constructor parameters and properties - this one is setting a property. Note that you can mix positional constructor arguments, named constructor... more 2/17/2017 12:06:30 PM

people

C# default parameter by name is this possible?

May be this is a stupid question but: I wonder if there is something like default parameter but not by value - but by name. Example: I must use a parameter "IWebDriver driver"...
Jon Skeet
people
quotationmark

No, there's no way of doing that. Default parameters have to have constant values - they can't depend on a value taken from a local variable. It sounds like you should probably construct an instance which stores a driver reference in a... more 2/17/2017 11:58:08 AM

people