Browsing 7239 questions and answers with Jon Skeet

readLine for reading multiple lines without a loop?

So my servlet is reading a url link from another servlet and then publishing the article associated with it. All the articles are in a single .txt file with each article on its...
Jon Skeet
people
quotationmark

I would strongly advise you to separate out the "reading the data" from the "outputting HTML" sections of your code. It looks like you should create a data structure for an article, with properties for whatever the different bits in each... more 9/1/2014 9:12:00 PM

people

Getting Week number of year starting from April

I'm currently using this snippet: private string GetCurrentWeek() { // Return a string to be used in the TextBox control on the Main Form DateTimeFormatInfo dfi =...
Jon Skeet
people
quotationmark

I'm assuming you want week 1 to begin on April 6th always, and be 7 days long, rather than having some rule like "Weeks always start on Mondays". Basically this is just a matter of: Working out which tax year you're in Finding the start... more 8/31/2014 7:39:00 PM

people

Is a private final String[] array threadsafe as long as its protected?

Let's say I have a static final String[] array that is only read but will not be modified private static final String[] myArray = { "Col1", "Col2", "Col3" }; If I can guarantee...
Jon Skeet
people
quotationmark

Yes, it's fine so long as nothing ever writes to the array. It will be initialized during type initialization, so all threads will "see" the values. For the sake of readability you might want to consider using an immutable collection... more 8/31/2014 4:42:54 PM

people

Why does this method keep returning dynamic despite the return type in the signature?

So the type being returned by the activator (not shown here) is just a POCO that I created. Nothing special about it at all. But despite, the return type of GetWrapper or...
Jon Skeet
people
quotationmark

I strongly suspect that either child1 or child1.kind are of type dynamic, meaning that the expression is deemed to be a dynamically-bound expression, despite everything else. Here's a short but complete example to demonstrate what I... more 8/30/2014 8:46:44 PM

people

Can I use a member of an object that requires a cast without using a temporary variable?

I'd like to know if there is some way to use the method of a casted object without creating a new variable. In other word, is there a way to do this: abstract class event {...
Jon Skeet
people
quotationmark

This is a problem with operator precedence. . has a higher precedence than a cast, so this: (loop) (e).a is being treated as: (loop) ((e).a) You want to cast and then use the result in the member access - so you need to bind the cast... more 8/30/2014 5:12:59 PM

people

Can obj.GetType().IsInterface be true?

While doing something almost completely irrelevant, a question popped into my head: Can an expression of the form obj.GetType().IsInterface ever be true in a codebase consisting...
Jon Skeet
people
quotationmark

Can an expression of the form obj.GetType().IsInterface ever be true in a codebase consisting exclusively of C# code? Yes - but probably not the way you were thinking of: using System; public class EvilClass { public new Type... more 8/30/2014 4:40:49 PM

people

Is this use of varargs safe?

I have a sort of util method to transform a varargs of a type into an array of that type - it looks like this: public K[] array(K... ks) { return ks; } The use case is so...
Jon Skeet
people
quotationmark

No, it's not safe - if called from another method which is using generics. Here's a complete example which looks okay, but throws an exception: class Utility<K> { public K[] array(K... ks) { return ks; } ... more 8/30/2014 4:28:56 PM

people

Does LINQ try to solve things in one pass?

Say we have list.Where(p=>p.Number > n).Select(p=> p.Name).Where(n=> n.StartsWith(a)).ToList(); will this be ran one pass algorithm or it will be 3 passes?
Jon Skeet
people
quotationmark

It will build up the list in one pass, due to the way that LINQ streams the data. For example, take this: var query = list.Where(p => p.Number > n); That in itself doesn't look at any of the elements of the list. Instead, it... more 8/29/2014 5:32:24 PM

people

How to get type of inherited generic

I have these classes: public abstract class BaseClass{} public class CustomerService : BaseClass<Customer>{} public class ProductService : BaseClass<Product>{} public...
Jon Skeet
people
quotationmark

If you're just looking for a class whose direct base type is the relevant BaseClass<T>, you can use code like this: var targetBaseType = typeof(BaseClass<>).MakeGenericType(entityType); var type =... more 8/29/2014 4:55:44 PM

people

push(int[][]) in a stack is duplicating the same value Java

I have a 2 dimensional array and stack: int[][] data = new int[][]; Stack<int[][]> undoList = new Stack<int[][]>(10); I wanna add the data after every change to...
Jon Skeet
people
quotationmark

You're making changes to the array - but you're not putting the array itself on the stack... you're putting a reference to the array on the stack. You're basically pushing the same reference several times. If you want independent values,... more 8/29/2014 4:51:38 PM

people