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

214
Views
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 answers
Answer question

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 Report

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 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!