Browsing 15836 questions and answers with Jon Skeet

.net async await, what I am doing wrong

Trying to understand use async await to done some work and something just doesn't work. have a code: private async Task<string> getStringAsync() { var tsk = await...
Jon Skeet
people
quotationmark

You're using Result, which blocks until the task completes. You're using that from the UI thread. So the UI thread will be blocked until the task completes. Now, that task comes from getStringAsync, which uses await (something). As the... more 7/21/2017 9:59:47 PM

people

parsing json w variable key value pairs in c#

I've got the following [ { "name": "ATVI", "dailyClosePrice": [ { "3/15/2017": 210.08, "4/6/2017": 235.08, "4/21/2017":...
Jon Skeet
people
quotationmark

You definitely shouldn't be parsing this by hand - Json.NET will be absolutely fine with this, so long as you model it correctly. The JSON represents a list of objects, each of which has a name (string) and a dailyClosePrice which appears... more 7/21/2017 9:46:41 PM

people

Convert string to ASCII without exceptions (like TryParse)

I am implementing a TryParse() method for an ASCII string class. The method takes a string and converts it to a C-style string (i.e. a null-terminated ASCII string). I had been...
Jon Skeet
people
quotationmark

Two options: You could just ignore Encoding entirely, and write the loop yourself: public static bool TryParse(string s, out byte[] result) { result = null; // TODO: It's not clear why you don't want to be able to convert an... more 7/21/2017 11:54:56 AM

people

Why I can access class that is defined outside of namespace?

I was wondering Why I can access class when I defined it outside of namespace scope? I am not very familiar with namespaces, I am just learning, but I thought that namespace...
Jon Skeet
people
quotationmark

Namespaces have nothing to do with access. It's important to differentiate between namespaces and assemblies - they're often closely related, in that types in a namespace Foo.Bar would be likely to be in assembly Foo.Bar.dll, but that's a... more 7/21/2017 7:59:26 AM

people

Can't convert returned string from service to int in c#

I create a service as you can see : public int ValidateAndSubmitReception(NajaResult ReceptionData) { ClientRequest.Headers["Content-type"] = "application/json"; ...
Jon Skeet
people
quotationmark

Firstly, ignore the backslashes in the debugger. They're misleading. The string you've got is: "-1" That's the actual text of the string, as you'd see it if you printed it to the console. While you could just remove the quotes... more 7/21/2017 7:46:02 AM

people

Avoid boilerplate code when initializing collection properties in c#

Is there a way to avoid writing exactly the same type twice when initializing properties (or fields) in c#? (something similar to diamond operator in Java would do..) For...
Jon Skeet
people
quotationmark

No, there's nothing like that in C# at the moment. The obvious potential fix would be to allow fields to be declared using var, but that's more complicated than it may sound. I'd be somewhat surprised to see anything like Java's "diamond... more 7/21/2017 7:28:42 AM

people

Add second indexer to Math.NET Numerics

Not sure how to meet the demands of stackoverflow on this one... I'd like to modify the MathNET Numerics package so that I can use the indexer to access or assign to a sub-matrix...
Jon Skeet
people
quotationmark

No, there's no such thing as an "extension indexer" right now. Extension methods are as close as you can get. However, it's entirely possible (but not guaranteed) that they'll come in C# 8. So your options are: Wait for C# 8 Use your own... more 7/20/2017 8:28:52 PM

people

Why doesn't name exist in the current context of shorthand member initialisation?

I am using an object initializer for a st object: public class Container { public Container () { ContainedItem = new Item; } public Item ContainedItem { get; set;...
Jon Skeet
people
quotationmark

You can assign values to "sub-properties" as it were, just not with that syntax. Here's a complete example: using System; public class Container { public Item Item { get; set; } = new Item(); } public class Item { public string... more 7/20/2017 10:24:33 AM

people

Linq functions give strange compile error when ambiguous use of IEnumerable possible workarounds?

Given code similar to the following (with implementations in the real use case): class Animal { public bool IsHungry { get; } public void Feed() { } } class Dog : Animal { ...
Jon Skeet
people
quotationmark

As you say, the error message is unfortunate, in that the problem is ambiguity rather than Where not being found at all (assuming you have a using directive for System.Linq). The problem is that the compiler can't infer the type argument... more 7/20/2017 10:08:49 AM

people

I have converted dll to exe and build then it shows error exe does not contains static main method suitable for an entry point

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace abc { public class Class1 { public...
Jon Skeet
people
quotationmark

The entry point needs to be called Main, not main. That's also in-keeping with .NET naming conventions, which I'd strongly urge you to follow: Your namespace should be capitalized, e.g. Abc rather than abc Your display method should be... more 7/20/2017 6:18:42 AM

people