Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

291
Visualizações
C# Await, null-coalescing, string interpolation does not compile

When conditionally awaiting a task using the null coalescing operator inside a string interpolation, I got an unexpected compilation error that my async method lacks an await, and that await isn't possible outside of an async context:

using System;
using System.Threading.Tasks;
                    
public class Program
{
    public static async Task Main()
    {
        Task<string> isNull = null;
        var result = "World";
        var helloWorld = $"Hello {await (isNull ?? Task.FromResult(result))}";
        Console.WriteLine(helloWorld);
    }
}
Compilation error (line 10, col 29): The name 'await' does not exist in the current context
Compilation error (line 6, col 27): This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

I assume this is due to some compilator details that I am not aware of, and can't be avoided, but I would like to understand it.

Link to fiddle describing issue

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It looks like you encountered a roslyn bug: https://github.com/dotnet/roslyn/issues/39149

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda