Browsing 7239 questions and answers with Jon Skeet

Json.Net deserialize JSON string inside a string

This sound very simple. But I can't find how to do it. I've receiving a bad json from an api. the actual json is inside a string Instead of...
Jon Skeet
people
quotationmark

If you've got a value which is "a string serialized as JSON", then just deserialize that first. Assuming your string genuinely starts and ends with a double quote, you should be fine to call JsonConvert.DeserializeObject<string> to... more 5/23/2017 11:01:22 AM

people

Escape XML using stringutils commons.lang version3 apply on writer

In the older version of "org.apache.commons.lang"(2.6) StringEscapeUtils, there is a method to escapexml by passing an instance of "java.io.Writer" as one of the parameter. eg:...
Jon Skeet
people
quotationmark

All the overloads accepting a Writer have been removed - but instead, CharSequenceTranslator instances are available, so you could write: StringEscapeUtils.ESCAPE_XML10.translate(input, writer); (Personally I'd strongly recommend using... more 5/23/2017 7:31:10 AM

people

C# Method reference of constructor

In C# I can have references of methods and static methods. Can I also get the reference of a classes constructor? In Java I can say Supplier<MyClass> createMyClass =...
Jon Skeet
people
quotationmark

No, there's no equivalent of method group conversions for constructors, or properties, indexers or operators. It's an entirely reasonable idea, but it isn't in C# at the moment. It is, however, tracked as part of a feature request in the... more 5/23/2017 6:42:46 AM

people

This is Sparta, or is it?

The following is an interview question. I came up with a solution, but I'm not sure why it works. Question: Without modifying the Sparta class, write some code that makes...
Jon Skeet
people
quotationmark

But why does Sparta in MakeItReturnFalse() refer to {namespace}.Place.Sparta instead of {namespace}.Sparta? Basically, because that's what the name lookup rules say. In the C# 5 specification, the relevant naming rules are in section... more 5/22/2017 8:02:07 PM

people

Partly negative period between 2 dates with NodaTime

I use NodaTime library to get period between two days, but sometimes I get partly negative period between two dates: var start = DateTime.Now; var end =...
Jon Skeet
people
quotationmark

TL;DR: Update to Noda Time 2.0.2. This was a bug in 2.0.0 and 2.0.1 of Noda Time. Brief testing suggests it wasn't an issue in earlier versions. The problem was due to an optimization when trying to obtain the time parts of a period... more 5/22/2017 4:26:34 PM

people

Java Static Initializer Block datatype

static int B,H; static boolean flag = true; static { Scanner scan = new Scanner (System.in); B = scan.nextInt(); H = scan.nextInt(); scan.close(); And...
Jon Skeet
people
quotationmark

Well in your second code, B and H are local variables within the block - you're not assigning to the field at all. That's exactly the same as if you were writing a method... Forget static initialization for the minute - imagine you had... more 5/22/2017 6:17:45 AM

people

How to create delegate for Result property getter in Task c#

I need to create a delegate for Result property of Task in c#( TResult getter_Result) I am able to get getter method for property Result Resultgetter =...
Jon Skeet
people
quotationmark

You can do this via expression trees, but the way your sample code is set up makes it look like you want just a Func<object>, which means the task has to be already embedded in the expression tree. You can do that: using... more 5/21/2017 6:56:12 PM

people

To Sum Value using linq

In the below code i have a list i am trying to get values from list using linq query and sum the values.But i don't know how to sum the values.So please help me to resolve the...
Jon Skeet
people
quotationmark

You shouldn't be including the value in your grouping - you want to group by just Desc and Month (I assume), then sum the value parts. Two options: var sums1 = objList .GroupBy(y => new { y.Desc, y.Month }) .Select(group =>... more 5/21/2017 6:20:01 PM

people

What is the difference between .net Core multi target and .net Standard?

.Net Standard is used to can be use the same library in different type of projects, such as WPF, xamarin, UWP... etc. .Net Core can't by default, but there is the possibility to...
Jon Skeet
people
quotationmark

You would need to target multiple frameworks in the csproj file. In the original launch of Visual Studio 2017 there's no UI for this, but you can do it manually. I believe that there will be UI support for this in an update. It's just a... more 5/21/2017 11:15:21 AM

people

Convert Utf 8 String To Windows Cp 1258

I want to convert a text file from utf-8 to windows cp-1258 by C# console, here is my code but it does not work, the output file content is not cp-1258 string path =...
Jon Skeet
people
quotationmark

Your current code is going through all kinds of steps it doesn't need to. All you need is: string text = File.ReadAllText("input.txt"); Encoding encoding = Encoding.GetEncoding(1258); File.WriteAllText("output.txt", text,... more 5/21/2017 8:39:22 AM

people