Browsing 7239 questions and answers with Jon Skeet

Alternative way convert int ? to double?

Is there any way to convert this ? public int? intVal ; public double? dblVal ; How I am working now if(dblVal==null) intVal =null; else intVal =...
Jon Skeet
people
quotationmark

Just cast: intVal = (int?) dblVal; This will already result in a null value if dblVal is null. Note that unlike Convert.ToInt32(double), this does not result in an exception if dblVal is outside the range of int. If that's a concern,... more 10/7/2015 6:06:57 PM

people

Why Dictionary "does not contain a definition for 'ElementAt'"?

Why Dictionary "does not contain a definition for 'ElementAt'" during dynamic programming Dictionary<string, dynamic> D1 = new Dictionary<string, dynamic>(); ...
Jon Skeet
people
quotationmark

There are two things wrong here: You're assuming that the second entry you added to D2 is the one retrieved by D2.ElementAt(1). Don't make that assumption: dictionaries are basically unordered. You're calling an extension method... more 10/7/2015 4:37:37 PM

people

Does Visual Studio compile test classes into an assembly?

Does Visual Studio compile classes marked with test attributes into an assembly? Suppose this assembly has internal classes that could benefit from unit test coverage.
Jon Skeet
people
quotationmark

Assuming you've got a separate test project, yes that's really just another class library project. (If you're expecting VS to split classes from a single project into "test" and "non-test" assemblies, then the answer is no... and you... more 10/7/2015 2:49:03 PM

people

Resolve the linq expression error

I've a code like given below, but it gives a compile error "Cannot convert expression type 'System.Collections.Generic.List' to return type 'bool' List<Condition> cod =...
Jon Skeet
people
quotationmark

Firstly, it's not clear why you're calling ToList all over the place. At the end, maybe... but no need for it any earlier. I think you're probably looking for the Any method: List<Condition> cod = MyRules.Where(r =>... more 10/7/2015 2:25:36 PM

people

C#: Update Item value in List<of T>

I'm looking for a way to update an Element in a List without enumerating it on my own. I got the Class MyProjects which hold a List named Projects. I want to find the...
Jon Skeet
people
quotationmark

You shouldn't try to get everything down to one line - just as brief as is readable. In this case, you can use: foreach (var project in MyProjects.Projects.Where(p => p.Name == "Overhead")) { project.IsActive = true; } That's... more 10/7/2015 1:34:07 PM

people

Any() linq query on XmlNodeList

I am not able to use Any() on XmlNodeList. I have also used System.Linq and System.Xml.Linq namespaces as well. But still I'm not finding any such extension methods on...
Jon Skeet
people
quotationmark

The problem is that XmlNodeList only implements IEnumerable, not IEnumerable<T>. The simplest way to use LINQ on it is to call Cast: var query = nodeList.Cast<XmlNode>() .Where(...) ... more 10/7/2015 9:59:19 AM

people

Java main argument identified as main class

I have a problem running a Java process with arguments as a command from another Java manager process. Say I have a main class Main, and I want to pass 0 as an argument (for...
Jon Skeet
people
quotationmark

Well you're passing the whole of "-X... -D... -cp ... Main" as a single argument. Instead, you should have: Process p = Runtime.getRuntime().exec(new string[] { "cmd.exe", "/c", "javaw.exe", "-X...", "-D...", "-cp", "...", "Main",... more 10/7/2015 9:46:38 AM

people

Java Creating a date in PST returns a date in PDT

public class TestTimeZone { public static void main(String[] args) throws ParseException { System.setProperty("user.timezone", "PST"); TimeZone.setDefault(null); ...
Jon Skeet
people
quotationmark

d1 and d2 are clearly different, so their formats shouldn't be same Well they wouldn't be if you had an unambiguous format - but you don't. (Heck, even leaving time zones aside you're only formatting to the hour, so it's trivial to... more 10/7/2015 9:14:36 AM

people

Read multiple lines using BufferedReader (Socket)

I already read some threads here on stackoverflow, also some tutorials, but I don't find a solution to my problem. I have Java client which connects to a server, then sends...
Jon Skeet
people
quotationmark

Presumably your server isn't closing the connection - therefore the underlying stream for the reader isn't closed... at any point the server could send more information. readLine() only returns null when the stream has been closed, i.e.... more 10/7/2015 7:59:20 AM

people

Method not found: 'Nancy.ErrorPipeline Nancy.ErrorPipeline.op_Addition

We updated Nancy from v1.2.0 to 1.3.0 via NuGet. According to the changelog there appear to be no breaking changes relevant to our application. However we are now getting the...
Jon Skeet
people
quotationmark

(Caveat: I've never used Nancy. This is just a matter of looking at the history in Github.) It looks like the + operator signature you're using changed from this in 1.2.0: public static ErrorPipeline operator + (ErrorPipeline... more 10/6/2015 9:54:02 PM

people