Browsing 7239 questions and answers with Jon Skeet

Why the singleton implementation in C# 6.0 does not need the beforefieldinit flag?

I'm trying to understand why this is a correct implementation of the Singleton pattern: public sealed class Singleton : ISingleton { public static Singleton Instance { get; }...
Jon Skeet
people
quotationmark

Both are correct singleton implementations. Whether you need the static constructor just depends on how much you care about full laziness. If you really, really don't want the singleton to be instantiated until it's used by a caller, you... more 3/21/2016 2:56:14 PM

people

Is it okay for a struct to have default value invalid?

Consider the following struct public struct ResizeFactor { public readonly float Horizontal; public readonly float Vertical; public ResizeFactor(float horizontal,...
Jon Skeet
people
quotationmark

I would try very hard to make the default value valid, yes. For example, you could do something like: public struct ResizeFactor { private readonly float horizontal; private readonly float vertical; public float Horizontal... more 3/19/2016 6:17:38 PM

people

C# Methods with infinite parameters array access

After learning how to Create a method with infinite parameters, I wonder is it legal to store the parameter array into a array. Will it cause any problem, since I don't see many...
Jon Skeet
people
quotationmark

That's fine - it's just an array. All the compiler does with a parameter array is convert a call like this: Foo("x", "y"); into: Foo(new string[] { "x", "y" }); That's really all there is to it. Anything you'd expect to be... more 3/19/2016 5:45:21 PM

people

Error with position

I get error: ERROR: The left-hand side of an assignment must be a variable, a property or an indexer What is the problem?
Jon Skeet
people
quotationmark

The type of Transform.position is Vector3, which is a struct. That means when you access it, you get a copy of the value. Then ScreenToWorldPoint takes that value and returns another Vector3. Mutating that value wouldn't do anything useful... more 3/19/2016 1:43:04 PM

people

Unable to understand recursive factorial

I am new to recursive and I'm having trouble understanding how this recursive factorial function is calculated. When I try to run through the code with my mind, this is how I...
Jon Skeet
people
quotationmark

Your visualization should be: If number = 4, 1st return: 4 x (2nd return) 2nd return: 3 x (3rd return) 3rd return: 2 x (4th return) 4th return: 1 That then simplifies to 4 x 3 x 2 x 1, as expected. Basically, you... more 3/19/2016 8:29:15 AM

people

Is this nameof() truly a recursive call?

I'm making a ConfigurationSection in C# right now. I decided to take advantage of some C# 6 features by using nameof(PropertyName) instead of hard coded strings. However, I'm...
Jon Skeet
people
quotationmark

Unless this[string] calls CoreApiRootUrl, then it's not recursive. You're right that nameof(...) doesn't actually make a call at all. I've just validated that I see the same bug (with R# 10.0.1) in a small test class: public class... more 3/18/2016 5:34:45 PM

people

Understanding BigInteger constructor

I have the following code from my textbook for figuring out factorial: import java.math.*; public class LargeFactorial { public static void main(String[] args) { ...
Jon Skeet
people
quotationmark

It's just calling the BigInteger(String) constructor, because there isn't a constructor taking an int. Using string concatenation is a nasty way of converting an int to a String, but it will work. A cleaner approach IMO would be to use... more 3/17/2016 11:00:44 PM

people

IntStream to sum a 2D array

What is the syntax for using IntStream to sum a range of elements in a 2D array? For a 1D array, I use the following syntax: int totalElementsInArray =...
Jon Skeet
people
quotationmark

You just need to map each sub-array to an int by taking the first element: import java.util.*; import java.util.stream.*; class Test { public static void main(String args[]) { int[][] data = { { 1, 2, 3 }, ... more 3/17/2016 7:52:51 PM

people

Force generation of Initialization Vector 32 bytes (16 characters) in length

I want to generate an Initialization Vector (IV) for my AES encryption method. I have defined the key size as 256 (since I am using AES) so my IV needs to 32 bytes in length (256...
Jon Skeet
people
quotationmark

I wish to store the IV as a string representation so using UTF-8, this would equate to a 16 character string. No. Just don't do this. Yes, you could interpret the 32 bytes as a UCS-2 sequence, but it's a really bad idea. This isn't... more 3/17/2016 3:45:41 PM

people

Why the containValue method returning true even if i override the hashCode() method?

HI even here if I comment out overridden hashcode method in the below code, the output is true for containValue method even the hashcodes are different please help with this. I...
Jon Skeet
people
quotationmark

Hash maps only use hash codes to find keys efficiently. When you ask the map to find a value, it basically has to iterate over all its entries, at which point there's no point in using hashCode(), so it just calls equals. If you try the... more 3/17/2016 2:46:30 PM

people