Error CS1056: Unexpected character ‘$’ running the msbuild on a tfs continuous integration process

The problem can be fixed installing a Nuget package Microsoft.Net.Compilers. Below is the link of my highlighted answer: Project builds fine with Visual Studio but fails from the command line That feature is a syntactic sugar for C#6, try to install the latest version of the framework 4.6.2 https://www.microsoft.com/en-us/download/details.aspx?id=53345 Then go to your Project properties … Read more

Request.Content.ReadAsMultipartAsync never returns

I ran into something similar in .NET 4.0 (no async/await). Using the debugger’s Thread stack I could tell that ReadAsMultipartAsync was launching the task onto the same thread, so it would deadlock. I did something like this: IEnumerable<HttpContent> parts = null; Task.Factory .StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents, CancellationToken.None, TaskCreationOptions.LongRunning, // guarantees separate thread TaskScheduler.Default) .Wait(); … Read more

Why doesn’t generic ICollection implement IReadOnlyCollection in .NET 4.5?

There are probably several reasons. Here are some: Huge backwards compatibility problems How would you write the definition of ICollection<T>? This looks natural: interface ICollection<T> : IReadOnlyCollection<T> { int Count { get; } } But it has a problem, because IReadOnlyCollection<T> also declares a Count property (the compiler will issue a warning here). Apart from … Read more

How do you create an asynchronous method in C#?

I don’t recommend StartNew unless you need that level of complexity. If your async method is dependent on other async methods, the easiest approach is to use the async keyword: private static async Task<DateTime> CountToAsync(int num = 10) { for (int i = 0; i < num; i++) { await Task.Delay(TimeSpan.FromSeconds(1)); } return DateTime.Now; } … Read more

Using async without await?

You still are misunderstanding async. The async keyword does not mean “run on another thread”. To push some code onto another thread, you need to do it explicitly, e.g., Task.Run: await Task.Run(() => Logger.LogInfo(“Pushing new call {0} with {1} id”.Fill(callNotificationInfo.CallerId)); I have an async/await intro post that you may find helpful.

How to implement INotifyPropertyChanged in C# 6.0?

After incorporating the various changes, the code will look like this. I’ve highlighted with comments the parts that changed and how each one helps public class Data : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { //C# 6 null-safe operator. No need to check for event listeners //If there … Read more

Task.Yield – real usages?

When you see: await Task.Yield(); you can think about it this way: await Task.Factory.StartNew( () => {}, CancellationToken.None, TaskCreationOptions.None, SynchronizationContext.Current != null? TaskScheduler.FromCurrentSynchronizationContext(): TaskScheduler.Current); All this does is makes sure the continuation will happen asynchronously in the future. By asynchronously I mean that the execution control will return to the caller of the async method, … Read more

Is async/await suitable for methods that are both IO and CPU bound?

There are two good answers already, but to add my 0.02… If you’re talking about consuming asynchronous operations, async/await works excellently for both I/O-bound and CPU-bound. I think the MSDN docs do have a slight slant towards producing asynchronous operations, in which case you do want to use TaskCompletionSource (or similar) for I/O-bound and Task.Run … Read more

Do the new C# 5.0 ‘async’ and ‘await’ keywords use multiple cores?

Two new keywords added to the C# 5.0 language are async and await, both of which work hand in hand to run a C# method asynchronously without blocking the calling thread. That gets across the purpose of the feature, but it gives too much “credit” to the async/await feature. Let me be very, very clear … Read more

Await in catch block

Update: C# 6.0 supports await in catch Old Answer: You can rewrite that code to move the await from the catch block using a flag: WebClient wc = new WebClient(); string result = null; bool downloadSucceeded; try { result = await wc.DownloadStringTaskAsync( new Uri( “http://badurl” ) ); downloadSucceeded = true; } catch { downloadSucceeded = … Read more