Browsing 7239 questions and answers with Jon Skeet

Is possible to use lambda expression in linq to sql?

I wonder if you can do something like this: public List<T> FindOrder<T>(Expression<Func<T, bool>> predicate) where T : class { ...
Jon Skeet
people
quotationmark

Not like that, no - but you can write: return preventivos.Where(predicate).Select(...); ... although your code example seems to be unclear as to whether this is really generic or whether it only deals with Preventivos. The point is... more 5/7/2015 1:59:53 PM

people

Is this is a right way to make java.text.DateFormat threadSafe?

I'm working on a project where the date format is kept as a static Utility field as shown below public static final SimpleDateFormat MM_DD_YYYY = new...
Jon Skeet
people
quotationmark

Well it's better in that having public fields is almost always a bad idea, particularly for mutable objects. However, it's definitely still not thread-safe. Two threads could absolutely call the method and end up using the formatter at... more 5/7/2015 1:47:26 PM

people

Why doesn't the following codes cause "unchecked cast" warning?

I think that (String)x is an unchecked cast, but the compiler does not give any warning. Why does it happen? public static void main(String[] args) { Object x=new...
Jon Skeet
people
quotationmark

I think that (String)x is an unchecked cast No, it's not. It's checked at execution time - if the cast isn't valid, it will throw an exception. Unchecked casts are about casts that look like they'll do checking, but actually don't... more 5/7/2015 12:59:28 PM

people

ClassCastException when casting Object[] array to generic type array in Java

Hi I'm very new to Java and in this code, I think I'm not creating the Bag correctly in the Main? Please help thanks! Exception in thread "main" java.lang.ClassCastException:...
Jon Skeet
people
quotationmark

Yes, you're creating an Object[] and then trying to cast it to T[], which the compiler is converting to a cast to Comparable[] (using the raw Comparable type) due to your constraint on T. Arrays and generics don't work terribly nicely... more 5/7/2015 9:17:17 AM

people

Java performance in creating objects

I'm trying to improve my knowledge in Java performance optimization and I tried multiple approach to create an object. I came across this behaviour I'm not familiar with regarding...
Jon Skeet
people
quotationmark

the creation of an object is far less expensive (in terms of time) if the members are not final. No, that's absolutely not the case. Basically, your approach to benchmarking is broken: You're using System.currentTimeMillis() which... more 5/7/2015 9:01:36 AM

people

How to prevent decimal.TryParse convert "," to "."?

This is my code: string myValue = "0,203"; decimal.TryParse(myValue, NumberStyles.Any, CultureInfo.CurrentCulture, out myValueAsDecimal; ... myValueAsDecimal is 0.203...
Jon Skeet
people
quotationmark

Is it possible that myValueAsDecimal has 0,203 after TryParse No. It's just a number - it has no concept of a text format. To think about it another way with a simpler type, consider these two lines of code: int x = 0x100; int y =... more 5/7/2015 8:18:43 AM

people

How to convert a byte array to a string under a restrictive character set?

I would like to apply a cryptographic hash to an IP number and have the hash be in the character set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_". My trials...
Jon Skeet
people
quotationmark

In general, don't reinvent the wheel: use base64. That's the general "shorter than hex, but still printable ASCII" solution. There are various base64 options available, depending on which version of Java you're using etc. If there's... more 5/7/2015 6:12:28 AM

people

Is there a non static ClassInitialize alternative in MSTest?

I'm using Visual Studio 2012 test framework (I guess that's MSTest). I have some code that I need to run once and only once before the various testmethods run. ClassInitialize...
Jon Skeet
people
quotationmark

I'm not sure whether it's really the best approach, but you could make it a static variable - it's fine to access static variables from instance methods (your tests) after all. Note that this could cause problems if you try to run your... more 5/6/2015 6:27:18 PM

people

Most efficient way to count occurrences?

I've got an array of bytes (primitive), they can have random values. I'm trying to count occurrences of them in the array in the most efficient/fastest way. Currently I'm...
Jon Skeet
people
quotationmark

I would create an array instead of a HashMap, given that you know exactly how many counts you need to keep track of: int[] counts = new int[256]; for (byte b : data) { counts[b & 0xff]++; } That way: You never need to do any... more 5/6/2015 5:28:42 PM

people

System.NullReferenceException based on List<int>

I'm trying to loop through rows and get their Indexes (Primary Keys from SQL). I'm getting a NRE on "this.SelectRowIndexes.Add(ResourceKey)" I can't figure out why it would...
Jon Skeet
people
quotationmark

What do you actually want to do if this.SelectRowIndexes is null? Currently you're just unconditionally calling Add on it, because both branches of your if statement do the same thing. Note that it definitely wouldn't be null if you'd... more 5/6/2015 4:21:20 PM

people