Browsing 7239 questions and answers with Jon Skeet

List<T>.Enumerator IEnumerator.Reset() method implementation

Despite the fact, that IEnumerator.Reset method should never be used I found strange behavior of the method implementation within List<T>. No matter how you examine the...
Jon Skeet
people
quotationmark

Any simple explanation I'm missing? Yes: you're boxing the List.Enumerator here: ((IEnumerator)e).Reset(); That takes a copy of the existing one and resets it - leaving the original in one piece. To reset the actual enumerator,... more 9/30/2013 5:52:54 PM

people

Accessing an object of a particular sub class from an array of abstract type objects

I have a quick question. I have a few classes, say Class SubA, SubB and SubC. I also have an abstract class, lets say Parent So I have an array of Parent objects, which contains...
Jon Skeet
people
quotationmark

Yes, that current code has an implicit cast, which will fail if you've got an object of the "wrong" type in your collection. I suggest you use LINQ's OfType method: using System.Linq; // Make LINQ extension methods available ... foreach... more 9/30/2013 4:12:24 PM

people

what happens if I don't implement a partial method?

I generated some code using SQLMetal and worked with it for some time. Just now, I looked through the generated code, and fond in each class a few partial functions, that I never...
Jon Skeet
people
quotationmark

These functions were called in the code. I wonder - whats happening when I call a function like that - that doesn't exist? The call is completely removed by the compiler. Any expressions which are evaluated in order to call the method... more 9/30/2013 3:55:28 PM

people

String.getBytes() in different default charsets

Is it safe to use String.getBytes() ? What happens when a program runs on different systems with different default charset? I suppose I can get different content byte[]? Is it...
Jon Skeet
people
quotationmark

Is it safe to use String.getBytes() ? It depends on what you mean by "safe". It will do exactly what you're trying to do. What will happens when program will run on different systems with different default charset? I suppose I can... more 9/30/2013 3:41:57 PM

people

C#.NET: internal class accessible from another class using Type.GetType and Activator.CreateInstance?

I know that access modifier internal declared on a class makes it accessible from inside same assembly. However, in my case another assembly is able to access internal class in...
Jon Skeet
people
quotationmark

However, in my case another assembly is able to access internal class in another assembly using Type.GetType and is also able to create its object using Activator.CreateInstance() successfully. Indeed, assuming the code trying to do... more 9/29/2013 4:18:53 PM

people

Understanding Primitive Data Types: Storage Capacity/Range of int and float in Java

I am trying to get a good understanding of Java's primitive data types. I already got a good overview and I understand how signed integer values are stored in Java, and how to...
Jon Skeet
people
quotationmark

The trick is realizing that a float can't accurately hold even all the integers in the range -3e+38 to 3e+38. The difference between "adjacent" float values depends on the magnitude: for float values close to 0, the difference between... more 9/29/2013 12:45:40 PM

people

yield keyword to digest code block like Ruby

In Ruby, we can yield the code block from other scope, in order to optimize the amount of writing code: def get_resource(published=true) find_resources do if...
Jon Skeet
people
quotationmark

I don't know Ruby, but it looks like basically you want to pass "some code to execute" - which is typically done in C# with a delegate (or an interface, for a specific meaning of course). As far as I can see, yield in Ruby is entirely... more 9/29/2013 7:21:31 AM

people

char array to int array conversion in java generates null pointer exception

I am trying to run the following HillCipher program but it is terminated after the line of conversion of char array to int array and after compilation of that code it shows me...
Jon Skeet
people
quotationmark

You never assign a value to iKey, so it has its initial default value of null - it's as simple as that. You need to create a new array, e.g. // Given that you've hard-coded the length of cKey as well... iKey = new int[2][2]; I'd also... more 9/29/2013 7:11:22 AM

people

Java: mutate an instance field

In the following example code, how do I mutate the instance field var1 while not changing the initialVal1 in the constructor (var2)? - should I make a copy using...
Jon Skeet
people
quotationmark

var1 and initialVal aren't arrays - they're just variables. Their values refer to an array... and they both refer to the same array. It's like having two pieces of paper both of which have the same house address on. Any changes you make to... more 9/28/2013 6:49:27 PM

people

List updating unexpectedly

I have a list that is unexpectedly updating. I have a function that returns a list. I want to call it twice with different parameters and store the results in a single long...
Jon Skeet
people
quotationmark

We don't know what webPages is, but I suspect the problem is that your runSearch method doesn't create a new list - it just mutates the existing one. I suspect you want to create a copy of the first list. Note that you don't need the... more 9/28/2013 6:31:19 PM

people