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

188
Visualizações
how to parse "" to long("0") in dotnet

given the following code:

string example = "1234";
long parsed_example = long.Parse(example);
Console.Writeline(parsed_example);
# => 1234

Works great.

the following example does not:

string example = "";
long parsed_example = long.Parse(example);
# [System.FormatException: Input string was not in a correct format.]

However the goal is:

string example = "";
if (example == "")
{
    example = "0";
}
long parsed_example = long.Parse(example);
Console.Writeline(parsed_example);
# => 0

is there a shorter, apropriate solution? The above code would almost justify a tiny function, Id preferable have a inline solution. Maybe something such as (pseudo code):

string example = "";
long parsed_example = example ?? 0, long.Parse(example);
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

long parsed_example = example == "" ? 0 : long.Parse(example);

However: don't be obsessed with single-line solutions; a multi-line solution is often more readable and correct. There are no prizes for creating complex code. You may also wish to look at string.IsNullOrWhiteSpace, long.TryParse, etc. For example:

long value;
if (string.IsNullOrWhiteSpace(example))
{
    // what you want to do with blank/empty values
    value = 42;
}
else if (!long.TryParse(example, out value))
{
    // what you want to do with non-integer values 
   value = 84;
}
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