Browsing 7239 questions and answers with Jon Skeet

LINQ Statement that parses multiple collection and gets a single List

I would like to know if there is a better/faster way to write the following LINQ Statement. First let me show you my class definition: public class FooA { public...
Jon Skeet
people
quotationmark

Well, in terms of efficiency that's creating a lot of extra objects for the sake of transparent identifiers in the translation from query expressions to SelectMany calls. You could convert it to: var fooDs = ListFooA.SelectMany(a =>... more 2/25/2014 11:46:18 AM

people

List thread safe?

Can the following be considered thread safe due to the atomic operation appearance of the code. My main concern is if the lists needs to be re-sized it becomes non-thread safe...
Jon Skeet
people
quotationmark

No. List<T> is explicitly documented not to be thread-safe: It is safe to perform multiple read operations on a List, but issues can occur if the collection is modified while it’s being read. To ensure thread safety, lock the... more 2/25/2014 10:41:31 AM

people

Multiple iterations of random double numbers tend to get smaller

I am creating a stock trading simulator where the last days's trade price is taken as opening price and simulated through out the current day. For that I am generating random...
Jon Skeet
people
quotationmark

Firstly, calling Thread.Sleep like this is not a good way of getting a different seed. It would be better to use a single instance of Random per thread. See my article on randomness for some suggested approaches. However, your code is... more 2/25/2014 10:37:09 AM

people

How does different spacing affect the unary operator?

Can anyone explain me how different spacing affects the unary operator? int i = 1; int j = i+ + +i; // this will print j=2 int k = i++ +i; // this will print k=3 int l = i+++i;...
Jon Skeet
people
quotationmark

First, let's separate this into three separate cases which can't interact: int i = 1; System.out.println(i+ + +i); // 2 int j = 1; System.out.println(j++ +j); // 3 int k = 1; System.out.println(k+++k); // 3 Now let's rewrite them... more 2/25/2014 9:43:21 AM

people

SqlCeCommand Parameters not working

I have a SQL Server Compact database and I'm trying to insert a record into it using cmd.ExecuteNonQuery(). This method worked perfectly fine in another project, but it doesn't...
Jon Skeet
people
quotationmark

Two problems: Firstly, your SQL is specifying literal values because of the quotes. It should be: INSERT INTO Profiles (ProfileName, ProfilePath, ProfileDescription) VALUES (@name, @path, @desc) That way the SQL refers to the... more 2/25/2014 9:01:27 AM

people

Linq query correct way

I want a list of people who share the same first Name, but have birth year either before 1960 or after 1970. It is just a practice query, i was working with intersect, through...
Jon Skeet
people
quotationmark

I suspect you just need to reverse your where and group by clauses. Filter first, then group: var result = from contact in contacts where contact.DateOfBirth.Year > 1970 || contact.DateOfBirth.Year <... more 2/25/2014 8:12:49 AM

people

Get local time for a user

I have an application in VC++ which is used by different users in different timezones. I have to convert this logic to C# now. I am struck up with the dateformats respective to...
Jon Skeet
people
quotationmark

I will have the offset for each user That's broken to start with. You can't have an offset for each user, because offsets change over time - and they don't all change at the same time. Your current code assumes that all users will... more 2/25/2014 8:05:51 AM

people

size is not compared when input taken from a text file

I've the below code. import java.io.File; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import...
Jon Skeet
people
quotationmark

This is the problem: ArrayList<BigInteger> a=new ArrayList<BigInteger>(); ArrayList<BigInteger> b=new ArrayList<BigInteger>(); for(int j=0;j<2;j++){ // Stuff } You're reusing the same lists for each test... more 2/25/2014 7:53:28 AM

people

What's the access modifier of the default constructor in java?

We all know that if we don't specifically define a constructor, the compiler inserts an invisible zero-parameter constructor. I thought its access modifier was public, but in...
Jon Skeet
people
quotationmark

I thought its access modifier is public, but when I deal with a inner class issue, I found maybe I was wrong. Yup. Indeed, I found myself in the same situation a couple of years ago. I was surprised by an error (through Guice... more 2/25/2014 7:16:26 AM

people

VB.NET Listen for TCP connections without wasting processor usage

I am creating a voice server with VB.NET using TCPListener. To check for incoming connections I have created an infinite loop in Form Load method and whenever a connection is...
Jon Skeet
people
quotationmark

Any time you find yourself reaching for Application.DoEvents, you should try to reconsider - it's generally a workaround. At least this time you already know you're in a bad situation :) If you're happy to do everything synchronously, I... more 2/25/2014 7:04:20 AM

people