Browsing 7239 questions and answers with Jon Skeet

Which should I reference in NETPortable , can't find GetMethods

I have a Portable Classlibrary. ".NETPortable,Version=v4.5,Profile=Profile75" The code typeof(T).GetMethods() with the error cannot resolve symbol 'GetMethods' my...
Jon Skeet
people
quotationmark

I don't know much about that specific profile (and testing anything with that profile is giving me massive errors around predefined types not being available), but I do know that reflection is now split between Type and TypeInfo. Chances... more 12/2/2015 9:23:47 AM

people

Slow initialization of large array of small objects

I've stumbled upon this case today and I'm wondering what is the reason behind this huge difference in time. The first version initializes an 5k x 5k array of raw ints: public...
Jon Skeet
people
quotationmark

I would expect it to be a bit slower, after all there is more memory to allocate (10 bytes instead of 4 if I'm not mistaken), but I cannot understand why could it take 50 times longer. No, it's much worse than that. In the first case,... more 12/1/2015 9:29:54 PM

people

Readonly getters VS property like functions

With C#6 came some new features, including getter-only auto-properties and property-like function members. I'm wondering what are the differences between these two properties?...
Jon Skeet
people
quotationmark

It's easiest to show them in terms of C# 1: public class Foo { private readonly string bar = "Bar"; public string Bar { get { return bar; } } public string Bar2 { get { return "Bar2"; } } } As you can see, the first... more 12/1/2015 8:58:45 PM

people

Does Null Object Pattern occupy memory

I've been reading that the null object pattern will help you to avoid the null checks and null pointer exceptions making your code cleaner to understand. I have never used it and...
Jon Skeet
people
quotationmark

Typically null objects aren't created on demand - instead, they're shared. They're typically immutable implementations of some other abstraction (with no-op methods or whatever is suitable) - so they can be shared very easily. At that... more 12/1/2015 8:43:18 PM

people

How to make async method generic over Task type

My question is on the surface very similar to Using a generic type as a return type of an async method. However, he did not have a real world usage scenario and so all that could...
Jon Skeet
people
quotationmark

No, async just doesn't support that. The simplest option to fake it would be: public static async Task<T> Retry<T>(Func<Task<T>> action) { for (var i = 0; ; i++) { try { return... more 12/1/2015 7:11:31 PM

people

C# Runtime Error: InvalidCastException with Generic Class Instance

I'm a java developer and new to C#, I'm stuck with InvalidCastException on the following code below. I have implemented a Queue, based on a custom Doubly Linked List...
Jon Skeet
people
quotationmark

Fundamentally, you shouldn't try to make PrintQueue work for every object - you should try to make it work for any Queue<T>... and the simplest way of doing that is to make it generic: public static void... more 12/1/2015 7:50:31 AM

people

C# Basic Arrays: Code returning incorrect value

I have two arrays of doubles. For example: {1,2,3} {4,5,6} We are using (x,y) coordinates as (1,4),(2,5) and (3,6) for example. So there are 3 data points in this example. We need...
Jon Skeet
people
quotationmark

If I understand it correctly, your current code will always finish with i == size - 2, j==size - 1... because every slope is greater than double.MinValue, which is the only value that maxSlope ever takes. The problem is here: if (slope... more 11/30/2015 8:57:58 PM

people

How do I pass a variable argument to instanceof?

I am looking for a way to pass the argument c to instanceof, because i need to loop through an array like shown in the code, but the compiler returns the error "unknown class c"...
Jon Skeet
people
quotationmark

You don't - the instanceof operator always takes the name of a type as its second operand. However, you can use the Class.isInstance method instead: bool = c.isInstance(a); Note the lack of an if/else - any time you have if (condition)... more 11/30/2015 5:28:27 PM

people

Why can a read only property be assigned via constructor?

I've set property Name is read only, but it can still be assigned. class Person { public string Name { get; } public Person(string name) { Name = name; ...
Jon Skeet
people
quotationmark

It can only be assigned in the constructor or in the initializer for the property declaration - just like a read-only field can only be assigned in the constructor or in the field initializer. There won't be a property setter generated -... more 11/30/2015 5:01:14 PM

people

I can't read a compressed file

We have to write a compressed file first with DeflaterOutputStream and than we have to read it back with the InflaterInputStream. I always get an EOFException when i try to read...
Jon Skeet
people
quotationmark

You have three problems here: You're using a PrintStream for no reason, and that will swallow exceptions. You're writing data using an ObjectOutputStream, i.e. creating a binary file of Java serialized objects, but you're trying to read... more 11/30/2015 2:38:31 PM

people