Browsing 15485 questions and answers with Jon Skeet

Conversion of var to class type in C#?

I am new to C# and was going through ArrayList. My question is can we store different datatype - different classes, different structures into ArrayList and access them. I could...
Jon Skeet
people
quotationmark

Firstly, I'd advise you to not use ArrayList. Use List<T> where you can, although storing different types of objects in a list is a bit of an anti-pattern to start with. Convert.ChangeType doesn't do anything for you, and you should... more 2/8/2017 7:25:33 AM

people

Check if Set of Object contain an Object with this attribute

Consider i have a List Of Objects, and i convert it to a Set to make some actions like this : List<User> listUser = new ArrayList<>(); listUser.add(new User(1,...
Jon Skeet
people
quotationmark

Does your User class override equalsusing just the ID? If so, you could use: if (mySet.contains(new User(1, "irrelevant")); Note that it's quite odd to have something called mySet which is actually a List rather than a Set... I'd... more 2/7/2017 9:40:38 AM

people

C# Setting XML Node values as Stings from StreamReader result

I'm using an API call to return some XML data from a web server. The XML data is in the following format: <forismatic> <quote> <quoteText>The time...
Jon Skeet
people
quotationmark

You're trying to use doc.Element("quote") - there's no such element, so that's returning null. You'd want doc.Root.Element("quote"). Next you're asking for quoteText and quoteAuthor as if they were attributes - they're not, they're... more 2/3/2017 11:37:30 AM

people

Java 8 check statement for every list item

I have a const experience value, person object, list of skill and method (can not modify it) hasSkill(skill,person,experience). I want to check (method should return boolean) that...
Jon Skeet
people
quotationmark

It sounds like you want allMatch: return skillList.stream().allMatch(s -> hasSingleSkill(s, person)); As another more general matter, any time you have condition ? true : false you can just replace that with condition So your... more 2/3/2017 9:39:39 AM

people

SimpleDateFormat parsing results are odd

I want to parse dates from a filesystem and get them in this format: 2013-07-29 14:49:53.813588954 +0200 Therefore my pattern looks like this yyyy-MM-dd HH:mm:ss.SSSSSSSSS...
Jon Skeet
people
quotationmark

You've parsed 813588954 as a number of milliseconds - that's over 9 days, and it's being added to 2013-07-29 14:49:53. Basically, SimpleDateFormat doesn't handle parsing nanoseconds, and java.util.Date only supports millisecond precision... more 2/2/2017 10:10:02 AM

people

The blank final field INSTANCE may not have been initialized

I follow this post to create a thread safe singleton classs, but there is an compile error in INSTANCE. It said The blank final field INSTANCE may not have been initialized. My...
Jon Skeet
people
quotationmark

Given what you've said, you shouldn't be using class initialization for this. In particular: You want to try multiple times You want to use a checked exception Both of those are feasible, but you'll need to move the initialization into... more 2/1/2017 9:25:10 AM

people

What is the actual difference between assertEquals() vs assertTrue() in TestNG?

I'm confusing about both these methods, because both can do the same thing, like below snippet of my code. Using assertEquals() method: String a = "Hello"; String b =...
Jon Skeet
people
quotationmark

assertEquals is better because it gives the unit test framework more information about what you're actually interested in. That allows it to provide better error information when the test fails. Suppose you had String a = "Hello"; String... more 2/1/2017 7:33:39 AM

people

C# 7 Expression Bodied Constructors

In C# 7, how do I write an Expression Bodied Constructor like this using 2 parameters. public Person(string name, int age) { Name = name; Age = age; }
Jon Skeet
people
quotationmark

You don't, basically. Expression-bodied members are only available when you have a single statement to execute. You have two in this case. I mean, you could use tuples, but I strongly advise against it: // DO NOT USE THIS CODE - IT'S... more 2/1/2017 7:27:29 AM

people

Adding and subtracting Period from LocalDate doesn't produce the same date

i use java 8 LocalDate and Period classes to add and remove years, months and days. Why in some cases if add Period to date and remove the same period java 8 return another...
Jon Skeet
people
quotationmark

Why in some cases if add Period to date and remove the sane period java 8 return another date? Because that's how calendrical arithmetic works - months are uneven lengths, and it makes things tricky to say the least. You're adding... more 1/30/2017 9:33:26 PM

people

Why does this non blocking IO call fail?

Background I'd like to send a large (30MB, but could be much larger in the future) amount of data using Java's non-blocking SocketChannel Why non-blocking? So that the...
Jon Skeet
people
quotationmark

I believe the answer lies in the WritableByteChannel.write documentation: Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state,... more 1/30/2017 12:29:48 PM

people