Browsing 7239 questions and answers with Jon Skeet

Java downcast in clone

Lets say that Class B extends class A and class A is Cloneable as follows: public class A implements Cloneable { public Object clone() throws CloneNotSupportedException { ...
Jon Skeet
people
quotationmark

Ultimately, this is using Object.clone() to create the object - and that's guaranteed to create a new object of the same execution-time type as the object it's called on: The method clone for class Object performs a specific cloning... more 9/27/2013 9:27:15 AM

people

Are elements in a .NET's Dictionary sequential?

Say, if I create a dictionary like this: Dictionary<string, MyClass> dic = new Dictionary<string, MyClass>(); dic.add("z1", val1); dic.add("abc9",...
Jon Skeet
people
quotationmark

Am I guaranteed to have these values retrieved as such: "z1", "abc9", "abc8", "ABC1"? Absolutely not. Always treat Dictionary<,> as an unordered collection of key/value pairs. Even though as an implementation detail you'll... more 9/26/2013 9:05:22 PM

people

Aborting the thread from inside the same thread

I created a thread A and decided to abort it from inside the thread A. Is that possible? If so how can I do that? Thanks for the help!
Jon Skeet
people
quotationmark

Sure, you can do: Thread.CurrentThread.Abort(); Arguably aborting the current thread is the only kind of abort that's safe, as it's the only thread you really know about in terms of what it's doing. This is what HttpResponse.End does,... more 9/26/2013 7:50:10 PM

people

static variable vs non static variable

I have defined an object and declared a static variable i. In the get() method, when I try to print the instance and class variable, both print the same value. Isn't this.i an...
Jon Skeet
people
quotationmark

No, there's only one variable - you haven't declared any instance variables. Unfortunately, Java lets you access static members as if you were accessing it via a reference of the relevant type. It's a design flaw IMO, and some IDEs (e.g.... more 9/26/2013 6:31:41 PM

people

BeginInvoke copy parameter

Just try to learn Invoke/BeginInvoke, I encountered in that problem. // Update UI public void UpdateForm(string value) { txtLog.AppendText(value + "\r\n"); ...
Jon Skeet
people
quotationmark

maybe "row" can be behavior like a shared variable rather than Yes, the row variable is captured - so whenever it's read, it's the latest value that's read. You should put the variable declaration inside the loop: while... more 9/26/2013 6:19:40 PM

people

c# Is it possible get a reference of an object, get the object itself and change it, instead of assinging to a new object?

I'm just curious if something like that is possible in c#. I don't know why would anybody want to do it, but it's still interesting if that can be done.: public class Test { ...
Jon Skeet
people
quotationmark

If you mean "can I change the value of a to refer to a different object, simply by changing the value of b?" then the answer is no. It's important to understand that the value of a variable is never an object - always either a value type... more 9/26/2013 6:12:30 PM

people

What is the correct word for a type shorthand in .NET?

For example: 100M is decimal 100l is long I wanted to find one for a unit, so I don't have to type (uint) 123 all the time and I realised I didn't know what word to search for.
Jon Skeet
people
quotationmark

If you're just talking about terminology, literal is a value specified directly in source, such as: 10 10M 100L "this is a regular string literal" @"this is a verbatim string literal" '\u000' '\n' 'X' A numeric... more 9/26/2013 4:00:33 PM

people

Check an object is not null before iterating over it

I'm getting an error System.ArgumentNullException: Value cannot be null. on the following: foreach (var pmt in payment.NewInvoiceViewModels .Where(x => x.PaymentReceived...
Jon Skeet
people
quotationmark

You've got the null-coalescing operator in the wrong place - currently you're calling Where on payment.NewInvoiceModels unconditionally and then checking whether the result will be null... it never will be (Where simply doesn't return... more 9/26/2013 1:38:43 PM

people

How to make this unit test independent?

One of unit test best practices is to make each test independent to all the others. Lets say I want to test add() method of a BoundedPriorityBlockingQueue custom class: public...
Jon Skeet
people
quotationmark

What is the best practice in this situation? Just suck it up, bearing in mind that tests are meant to serve you, not the other way round. Will your tests break if something goes horribly wrong? Yes. Will it be clear where the... more 9/26/2013 12:44:27 PM

people

Calculate difference between two dates returns a negative number

I am trying to calculate how many days there are between different dates. Like the topic says, I'm getting a negative value. I guess I could just take the absolute value, but the...
Jon Skeet
people
quotationmark

I guess I could just take the absolute value, but the fact that it returns a negative value, makes me doubt if the calculations are correct. Why? nextDay is earlier than previousDay, so of course subtracting previousDay will give a... more 9/26/2013 12:38:46 PM

people