Browsing 7239 questions and answers with Jon Skeet

Why don't threads run consistently?

I am playing around with multithreading and came across an inconsistency when running a small snippet of code. The following code should print out 123123... but what I'm getting...
Jon Skeet
people
quotationmark

The following code should print out 123123 Not necessarily. You should basically never rely on threads with no synchronization between them happening to wake up and execute in any particular order. Let's take the very first character... more 6/11/2015 1:06:20 PM

people

Set attribute name property from static data in C#

How can I set an attribute name property from static data in C#: [FaraAuthorize(Roles = RoleValues.SiteManager)] // Error here public class ProjectsController :...
Jon Skeet
people
quotationmark

You can't, basically. Attribute values are embedded directly in the compiled assembly - they can't be determined at execution time. If you need information which is only available at execution time, you'll need a different approach which... more 6/11/2015 8:48:36 AM

people

Sane pattern for immutable pojos and single field changes

Usually I'd love all my POJOs to be immutable (well, to contain only final fields as Java understands immutability). But with my current project, a constant pattern is that I need...
Jon Skeet
people
quotationmark

Yes, that's a fairly common pattern - usually with a bunch of methods with a with prefix. Each with* method "changes" a single field, so you can have: Person jon = new Person("Jon", "Skeet"); Person holly = jon.withFirstName("Holly"); //... more 6/10/2015 5:05:22 PM

people

how to get random rows in linq with last inserted row on top

i am trying to show random products in view for each request, this OrderBy(r => Guid.NewGuid()) works fine, but i am trying to increase perfomance when table records are huge ,...
Jon Skeet
people
quotationmark

Okay, so the updated requirements are: Fetch all items Ordering is random other than the first item, which should be the last one added Firstly, get rid of your Skip call. You're not trying to skip anything. Just fetch all the products... more 6/10/2015 10:44:49 AM

people

Method name expected

I have to create a json file with an array inside in c# so this is my code but throws an exception: Method name expected before new Dictionary<string, int>[2](); Have...
Jon Skeet
people
quotationmark

This isn't how you construct an array; new Dictionary<string, int>[2]() You don't need the () at all - you're not calling a method or a constructor; you're creating an array instance. Just: new Dictionary<string,... more 6/10/2015 10:22:41 AM

people

Why Exception in thread "main" java.lang.NoSuchMethodError: main?

Well as I researched it seems to be a common error, but through the information I collected, it is mainly caused by the misbehaviour of main method, while in here I inspected...
Jon Skeet
people
quotationmark

Your main method has the wrong signature. To be an entry point for a Java application, it must have a single parameter of type String[] (and a void return type, and it must be public and static): public static void main(String[]... more 6/10/2015 9:36:24 AM

people

Adding array list to another arraylist

I am trying to add array list in another array list, and returning final array list. I have 4 records in table, when i am trying to display, the first set of record only 4 coming...
Jon Skeet
people
quotationmark

You're creating a single ArrayList<String>, and adding a reference to that list to your ArrayList<ArrayList<String>> several times. You need to create a new object within your loop, so that each iteration creates an... more 6/10/2015 9:30:42 AM

people

why escape sequence can't be represented as unicodeEscape in java?

In java, "Carriage return" is represented as '\r' & "Line Feed" is represented as '\n'. But Java does not allow, "Carriage return" as '\u000d' and "Line Feed" as...
Jon Skeet
people
quotationmark

The Unicode escape sequences are applied earlier in the source transformation than the character literal escape sequences. Unicode escape sequences are transformed very early in the process - before any other lexing happens, including... more 6/10/2015 8:16:07 AM

people

Does java cache array length calculation in loops

Lets say that i have an array which i would like to iterate over: int[] someArray = {1,2,3,4} for (int i = 0; i < someArray.length; i++) { // do stuff } Will this...
Jon Skeet
people
quotationmark

As always for performance: write the simplest code you can, and test it to see whether it performs well enough. If you only need the element (and not the index) I would encourage you to use the enhanced-for loop: for (int value : array)... more 6/10/2015 6:21:57 AM

people

Why should I use Result in Tasks C#?

I've seen that when you use Task Class like this: Task<Test> task2 = Task<Test>.Factory.StartNew(() => { string s = ".NET"; ...
Jon Skeet
people
quotationmark

Any way to not wait and still get result? No, because there isn't a result until the task has completed. The point is that you can maintain a reference to the task, perform some other operations, and then get the result. Note that... more 6/9/2015 7:52:48 PM

people