Browsing 7239 questions and answers with Jon Skeet

Task continue with not working upon exception

I am in a strange situation, I am using task and continue with upon faulted i am calling one method to process upon faulted or success but the function does not get fired. Below...
Jon Skeet
people
quotationmark

You're trying to use t1.Result within your continuation. If t1 has faulted, then accessing t1.Result will itself throw an exception. You either need a single continuation which can handle any kind of end result, or you should attach... more 6/12/2014 9:15:36 AM

people

How do you access the number of times a delegate has been called?

I am currently writing a test for a piece of functionality. I need to count how many times an event handler has been called. In this example I want to ensure the delegate...
Jon Skeet
people
quotationmark

No, there's no standard property for that... but you can easily use the fact that a lambda expression is a closure to emulate it: int callCount = 0; Action handler = () => callCount++; _testObj.TaskCompletedForItems += handler; // Do... more 6/12/2014 8:55:31 AM

people

This code seems to return all rows

match match = myRepo.GetAll() .Where(m => m.personId == personId) .Where(m => m.companyId == companyId).FirstOrDefault(); now there are over 1m records and it...
Jon Skeet
people
quotationmark

The fact that your GetAll method returns an IEnumerable<T> (rather than IQueryable<T>) means that your Where call is using LINQ to Objects, not EF. In other words, that query is fetching all the data locally, and filtering it... more 6/12/2014 8:16:53 AM

people

Parse JSON of two dimensional array in JAVA

Problem is, my JSON string looks like this: jsonString = [["1","100"],["2","200"],["3","300"]] I need to make a two dimensional array out of it in Java. If I write...
Jon Skeet
people
quotationmark

The JSON you've got is for an array, not an object. You probably want JSONArray array = new JSONArray(jsonString); Full sample code: import org.json.*; public class Test { public static void main(String[] args) { String... more 6/12/2014 7:56:14 AM

people

Why does serializing Integer take so many (81) bytes?

I wrote a small test program to show how many bytes we need to serialize Integer object: ByteArrayOutputStream data = new ByteArrayOutputStream(); try { ...
Jon Skeet
people
quotationmark

It's a balancing act, between making the serialization protocol more complicated by having direct support for lots of types, and between keeping it simple. In my experience, Integer values are relatively rare compared with int values -... more 6/12/2014 7:12:44 AM

people

.NET WebClient.DownloadData get file type?

In order to handle cases of downloading data from a url that has no file extension, I need to know what the file type is. for example, how can the WebClient.DownloadData method...
Jon Skeet
people
quotationmark

It can't, directly. If you trust the headers the web server sends back, you could use a different HTTP client (e.g. WebRequest or HttpClient) to make the entire response available rather than just the body. You can then look at the... more 6/12/2014 6:09:11 AM

people

Using statement with unknown implementation?

Can the following code be translated into using the... ejem... 'using' statement? TextReader TheReader; if( UploadedFile ) { TheReader = new System.IO.StreamReader( LocalFileName...
Jon Skeet
people
quotationmark

Well you could use the conditional operator: // Variable names changed for convention, and fully-qualified names simplified using (TextReader reader = uploadedFile ? File.OpenText(localFileName) :... more 6/11/2014 10:28:42 PM

people

Why does undefined Generics types of Collections defaults to Object even if bound to other class?

class Test<G extends String>{ public G test(){return null;} public List<G> tests(){return new ArrayList<>();} } public void doTest(Test t){ //works...
Jon Skeet
people
quotationmark

No, the type as been erased. You're running into two different rules for type erasure - from JLS section 4.6: The erasure of a parameterized type (§4.5) G is |G|. The erasure of a type variable (§4.4) is the erasure of its... more 6/11/2014 7:57:17 PM

people

Why am I not getting the actual string value with this byte array conversion?

I am trying to send a "getvar" command to a printer over the serial port, and receive the response, but all I'm getting when trying to verify what I'm sending, and what is...
Jon Skeet
people
quotationmark

If you want to see the bytes in hex form, just use BitConverter.ToString(byte[]). That will give you output such as "01-56-AF". If you want the text - well, you've got that already as cmd - but in general, to convert a byte[] to the text... more 6/11/2014 7:30:35 PM

people

I found a class that relies on a method that is defined in an extension of it. Is this a pattern?

I found a class that looks something like: using System; // no using statement that brings either IMyClass or // MyClassExtensions into scope namespace Org.Foo { class...
Jon Skeet
people
quotationmark

Thus, MyClassExtensions is clearly out of scope when MyClass is defined Nope, both MyClassExtensions and MyClass are declared in the same namespace. You don't need a using directive to invoke an extension method in the same namespace... more 6/11/2014 6:48:34 PM

people