Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
¿Por qué no está compilando esto? ¿No se puede convertir de 'X?' a 'X'

Estoy enfrentando un problema inesperado (¿o es este el comportamiento esperado?). El siguiente código no se está completando y me está dando el error:

CS1503 Argument 1: cannot convert from 'long?' to 'long'

 public static void Add(long? ticks) { if (ticks != null) { new DateTime(ticks); } }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

No hay una conversión implícita de Nullable<T> a T ; puede hacerlo explícitamente a través de una conversión, por ejemplo, new DateTime((long) ticks) , o usar la propiedad Value , por ejemplo, new DateTime(ticks.Value) .

Como alternativa que generalmente preferiría ahora, puede usar la coincidencia de patrones de C# 7 para hacerlo un poco más simple, extrayendo el valor no nulo en el mismo paso donde verifica que no sea nulo:

 public static void Add(long? ticks) { // This will match if ticks is non-null, and assign the value // into the newly-introduced variable "actualTicks" if (ticks is long actualTicks) { var dt = new DateTime(actualTicks); // Presumably use dt here } }
over 4 years ago · Santiago Trujillo Report

0

¿Puedes ampliar implícitamente de long a long? , pero no puede reducir implícitamente desde long? a long

Usted necesita hacer ésto:

 public static void Add(long? ticks) { if (ticks != null) { new DateTime(ticks.Value); // or (long)ticks } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!