Browsing 7239 questions and answers with Jon Skeet

Field initialization in java class outside the methods' bodies

Please explain below behavior, why first statement is valid while other one is invalid and throws error. public class Test{ private String firstName="John";// is Valid ...
Jon Skeet
people
quotationmark

A class can only contain declarations (and static/instance initializers). A field declaration can contain an initializer, as per firstName - and your declaration of lastName is valid, but the assignment after it is just a statement, and a... more 8/19/2016 9:13:48 AM

people

Unreachable code try catch finally

I know if java finds a line of code where it's guaranteed that control will never reach, then compiler reports unreachable code error. consider following code. static int...
Jon Skeet
people
quotationmark

This is the relevant part of JLS 14.21: A try statement can complete normally iff both of the following are true: The try block can complete normally or any catch block can complete normally. If the try statement has a... more 8/18/2016 4:48:19 PM

people

Array as property. Looking for simpler way to fill an array

I am pretty new to C# and I see a lot of code where I'm not quite familiar with the syntax. So I was wondering if there's some simpler way of doing what I did here: I have a...
Jon Skeet
people
quotationmark

LINQ is the way forward here, I'd say: public int[] Types => Enumerable.Range(0, NumberOfItems) .Select(i => GetTypeForItem(i)) .ToArray(); I've changed the names to... more 8/18/2016 2:24:25 PM

people

Casting a list of a primitive type to a IEnumerable<object>

in my C# program, I need to cast an object to a IEnumerable<object>. All I know about that object is, that it is a list (Type.IsGenericType): IEnumerable<object> iEnu...
Jon Skeet
people
quotationmark

Just use IEnumerable instead of IEnumerable<object>; although an int[] doesn't implement IEnumerable<object>, it does implement IEnumerable: IEnumerable enumerable = myObject as IEnumerable; if (enumerable != null) { ... more 8/18/2016 11:19:19 AM

people

Deserialize Json from a stream using a custom converter

I am deserializing a large JSON fragment that I get as a response body from a request to a REST endpoint. I use the code below var serializer = new JsonSerializer(); var sr =...
Jon Skeet
people
quotationmark

You need to customize the serializer itself. For example: serializer.Converters.Add(new ResultItemConverter()); I don't know whether you have to use a custom converter in this case, but that's how you can do so easily. more 8/18/2016 10:56:35 AM

people

IOException: file being used by another process

I know this type of questions have been asked before, and I am aware when this exception is thrown. However, I am unable to resolve it in my code. What I have is a DataSet and I...
Jon Skeet
people
quotationmark

You're calling FileInfo.Create(), which returns an open stream - but then you're not closing it, which prevents the next statement from opening the same file to read write to it. Further, I wouldn't expect you to have to create the file... more 8/18/2016 6:13:37 AM

people

Why dynamic .ToString() (string.Format(), Convert.ToString()) returns dynamic and not string?

This question is for educational purposes only. I have this code that fails at compile time on line where I want to write lines to file....
Jon Skeet
people
quotationmark

It seems to me like your question can really be boiled down to: dynamic d = "x"; var v = Convert.ToString(d); ... the compile-time type of v is dynamic, as shown by hovering over it in Visual Studio, and you'd expect it to be string. No... more 8/17/2016 8:48:34 PM

people

Instantiating inherited Constructor

I have two classes A (superclass) and B (subclass) as shown below. I used super() keyword along with parameters to instantiate the constructor of the super class. However, I'm...
Jon Skeet
people
quotationmark

However, I'm looking another elegant way of doing this rather than listing all the parameters of the super class in the sub class since it becomes lengthy when the number of attributes for the super class is huge. Well, the elegant... more 8/17/2016 8:09:51 PM

people

Join multiple list on property in parallel

I would like to join multiple list with same type on property in parallel. Let say I have three list like below. List 1 id |Name |Phone|Address 1 |John |NULL |NULL 2 ...
Jon Skeet
people
quotationmark

It sounds like you just want to parallelize your existing join. That's as simple as adding .AsParallel() to each source: var newList = from l1 in list1.AsParallel() join l2 in list2.AsParallel() on l1.Id equals l2.Id ... more 8/17/2016 5:42:11 PM

people

Git: Command for cancel a commit

When I work, I often meet with this workflow: Doing something. Someone comes to my desk and ask something, I must move to another branch for checking. I commit code on this...
Jon Skeet
people
quotationmark

In addition to the two existing answers (git reset and git stash), I personally just ignore the extra commit (which almost has a commit message of wip...) until I'm ready to push the change somewhere remote, e.g. to github to create a pull... more 8/17/2016 5:38:28 PM

people