Browsing 7239 questions and answers with Jon Skeet

Arrays of Types in attributes

I want to pass array of Types to attribute, I tried do it this way class CustomAttribute: Attribute { Type[] included; Type[] excluded; } [CustomAttribute{included =...
Jon Skeet
people
quotationmark

It looks like this has nothing to do with arrays, but everything to do with your general syntax. You're using { } for attribute arguments instead of ( ), and your attribute doesn't have public properties, which are required for named... more 1/5/2018 7:30:24 AM

people

Newtonsoft.Json.Linq.JObject.ToObject() converting date in string format

I'm building a .net core library. The error is true for 1.1 as well as 2.0. I have a JObject (I read a bunch of other answers where people tell the OP to just do...
Jon Skeet
people
quotationmark

You've been experimenting when serializing the data, but the conversion is happening when you deserialize the JSON to start with. That's where you need to disable DateParseHandling. Here's the change you need: var settings = new... more 1/4/2018 6:34:05 PM

people

Java 8 sort on Class member's property

Class declaration: class Entity { String name; SubEntity subEntity; // subEntity has a method getAmount() which returns int } I understand with Java 8 we can sort...
Jon Skeet
people
quotationmark

Not by using a method reference, no - but it's easy to do with a lambda instead: entities.sort(Comparator.comparing(entity -> entity.getSubEntity().getAmount())); Fundamentally there's nothing magical about Comparator.comparing - it... more 1/4/2018 12:21:08 PM

people

Cannot declare Statement as instance variable

I cannot seem to be able to declare the following as an instance variable. public Statement stmt =...
Jon Skeet
people
quotationmark

It's not the declaration that's failing - it's the attempt to initialize the variable using an expression which can throw a checked exception. You should declare the field, then initialize it in a constructor. That constructor will have... more 1/4/2018 11:25:25 AM

people

Why can I ref return an item of an array that only exists inside the method?

I was trying out the new ref returns of C#7. I am able to compile and build this: public ref string MisUseRefReturn(int index) { string[] array = { "a", "b",...
Jon Skeet
people
quotationmark

Think of the array element as if it were an instance field of the array. Imagine your array were: public class FourElementStringArray { public string element0; public string element1; public string element2; public string... more 1/3/2018 10:04:58 AM

people

c# return that also returns the calling function similar to PHP's die() early out

Note: This is a question from the PHP perspective for C#. Maybe it isn't the right way to do it, but here is what I'm trying to do. Suppose you have defined: void Die(string...
Jon Skeet
people
quotationmark

Not without throwing an exception, no. Bear in mind that in many cases the calling method will be non-void - in that case, what value would you expect it to return? The use case you're describing does sound like it would be better handled... more 1/3/2018 9:13:12 AM

people

How to check for integer and less than or equal to 100 in an if/else statement

I have an if/else statement with a scanner in the conditions, as well as an && statement checking for the input being 100 or less. It goes like this: Scanner input = new...
Jon Skeet
people
quotationmark

You're checking the value before you assign a new value to it. You need to assign the value from nextInt(), then check whether it's in range. For example: if (input.hasNextInt()) { int candidate = input.nextInt(); if (candidate... more 1/2/2018 9:15:26 PM

people

orderByDescending date, month and year

I am using a linq query to get the data order by descending date. but when the year is chaged then descending order of date is not in proper way Here is my code in this code...
Jon Skeet
people
quotationmark

You're ordering by your string representation which starts with the month. It looks like you're actually only interested in distinct dates though - grouping and then discarding everything other than the key is equivalent to just... more 1/2/2018 12:19:53 PM

people

Reading text file works in IDE but not in .jar

I am using File file = new File("res/movies.txt"); to read text from a bundled .txt file. My code works perfectly when running the program within IntelliJ IDEA, but when I...
Jon Skeet
people
quotationmark

You need to load the file as a resource. You can use Class.getResourceAsStream or ClassLoader.getResourceAsStream; each will give return an InputStream for the resource. Once you've got an InputStream, wrap it in an InputStreamReader... more 12/29/2017 8:50:05 PM

people

Check if the node exist in XDocument

I have this xml: <Rejectedparameters> <parameter> <name>CO</name> <value>5.34</value> </parameter> <parameter> ...
Jon Skeet
people
quotationmark

Firstly, each parameter element only has a single name, so just use Element for that. Next, use name as the element you're looking for, not Name. Finally, you're looking for a value of CO, not an element called CO. So you'd have... more 12/25/2017 2:06:40 PM

people