Featured

Service Fabric Handbook

Welcome!

This post is a handbook where you probably will find an answer to “How different things works in Azure Service Fabric?” question. Almost all of the information (around 90%) is from docs.microsoft.com, GitHub and etc., the rest of it are personal findings made during development of CoherentSolutions.Extensions.ServiceFabric.Hosting.

Where possible the information will be confirmed by reliable source (docs, issues or posts).

Continue reading “Service Fabric Handbook”

.NET R&D Digest (September, 2019)

Every month there is something exciting happening in the world of technology. This month (September) was a special one for .NET community because of .NET Core 3.0 release (and if you haven’t noticed that I recommend you to watch these .NET Conf recordings and read this announcement).

Despite the fact we all are overwhelmed by the information and blog posts we now have to read about new features and changes I hope there is still a small room for new issue of .NET R&D Digest (September, 2019) 😊

This issues includes bits of architecture, a story and a few posts about .NET and .NET Internals.

Have a nice read!

Continue reading “.NET R&D Digest (September, 2019)”

What is the difference between ToArray and ToList?

Introduction


In the beginning of June I attended .NET Summit conference in Minsk, Belarus. Besides interesting presentations (recording are available on YouTube, language is Russian / English) there were interesting quizzes.

One of the questions was:

What is the difference between ToArray and ToList?

Software Engineer

Hmm… what could be the difference besides one returns T[] and another one returns List<T>?

If you don’t know the answer or just like digging into sources of dotnet/corefx then I invite you to join me and find out the answer!

Note

All code snippets were taken from release/3.0 branch of dotnet/corefx repository. All code snippets were truncated to make them more representative (removed input argument checks, contracts, …, etc.).

Tests were targeting .NET Core 3.0 preview 7. However most of the information should be relevant for .NET Core 2.0+ and .NET Framework 4.5+.

Continue reading “What is the difference between ToArray and ToList?”

.NET R&D Digest (April, 2019)

There are a lot of good technical articles & books on Web. I see them all the time – when trying to find a solution, when investigating things or simply when reading new posts from my RSS Feed during breakfast.

Having all these knowledge only for myself was a bit selfish. That is why once a month or two I compose some of this content into a so called ‘digest’ for colleagues & friends.

After fourth issues (this one is the fourth) I though – maybe this can be interesting for my readers? So this is the Aprils issue of .NET R&D Digest.

Continue reading “.NET R&D Digest (April, 2019)”

Code Tip: How to work with asynchronous event handlers in C#?

Have you ever faced a need to raise an event in C#? I am almost certain the answer is “Yes, and I have no problems with it“. So did I, but as usual there are some cases when raising events can be tricky.

Imagine a Demo class with single DemoEvent event and Raise method:

public class Demo
{
  public event EventHandler DemoEvent;

  public void Raise()
  {
    this.DemoEvent?.Invoke(this, EventArgs.Empty);
    Console.WriteLine("All handlers have been executed!");
  }
}

In Program.Main create a new instance of Demo and subscribe for DemoEvent:

class Program
{
  static void Main(string[] args)
  {
    var instance = new Demo();
    instance.DemoEvent += (
      sender,
      eventArgs) =>
    {
      Console.WriteLine("Executed!");
    };
    instance.Raise();
  }
}

Application outputs:

Executed!
All handlers have been executed!

Everything is fine until someone replaces synchronous event handler with asynchronous one:

// class: Program / Main()
//...
instance.DemoEvent += async (
  sender,
  eventArgs) =>
{
  await Task.Delay(10);
  Console.WriteLine("Executed!");
};
//...

Outputs:

All handlers have been executed! 

Oh no! One more post about async-await and event handlers! I am out!

Disappointed reader

(Sound of clapping browser page)

Hey await! Just give me a moment! I am promising not to say anything about when you should or shouldn’t use async-await!

This post is about absolutely opposite thing. Here I want to share a way how you can update Demo class to support both synchronous and asynchronous event handlers.

(Sound of incoming HTTP GET)

Just want to see code? The complete sample (with all the improvements) is available as Gist.

Want to know more? Then let’s dig into.

Continue reading “Code Tip: How to work with asynchronous event handlers in C#?”