Browsing 7239 questions and answers with Jon Skeet

Why does C# object initializer has this behaviour?

Why does C# object initializer has this behavior? public class ChuckNorris { public string Impact { get; set; } } public class Burnination { public string Impact { get;...
Jon Skeet
people
quotationmark

Basically, the expression on the RHS of the = is resolved and evaluated as it would be in any other context. So you could have: ChuckNorris chuck1 = new ChuckNorris { Impact = "foo" }; ChuckNorris chuck2 = new ChuckNorris { Impact =... more 5/10/2015 2:54:51 PM

people

String array error using JFrame

I'm creating a simple roulette remake. I'm using JFrame. Everything about my code works fine, except for one thing. I marked it in my code, it's the method ButtonAction. I'm sorry...
Jon Skeet
people
quotationmark

This is the problem: for (int i = 1; i <= BUTTON_COUNT; i++) Arrays in Java are 0-indexed. So while you're using array indexes 1 to 8, the valid indexes are actually 0 to 7. So your loop should be: for (int i = 0; i <... more 5/10/2015 8:16:35 AM

people

list all tags in XML with LINQ

I am trying to read variety type of XML files and in each xml type, root name and all other tags may change. I need to list all the xml tags but I couldnt succeed to read. this...
Jon Skeet
people
quotationmark

Well, you can get all elements using the Descendants() method: foreach (var element in xdoc.Descendants()) { Console.WriteLine(element.Name); } However, that won't do any indentation. If you want indentation as well, you could use... more 5/10/2015 6:49:00 AM

people

finding the non common element between two arrays

In an interview it was asked to find non- common elements between two string arrays. Eg: String a[]={"a","b","c","d"}; String b[]={"b","c"}; O/p should be a,d I have answered...
Jon Skeet
people
quotationmark

I would handle this in three steps: Find all elements in a but not b Find all elements in b but not a Add those two sets together So for example: Set<String> aSet = new HashSet<>(Arrays.asList(a)); Set<String> bSet =... more 5/10/2015 6:35:49 AM

people

Non static method reference?

A little side project I've been doing for fun involves subtracting the current date from a future date given by the user to return the days between them. public int...
Jon Skeet
people
quotationmark

The method for doing this is non-static, as the date2 int changes. I think you've misunderstood the meaning of the static modifier. Your method doesn't use any instance fields, and there's no reason to override it in subclasses, so... more 5/9/2015 7:52:11 PM

people

Summing duplicate values while reading in data

I am reading in 5000 rows of data from a stream as follows from top to bottom and store it in a new CSV file. ProductCode |Name | Type | Price ABC | Shoe | Trainers |...
Jon Skeet
people
quotationmark

That sounds like you just need an appropriate LINQ transformations: results = results .GroupBy(p => p.ProductCode) .Select(g => new Product { ProductCode = g.Key, Name = g.First().Name, Type =... more 5/8/2015 1:58:35 PM

people

prevent unassigned objects, any reason this is a bad design consideration?

In my WPF application, I have a few places that based on a given ID of a record, I call a new form to be displayed as modal to view details. It then closes and returns back to...
Jon Skeet
people
quotationmark

To keep this simplified in coding, I put a "ShowDialog()" call at the end of the constructor of the form being displayed. That sounds like an ugly design to me, personally. Constructors are designed to return a usable object - and... more 5/8/2015 1:05:12 PM

people

Where does the LINQ query syntax come from?

I'm new to C# and have just started to delve into using classes that essentially mirror a database. What I'm confused about is how I'm able to use lines like var...
Jon Skeet
people
quotationmark

From what I understand, that syntax isn't "normal" C# Yes it is, as of C# 3. so the above line wouldn't have any meaning if I hadn't included System.Linq Yes it would. It would still have effectively been transformed by the... more 5/8/2015 12:45:22 PM

people

java iterator with index parameter

hi a normal iterator for a LinkedList would be the following, however, how do we build an iterator that returns an iterator starting at a specified index? How do we build: public...
Jon Skeet
people
quotationmark

Well you could just call the normal iterator() method, then call next() that many times: public Iterator<E> iterator(int index) { Iterator<E> iterator = iterator(); for (int i = 0; i < index &&... more 5/8/2015 12:35:51 PM

people

Better way to write if statement with a lot of ||

I wonder if there is a more elegant way to write an if statement with a lot of || in java. I've given different values to alphabet letters: A,O,I,B,T,S,M,N -> 1 C,D,F,G ...
Jon Skeet
people
quotationmark

Three options: Use a switch statement, assuming you're using Java 7 or 8: switch (getLetter()) { case "A": case "O": case "I": case "B": ... value = "1"; ... } Use three Set<String> objects (Best,... more 5/8/2015 11:26:31 AM

people