Browsing 7239 questions and answers with Jon Skeet

Thread safe Singletion static method initialization

I'm implementing a singleton pattern, and need the initialization to be thread safe. I've seen several ways to do it, like using the double check lock implementation, or other...
Jon Skeet
people
quotationmark

Yes, that's thread-safe - although it's not the normal singleton pattern, as there are no instances of your class itself. It's more of a "single-value factory pattern". The class will be initialized exactly once (assuming nothing calls the... more 11/21/2014 7:05:01 AM

people

An extenstion method for a WPF control is causing an StackOverflowException

Here's my extension method: public static void SetThreadSafeProperty<T>(this System.Windows.FrameworkElement control, Expression<Func<T>> property, T value) { ...
Jon Skeet
people
quotationmark

You've got your condition the wrong way round - CheckAccess() will return true when it's okay to modify the object in the current thread. Currently, you're saying, "If I'm already in the UI thread, call the method again in the UI thread"... more 11/20/2014 7:37:33 PM

people

Unity C# Array how to clone

Im developing a small app/game in Unity3D. The problem is: I need to clone an array (call it tempArray) and make some modifications to it. Then i need to change values of the...
Jon Skeet
people
quotationmark

However, the output shows that allCells[0,0] element was also changed. This means that any operation I do to the cloned array, is executed to the main array. Unless Cell is a struct, your setRegion method (which sounds like it should... more 11/20/2014 7:21:49 PM

people

Can I Avoid Implementing Parameterized Constructors in Sub classes

I have an abstract class with a 1-param constructor which should be identical for any concrete sub-class. Does every concrete sub-class have to have that same 1-param constructor,...
Jon Skeet
people
quotationmark

Does every concrete sub-class have to have that same 1-param constructor Well, strictly speaking they don't have to have that constructor - but they'll need to have a constructor, in order to pass a value to the AbstractClass... more 11/20/2014 4:40:15 PM

people

Convert byte[] array to a short[] array with half the length

I have a byte[200] that is read from a file, representing a short[100] in little-endian format. This is how I read it: using (FileStream fs = new FileStream(_path, FileMode.Open,...
Jon Skeet
people
quotationmark

I think you're looking for Buffer.BlockCopy. Buffer.BlockCopy(record, 0, target, 0, record.Length); I believe that will preserve the endianness of the architecture you're on - so it may be inappropriate in some environments. You might... more 11/20/2014 3:58:53 PM

people

Duplicate number in an array in java giving wrong output

Here I have sample java code which to count number of duplicate numbers in an array. example: I have array A[]={3,4,5,3,4,3} so I need the output 3 occurred 3 times 4...
Jon Skeet
people
quotationmark

You're currently writing output on each iteration of the inner loop... which means you're getting much more output than you want. You want to print out the result once per iteration of the outer loop, which means bringing count into the... more 11/20/2014 7:49:33 AM

people

Select elements based on condition LINQ TO XML C#

I am querying XML using LINQ TO XML. I want to query records based on some conditional check here is my XML below: XML will have multiple Orders each order will be in I want to...
Jon Skeet
people
quotationmark

The problem is that you're only looking at one param element - always the first one, because that's what the Element does. You want to match if any of the parameters is a "Store" element with a value of 1, so you want to use the Any... more 11/19/2014 8:23:33 PM

people

Call method from a parent class and use child class properties

Actual call: ChildClass classCall=new ChildClass(); classCall.FullName="test name"; string returnName=classCall.GetName(); Parent class with method: public class BaseClass { ...
Jon Skeet
people
quotationmark

Firstly, I'd suggest avoiding using public fields - use properties if you want to make state public. (You're asking for a property in your reflection call, but your derived class declares a field...) At that point, you can make FullName... more 11/19/2014 5:34:03 PM

people

Use Generic class with SqlDataReader GetValue

I have this code that works ok with a database with no NULL values public T GetField<T>(SqlDataReader dr, string fieldName) { return...
Jon Skeet
people
quotationmark

Your final method would fail because it's trying to declare a type parameter called string. I suggest you add another overload without the constraint on T, but with a default value: public T GetField<T>(SqlDataReader dr, string... more 11/19/2014 5:14:21 PM

people

C# Multiple conditions

To put it simply I would like to know if there is a way I can have multiple conditions to trigger a if statement. To add to that the program I am working on will be for toner...
Jon Skeet
people
quotationmark

Yup, you're just looking for the || (conditional-OR) operator: if (kTonerIsLow || cTonerIsLow || ...) { SendEmail(); } Note that the operator is short-circuiting - so if kTonerIsLow is true, it won't evaluate cTonerIsLow, etc. If... more 11/19/2014 2:55:13 PM

people