Browsing 7239 questions and answers with Jon Skeet

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

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

How to copy image in java using bufferedreader/writer

File file = new File("download.png"); File newfile = new File("D:\\Java.png"); BufferedReader br=null; BufferedWriter bw=null; try { FileReader fr =...
Jon Skeet
people
quotationmark

Whats wrong with this code. You're using text-based classes for binary data. Is it possible with BufferedReader and Writer Class? Not while you're dealing with binary data, no. I know how to to make copy of image using... more 2/16/2017 3:57:55 PM

people

why does i.Parent.ReplaceWith(i) not throw an exception?

In these statements (running with MoreLinq): var xml = @" <div> <p> <h2>hey</h2> </p> <pre /> <h2 class=""cool"" /> <p> ...
Jon Skeet
people
quotationmark

You don't need MoreLINQ to demonstrate this at all - and you can simplify the sample code, too: using System; using System.Linq; using System.Xml.Linq; class Program { static void Main() { var element = new XElement( ... more 2/16/2017 9:15:54 AM

people

C# convert System.Func<Tderived, bool> to System/Func<Tbase, bool>

I've googled but couldn't find satisfactory answers. I'm basically trying to get this code to work: public List<WordEntry> WordDataBase = new...
Jon Skeet
people
quotationmark

Basically, you just need to cast - the language rules don't allow the compiler to take the if statement into account when it thinks about the types involved. Note that you also need to call ToList<Entry>(), specifying the type... more 2/15/2017 3:18:47 PM

people