Browsing 7239 questions and answers with Jon Skeet

Property exists in debugger but not when compiling

Context: I need to check if a window was disposed before it is shown (if some other code called App.Shutdown). The solution given in How do you tell if a WPF Window is closed?...
Jon Skeet
people
quotationmark

Question: how can it be that a Property is accessible in the debugger but not in the code? Because the debugger can show private, internal and protected members that your code doesn't have access to, basically. You can see this for... more 12/11/2015 10:21:15 AM

people

Java String immutable

I was thinking about string in java. String are immutable. But when the code is mutable String str = new...
Jon Skeet
people
quotationmark

No, the output is what it is precisely because strings are immutable. Calling toUpperCase() doesn't change the contents of the existing string, it creates a new one and returns a reference to that... it has to do that, due to strings being... more 12/11/2015 10:04:58 AM

people

InvalidOperationException: Sequence contains more than one element SingleOrDefault()

using (var db = new ABC()) { for (int column = rangeClass2.Start.Column; column <= rangeClass2.End.Column; column++) { var classValue = censusSheet.Cells[row,...
Jon Skeet
people
quotationmark

But if I want to add multiple records in my list once then what is the solution? You mean you want to add all the results? Assuming that lifeReductionByData is a List<T> for the appropriate type, you can just use... more 12/10/2015 9:33:48 PM

people

Why am I seeing a Cross thread exception when invoking EventHandler.BeginInvoke through Delegate.Target?

I am attempting to write an extension method which will simplify cross-thread event handling. The below is what I have conceived and by my understanding it should work; however I...
Jon Skeet
people
quotationmark

You're calling the delegate on the right thread - but the delegate itself then calls evnt.BeginInvoke, which executes the evnt delegate on the thread pool... so you still end up executing the real underlying delegate (in this case _Test,... more 12/10/2015 7:26:49 PM

people

Getting error while parsing string to datetime?

Getting error while parsing string to datetime. string datestring = "111815"; DateTime date = Convert.ToDateTime(datestring); I also tried using, Parse, ExactParse...
Jon Skeet
people
quotationmark

You just need to specify the right format string when you call ParseExact. In your case, it looks like this is month-day-year, without any separators, and with a 2-digit year (blech). So you'd parse it like this: using System; using... more 12/10/2015 7:19:28 PM

people

" +" operator c# MONO 2.10.12

I just found very strange thing in my code. By mistake I have put as an index something like this: int a = 1; int b = 1; Dictionary<int, SomeClass> dic = new...
Jon Skeet
people
quotationmark

There's nothing strange about this, and there isn't a -+ operator. There's a unary + operator and a binary - operator. Just add parentheses and some spacing to make it clearer: int a = 1; int b = 1; int c = a -+ b; int d = a - (+b); //... more 12/10/2015 5:34:12 PM

people

List broken or pebkac?

Either the Generic List (and HashSet) is broken or ... pebkac This unworthy test-program outputs the following: System.Byte[] 6:2 3 4 5 6 7 True // as...
Jon Skeet
people
quotationmark

Nope, nothing's broken - but arrays don't override Equals or GetHashCode. So a HashSet<byte[]> is just checking for the presence of the exact object that the key refers to. It's not looking for "an array with an equivalent sequence... more 12/10/2015 3:13:22 PM

people

Thread.Sleep in Asp.net vNext

I need to make a call to a JSON api which only allows a limited number of requests per second to ensure no DDOS is happening from a specific IP. It doesn't matter how long the...
Jon Skeet
people
quotationmark

You're missing a dependency. If your code is not async, you shouldn't use Task.Delay... you should use Thread.Sleep() or normal. Just add the following dependency: "System.Threading.Thread": "4.0.0-beta-23516" Then you can use... more 12/10/2015 3:08:44 PM

people

Set value to mocked object but get null

I have a simple class Foo to be mocked: public class Foo { private String name; public Foo() { } public Foo(String name) { this.name = name; } public void...
Jon Skeet
people
quotationmark

Well yes - the actual code of Foo doesn't matter, because you're mocking it... and Mockito doesn't know there's meant to be a relationship between setName and getName. It doesn't assume that it should store the argument to setName and... more 12/10/2015 3:03:25 PM

people

Converting a string to a DateTime with more than 7 decimals of milliseconds

So I am trying to convert a string date in the following format to a DateTime. I am able to parse it using ParseExact when there are 7 decimals of fractions of a second using the...
Jon Skeet
people
quotationmark

I don't believe you can do this with the normal parsing code and the existing text. DateTime's precision only goes down to ticks, where a tick is 100 nanoseconds. I think the simplest thing to do is truncate the string itself: string... more 12/10/2015 2:48:11 PM

people