CHALLENGE: What does this code do?

What should be the execution result of the following code?

using System;
using System.Threading.Tasks;

using static System.Console;
using static System.IO.File;

class Program
{
    static void Main(string[] args)
    {
        Run();
        ReadLine();
    }
    static void Run()
    {
        var task = ComputeFileLengthAsync(null);
        WriteLine("Computing file length");
        WriteLine(task.Result);
        WriteLine("done!");
    }

    static async Task<int> ComputeFileLengthAsync(string fileName)
    {
        WriteLine("Before If");
        if (fileName == null)
        {
            WriteLine("Inside");
            throw new ArgumentNullException(nameof(fileName));
        }
        WriteLine("After");

        using (var fileStream = OpenText(fileName))
        {
            var content = await fileStream.ReadToEndAsync();
            return content.Length;
        }
    }
}

Hint: it will print “Computing file length” on the output.

Now, can you figure out why? What implementation strategy could I follow to get the exception when calling the function?

Compartilhe este insight:

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

In the first post of this series, I explained how to produce an inverted index using C#. In the second...
It’s time to start learning RavenDB 4. Even if you know previous versions of RavenDB, you will probably get good...
The example which motivated this post comes from the excellent book Designing Distributed Systems by Brendan Burns (co-founder of the...
The following works as expected when building in Debug – the execution is done after three seconds. But, for some...
This one comes from Ayende’s book about RavenDB. If you want to learn RavenDB basics, I would recommend you to...
Empresas, famílias e grupos de amigos mudam. Entretanto, uma coisa permanece constante: o fato de que todos percebemos a vida...