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

217
Vistas
How to know if method argument is a nullable type

I have a method which receives a dictionary of string and object

private void MyFunc(Dictionary<string, object> parameters)
{
}

I have a use case where I need to call the method with object being a nullable Guid

Nullable<Guid> guid = Guid.NewGuid();
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("myGuid", guid);
MyFunc(parameters);

When I am trying to get object's type it appears to be System.Guid

parameters["myGuid"].GetType()

How can I know if the object is nullable? Am I losing nullable when sending an object as method argument?

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

0

When you call parameters.Add("myGuid", guid) you're boxing the value of guid. When you box a Nullable<T>, the result is either a null reference or a boxed T... there's no trace of the nullability any more. In other words, there's no object representation of a Nullable<T>.

So no, you can't tell that the original type of the guid variable was Nullable<Guid>.

Note that you don't need a dictionary to demonstrate that. This shows the same thing:

Nullable<Guid> guid = Guid.NewGuid();
object boxed = guid;
Type type = boxed.GetType(); // System.Guid
over 4 years ago · Santiago Trujillo Denunciar

0

You have to check whether the dictionary item is same type Nullable<Guid> like below.

if( parameters["myGuid"] is Nullable<Guid>){

}

If you directly call GetType() and the value it was having null value in the dictionary then you would have null exception.

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