Browsing 7239 questions and answers with Jon Skeet

Execution of C# Lambda expressions based on async annotations

I am trying to find an elegant implementation of the Execute(..) method below that takes in a lambda expression. Is what I'm trying to do even possible? It seems like I should be...
Jon Skeet
people
quotationmark

You could check whether the method underlying the delegate you've been passed is annotated with the AsyncStateMachineAttribute - but to be honest, I wouldn't. It's just asking for trouble, using an implementation detail like... more 10/17/2013 8:55:16 PM

people

Can I make a function accepting generic functions as a parameter?

Say I'm benchmarking a bunch of different functions, and I just want to call one function to run function foo n times. When all the functions have the same return type, you can...
Jon Skeet
people
quotationmark

You can make it generic, certainly: static void Benchmark<T>(Func<T> function, int iterations) You might also want to overload it to accept Action, for void methods. more 10/17/2013 8:09:28 PM

people

Running java in package from command line

I have read the previously posted questions. Some are vague and none solved my problem so I am forced to ask again. I have two simple classes, package One; import...
Jon Skeet
people
quotationmark

You'd run it as: java One.Test ... but from the root directory (basic), not from the One directory. You always specify the fully-qualified class name. Oh, and package names in Java should be lower-case, so it should be one and... more 10/17/2013 5:29:43 PM

people

How to pass code blocks (not full methods) as arguments in C#?

I'm building a messaging app in csharp (.net 4.0), my class has basic methods for sending/receiving messages: void sendMessage( string msgBody, string properties); object...
Jon Skeet
people
quotationmark

You're nearly there at the end - you just need to enclose the lambda expression in () as it's a method argument. You also need to use the return value from makeAttempt to provide a return value for your getNextMessage method. So: public... more 10/17/2013 5:02:04 PM

people

Read text file with lots of line in C#

I have a text file, that could potentially have up to 1 Million lines in it, and I have a code for reading the file one line at a time, but this is taking a lot of time...lots and...
Jon Skeet
people
quotationmark

Well, this code would be simpler, if you're using .NET 4 or later you can use File.ReadLines: foreach (var line in File.ReadLines()) { // Do something } Note that this is not the same as ReadAllLines, as ReadLines returns an... more 10/17/2013 4:27:33 PM

people

Exception inside using initialization

I read many threads about the using keyword in C#, but I couldn't find anyone with the same question. Reading this interesting article, it says that the using statement is...
Jon Skeet
people
quotationmark

Well if the constructor fails, the exception will be thrown instead of the reference being returned - so the calling code has nothing to dispose. Basically, constructors need to be careful. If an exception is thrown in an exception, the... more 10/17/2013 4:25:14 PM

people

Avoiding memory inefficiency when overring toString() of a common POJO using StringBuilder

I'm developing a project that all my POJOs must have they toString() inherited from Object class overridden. Consider the immutable class below: public final class SomeActivity...
Jon Skeet
people
quotationmark

As far I know, String is a immutable class. (refer to the javadoc), so, if I implement a method to receive such output, I may have many objects being created because of my concatenations: Nope. As you're performing all the... more 10/17/2013 3:09:42 PM

people

How should parameter to function be given when AddressOf used?

See the following code: Public Sub MyFunction1(ByVal CodeNo As Integer) Dim thread As New Thread(AddressOf MyFunction2) thread.Start() End Sub Private Sub...
Jon Skeet
people
quotationmark

It's compiling because you haven't got Option Strict on. If you turn on Option Strict (which IMO you should pretty much always do) it won't compile - your function isn't compatible with either ThreadStart or ParameterizedThreadStart. If... more 10/17/2013 3:07:58 PM

people

Trouble in fetching results from server in windows phone

I'm communicating with my server with the following code, private void Save_Click(object sender, RoutedEventArgs e) { var request = HttpWebRequest.Create(url) as...
Jon Skeet
people
quotationmark

Well presumably the HTTP status code is 404. You're already accessing the response in your error code though - all you need to do is try to parse it as JSON, just as you are in the success case, instead of using we.Message to show an error... more 10/17/2013 2:33:52 PM

people

HttpWebRequest method GET/POST not working?

I am working with GoogleApi. I want to get accesstoken as response using Google api. when I am sending httpwebrequest for getting access token then When I used :- request.Method...
Jon Skeet
people
quotationmark

This is the problem: var request = (HttpWebRequest)WebRequest.Create(@"https://accounts.google.com"); That's not the URL you showed originally. That's just the domain root. You need: var request =... more 10/17/2013 1:26:26 PM

people