Browsing 7239 questions and answers with Jon Skeet

About Dictionary

Can I make sure that in dictionary in C# there will be only a single of a specific value in it? For example, if I define a Dictionary which key is char and value is char, can I...
Jon Skeet
people
quotationmark

Can I make sure that in dictionary in C# there will be only a single of a specific value in it? Not as such (it wouldn't follow the the normal dictionary contract at that point), but it sounds like you effectively want a... more 3/3/2014 7:59:46 PM

people

Difference between putting code at the end of a Task that is awaited versus putting code after the await:

Should this block of code... async void SomeMethodAsync() { this.IsDoingLongRunningWork = true; await Task.Run(() => { DoLongRunningWork(); ...
Jon Skeet
people
quotationmark

Well they may well be executed in different threads, for one thing. If IsDoingLongRunningWork affects a user interface (for example) then it should probably only be changed in the UI thread, in which case the first code is incorrect (the... more 3/3/2014 3:08:27 PM

people

Is there a VB.NET expression that *always* yields null?

We all know that VB's Nothing is similar, but not equivalent, to C#'s null. (If you are not aware of that, have a look at this answer first.) Just out of curiosity, I'd like to...
Jon Skeet
people
quotationmark

The first answer I gave missed some points, but this should do it: Dim o As Object = If(myBool, 5, DirectCast(Nullable.GetUnderlyingType(GetType(Integer)), Object)) This uses the fact that Nullable.GetUnderlyingType will return a null... more 3/3/2014 1:17:55 PM

people

Retrieving property type from CodeProperty in T4 template

I'm implementing some fairly straight forward code generation using T4, but I'm stuck on a basic problem when it comes to the details of Property generation. When I access the...
Jon Skeet
people
quotationmark

Looking at the docs, I suspect you want AsString instead of ToString(). That would call CodeTypeRef.AsString: AsString return a string representation for the CodeTypeRef in the language being modeled. For example, if the vsCMTypeRef... more 3/3/2014 7:23:00 AM

people

Portable Class library object serialization

I am working on Xamarin iOS application. I also want to develop the same application for Android as well. So I am planning to write most of the code in a portable class library...
Jon Skeet
people
quotationmark

I would strongly suggest not using the built-in binary serialization even if you could... and it sounds like basically can't anyway. There are plenty of alternatives for serialization, depending on your requirements. You could... more 3/3/2014 6:47:53 AM

people

NiftyGUI's Nifty class cant instantiate?

package test; import java.io.IOException; import de.lessvoid.nifty.Nifty; import de.lessvoid.nifty.examples.LwjglInitHelper; import...
Jon Skeet
people
quotationmark

After doing a bit of research, it looks like you're missing the eventbus jar file. You can download the jar file from the Maven repository. Note that I didn't know anything about this class beforehand - it was merely a matter of looking... more 3/2/2014 7:06:12 PM

people

DLL out int not accepted

I have built a DLL in C#. In the following function the IDE tells my that the function _api.Version has some invalid arguments. But I don't think that this is true. public...
Jon Skeet
people
quotationmark

In C#, you need to specify out and ref modifiers at the call site as well as in the member declaration. That means that anyone reading the code is aware of what is going on. (This is a big boost to readability, in my view.) So you... more 3/2/2014 6:05:04 PM

people

Java: Is the line args[0].length possible?

I am wondering if the java line if (args[0].length > 2) is a valid line. I'm taking in command-line arguments, and I want to find out if the amount of numbers in the first...
Jon Skeet
people
quotationmark

It's a valid line if args is declared as a String[][] - but that wouldn't be how a main method would be declared. length is a valid member of an array, but it's not a (public) member of the String class. If you're trying to check the... more 3/2/2014 5:32:20 PM

people

How to mock Calendar.getInstance method using JMockit?

I am trying to mock Calendar.getInstance() method using JMockit. This is the first time I am mocking anything. Below is my method which gives me the date of either recent Monday...
Jon Skeet
people
quotationmark

Rather than try to mock a static method here, I would strongly recommend introducing a Clock interface along these lines: public interface Clock { Date now(); // Or long, or Instant if you're using Joda Time } Then you can have a... more 3/2/2014 5:01:27 PM

people

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

On my website I am trying to return a list which shows the users that the current user is following but I keep on recieving the error Cannot implicitly convert type...
Jon Skeet
people
quotationmark

You're calling ToList for no obvious reason. It would also make things clearer if you moved the query out of the return statement. Breaking your code down, you've effectively got: // We're not certain what type `db.Followusers` is, to be... more 3/1/2014 9:20:58 PM

people