Browsing 7239 questions and answers with Jon Skeet

Creating a Singleton class with builder capabilities

Consider the following hypothetical Singleton class DateSingleton which currently looks like the following: public class DateSingleton { private static DateSingleton instance...
Jon Skeet
people
quotationmark

I would separate "I only need one value for my whole web service" from "this class should be a singleton". The latter is one possible implementation, but it's certainly not the only one. I assume this is part of some configuration data... more 2/25/2015 10:38:26 AM

people

sql to linq using distinct

I have the following linq statement and cannot get it to work. the code is: (from ll in table1 join l in table2 on ll.LandlordId equals l.Id select((value, index) => new...
Jon Skeet
people
quotationmark

Your query expression is entirely broken at the moment - you're trying to use a mixture of query expressions and non-query-expressions. Additionally, constructing a SelectListItem and then using Distinct on that seems like a bad idea to... more 2/25/2015 10:29:01 AM

people

Can I use Format instead of using substring?

Using a memory profile on my C# desktop app I have found that strings are not being released from memory causing a slow and gradual buildup. I have this code: var...
Jon Skeet
people
quotationmark

My guess is that your real code has a value of something like 20150225071945 - so not actually the literal YYYYMMDDHHMMSS. If that's the case, I would parse the value as a DateTime rather than extracting substrings: DateTime dateTime =... more 2/25/2015 7:22:31 AM

people

Using xpath select how do I get the value of this element in example?

with this XML <?xml version="1.0" encoding="UTF-8"?> <createTransactionResponse xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"...
Jon Skeet
people
quotationmark

Your first problem is that the XML you originally gave isn't valid. You're masking that by returning "" whenever you encounter an exception, so you don't have any information any more. So the first thing to do IMO is remove the spurious... more 2/25/2015 7:13:36 AM

people

Sending ArrayList<integer>() into byte[] format thru USB in Android

My code uses a Modbus CRC16 protocol, which generates a List<Integer> with the hexadecimal values that must be written on the USB port. I'm trying to convert this...
Jon Skeet
people
quotationmark

It's not entirely clear what output you're expecting, but given your example code, I suspect you just want: byte[] bytes = new byte[list.size()]; for (int i = 0; i < list.size(); i++) { bytes[i] = (byte) list.get(i).intValue(); } more 2/25/2015 6:55:07 AM

people

Is Monitor.TryEnter(lockObject, timeout) overload unsafe? (.net)

I was suggested during a code review to do bool acquiredLock = false; try { Monitor.TryEnter(lockObject, 500, ref acquiredLock); if (acquiredLock) { // do...
Jon Skeet
people
quotationmark

Assuming in your second snippet you'd actually call Monitor.Exit, the difference is explained in the documentation: This overload always sets the value of the variable that is passed to the ref parameter lockTaken, even if the method... more 2/24/2015 6:41:12 PM

people

LINQ Outer Join Has Duplicates

I'm running this query in LINQPad. It works except that ProductSeries has duplicate records. var query = from etaRecord in EtaRecord_0140 join productSeriesRecord in...
Jon Skeet
people
quotationmark

The problem is your extra from clause: from productSeries in productSeriesGroup.DefaultIfEmpty() You should ditch that, and just use: let productSeries = productSeriesGroup.FirstOrDefault() ... or just use... more 2/24/2015 5:15:34 PM

people

How to check if Math.cos(angle) = 0

I am trying to find the correct domain for the tan function, I know that tan x = sinx / cos x and tan is undefined when cos x = 0. So I am trying to check if cos x is 0. if...
Jon Skeet
people
quotationmark

You need to broaden your expectations a bit - in order for Math.Cos(x) to be genuinely equal to 0, you'd either need inaccuracy in Cos (which will happen, of course) or for x to have an irrational value. Instead, you should work out some... more 2/24/2015 3:49:51 PM

people

How can I check for an InternalsVisibleTo attribute on an assembly?

I've used ILMerge to merge a secondary assembly that is itself merged with Castle.DynamicProxy, having set most if not all of the normally-public Castle types to internal. When I...
Jon Skeet
people
quotationmark

Assuming you can load the assembly itself, you can use Assembly.GetCustomAttributes: var asm = ...; var internals = asm.GetCustomAttributes(typeof(InternalsVisibleToAttribute), false); var... more 2/24/2015 3:45:37 PM

people

Force a C# method to pass only positive parameters?

I want to force my method to only accept an integer parameter if it is positive. If the method is passed a negative integer it should throw a compile time error. What is the best...
Jon Skeet
people
quotationmark

Just make the parameter a uint or a ushort. That will prevent negative input at compile-time. It won't prevent 0, however... I assume you're really after non-negative input rather than strictly positive input. If your method has to accept... more 2/24/2015 3:18:04 PM

people