Browsing 7239 questions and answers with Jon Skeet

Image to byte array inconsistent

I'm currently trying to convert a JPEG image from the local java project directory to a byte array to send over a tcp connection. This is currently how I'm converting my file to...
Jon Skeet
people
quotationmark

You're calling toString on a byte[]. Arrays don't override toString(), so you're seeing the implementation in Object: The toString method for class Object returns a string consisting of the name of the class of which the object is an... more 11/4/2013 4:37:14 PM

people

Why Does an Array Cast as IEnumerable Ignore Deferred Execution?

I ran across this issue today and I'm not understanding what's going on: enum Foo { Zero, One, Two } void Main() { IEnumerable<Foo> a = new Foo[]{...
Jon Skeet
people
quotationmark

It's because of an optimization which is unfortunately slightly broken in the face of unexpected CLR conversions. At the CLR level, there's a reference conversion from a Foo[] to int[] - you don't actually need to cast each object at all.... more 11/4/2013 4:28:57 PM

people

Binary Search w/o Library Methods

I am trying to construct a binarySearch method that goes through a sorted array, and evaluates whether a given element, given as int target, is present. It will do so by...
Jon Skeet
people
quotationmark

This is a bad start: for (int i = 0; i < arrayBeingSearched.length; i++) That's a linear search, with something else within it. I haven't followed exactly what you're doing, but I think you should probably start again... with a... more 11/4/2013 4:18:50 PM

people

Java Interfaces, methods need to return something?

(1st post don't bully me :D) My question is simple, is it imperative that a method included in an interface HAS to return some value? (int, double, String etc..) Cause last time...
Jon Skeet
people
quotationmark

My question is simple, is it imperative that a method included in an interface HAS to return some value? No, absolutely not. You can declare a void method in an interface, and indeed there are plenty of standard library interfaces... more 11/4/2013 11:04:30 AM

people

Name of field or property being initialized in an object initializer must stat with '.'

I have converted some C# code to VB.net C# code: private static List<Hotels> LoadData() { List<Hotels> lst = new List<Hotels>(); DataTable...
Jon Skeet
people
quotationmark

You only use Key with anonymous types - not object initializers. So your VB code should be: lst.Add(New Makes() With { _ .Id = Convert.ToInt32(dr("ID")), _ .MakeName = Convert.ToString(dr("CHASSIS_NO")), _ .Model =... more 11/4/2013 10:44:26 AM

people

How Threadpool re use Threads and how it works

My Multithreading concepts are weak and trying to learn. In java what i know is, we can't call a Thread more then once i.e Thread t = new Thread(//Some...
Jon Skeet
people
quotationmark

If there is no need to create new Thread in ThreadPool scenario, then how it works with same thread which just finished its run method, will that Thread can be used again? Simple - the original thread never actually completes. It just... more 11/4/2013 10:37:54 AM

people

ObservableCollection's IEnumerable ctor finalizes collection, but the IList ctor does not

In the following method, when I copy the IEnumerable result to a list with ToList(), no finalizers are called at the ObservableCollection's IList constructor, and the IDisposable...
Jon Skeet
people
quotationmark

In the second case, you're actually evaluating the query twice. Once when you use foreach, and then another time when you create the ObservableCollection. You'll end up with two distinct sets of objects, which you could validate by adding... more 11/4/2013 7:02:09 AM

people

Could finally block get skipped due to GC in a threaded situation?

Say that (possible on a separate thread) I am running some method ClassA.foobar(). Inside that method is a try, (possibly catch), finally block. Now if the last reference to this...
Jon Skeet
people
quotationmark

In other words: is the finally block still guaranteed to run even if there are no strong references to the object(/thread) left in memory? Yes. finally blocks don't get skipped like that - nor does any other code. Execution threads... more 11/4/2013 6:43:07 AM

people

Change struct values from a different class

I am trying to change a struct values (located in class A) from another class (class B per say) I wrote a method to get the struct (the method is located in class A) but all I get...
Jon Skeet
people
quotationmark

Yes, that's what happens with structs. You need to make the changes locally, then shallow copy back again. For example: public class Foo { public Point Location { get; set; } } public class Bar { private Foo foo = new Foo(); ... more 11/3/2013 9:23:08 AM

people

How to override ToString() in [] array?

Say, if I need to override ToString method in a custom List, I'd do this: public class WebUILanguage2 : List<WebUILanguage> { public override string ToString() { ...
Jon Skeet
people
quotationmark

You can't. You can't derive from array types. I'd generally advise against overriding ToString in List<T>, too - usually it's better to use composition than inheritance for things like this, in my experience. more 11/2/2013 8:52:07 PM

people