Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

336
Vistas
What does a question mark after a reference type mean in C#?

What is the purpose of the question mark after the type of the parameter which is a reference type?

e.g one of the constructors for an OverflowException contains string? and Exception? parameters:

public OverflowException(string? message, Exception? innerException);

I don't see a point in marking them as nullable as those are not value types, so they are nullable by default.

What is the point? Does it mean something different?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

In C# 8.0, nullable reference types were introduced - this allows you to mark reference types as nullable by appending ? to the type name.

In null-state analysis, the code is statically checked to keep track of the null-state of references. This analysis can result in one of the 2 states below:

  • not-null - variable has a non-null value

  • maybe-null - variable can be null or not-null; the analyser can't determine for sure

This is useful as it helps the compiler provide warnings to help you write code less prone to NullReferenceExceptions and more consumable by other APIs.

For example, the compiler will emit a warning for the below.

#nullable enable
...

string message = null;

// Warning - CS8602 Dereference of a possibly null reference

Console.WriteLine(message.Length);

This is because since message was set to null, its state was tracked as maybe-null. This throws a warning to alert you just in case you're trying to access the properties of a null object. And yes, you are.

Here's a different example:

#nullable enable
...

string message = "The quick brown fox jumps over the lazy dog";

// No warning
Console.WriteLine(message.Length);

Since message is known to not be null, its state is set to not-null. The compiler won't produce a warning as it knows that you can dereference the variable safely. It is definitely not null.

Alternatively, you can also just decide to tell the world that some variables can be null :)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda