Browsing 7239 questions and answers with Jon Skeet

How to get the TResult of Func in C#?

I am new to func, I have define the Func like below, private Func<int, Type, GridCell> getGridCell; public Func<int, Type, GridCell> GetGridCell { get { ...
Jon Skeet
people
quotationmark

A Func doesn't have a result until you call it. So GetGridCell doesn't have a value of type GridCell - it's a function which can be called to get a GridCell. For example: Func<int, Type, GridCell> function = GetGridCell; GridCell... more 3/14/2016 6:52:33 AM

people

Thread safety in static methods and variables

I was under the impression that static initialization is thread safe by default. Is that correct? Also consider the below example of singleton pattern. public sealed class...
Jon Skeet
people
quotationmark

I was under the impression that static initialization is thread safe by default. Is that correct? Yes, in terms of initialization - unless you use reflection to call the type initializer again, etc. Of course, there can then be... more 3/13/2016 8:53:17 AM

people

Java: ArrayStoreException

I would like fill an external array with the elements of my ArrayQueue via E[] toArray(E[] a) , but somehow it throws ArrayStoreException at the first System.arraycopy method. I...
Jon Skeet
people
quotationmark

I suspect the exception doesn't actually happen on the line you've indicated, but later, in System.arraycopy. The problem is that your call to Array.newInstance passes in the array type, when you only want to pass in the element type. In... more 3/12/2016 9:48:47 PM

people

NUnit 3.2.0 Console Execution

I am trying to run multiple categories from nunit console. In version below 3.0 i was running it as nunit.exe "mydll.dll" /run /include=Category1,Category2 I would like to use...
Jon Skeet
people
quotationmark

To join multiple conditions together, you need each to be an actual condition - so I think you want: --where:cat==Category1||cat==Category2 Or more readably IMO: "--where:cat == Category1 || cat == Category2" The quoting may be... more 3/11/2016 1:40:39 PM

people

Why the Enumerator of List<T> is public?

What is the reason for the public access modifier on the Enumerator in List? I would expect private modifier instead of public. List source code
Jon Skeet
people
quotationmark

It's public so that the GetEnumerator() method can be declared to return it. That then allows the C# compiler to use it in a foreach loop... avoiding any heap allocations because List.Enumerator is a struct. (A mutable struct, which makes... more 3/11/2016 11:56:39 AM

people

Delete node in XML file

My XML file: <?xml version="1.0" encoding="utf-8"?> <WebServices> <WebService> <Name>ServiceA</Name> ...
Jon Skeet
people
quotationmark

Well, you've selected the child element - so you just need to select its parent: xDoc.Root .Elements("WebService") .Elements("Name") .Where(node => node.Value == "Name1") .Select(node => node.Parent) ... more 3/11/2016 11:01:52 AM

people

c# change file encoding without loading all the file in memory

I need to change a file's encoding. The method that I've used loads all the file in memory: string DestinationString =...
Jon Skeet
people
quotationmark

You can't do so by writing to the same file - but you can easily do it to a different file, just by reading a chunk of characters at a time in one encoding and writing each chunk in the target encoding. public void RewriteFile(string... more 3/11/2016 7:04:25 AM

people

c# MySqlCommand.ExecuteNonQuery() return 1

I'm trying to execute a SQL request in C# to know if a user is already registered on my data base. To do that, I'm using the following source code : public bool...
Jon Skeet
people
quotationmark

You're calling ExecuteNonQuery, despite trying to execute... a query. You should be using ExecuteScalar - or ExecuteQuery and check whether there are any results. ExecuteNonQuery is specifically for insert/delete/update SQL statements,... more 3/10/2016 10:09:23 PM

people

Java 8: Difference between method reference Bound Receiver and UnBound Receiver

I am trying to use Java 8 method references in my code. There are four types of method references available. Static method reference. Instance Method (Bound receiver). Instance...
Jon Skeet
people
quotationmark

Basically, unbound receivers allow you to use instance methods as if they were static methods with a first parameter of the declaring type - so you can use them as functions by passing in whatever instance you want. With a bound receiver,... more 3/10/2016 11:17:03 AM

people

Optimize multiple or Statements for a Set<String>

What will be the best way to optimize below code? protected void saveUserRoles(Set<String> userRoles) { if (userRoles != null &&...
Jon Skeet
people
quotationmark

I suggest you just keep a list or array of the values to test against: private static final List USER_ROLES_TO_SAVE = Arrays.asList( StaticValues.LUMEN_SELECT_USER_ROLE, StaticValues.EASY_SENSE_USER_ROLE, ... more 3/10/2016 7:04:09 AM

people