Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot convert from out System.Guid? to System.Guid

I’m trying to parse string into nullable guid. Depending on the TryParse result nullable guid should store value or be null.

private Guid? GetData()
{
    string carName = "Volvo";
    Guid? data = Guid.TryParse(carName, out data) ? (Guid?)data : null;
    
    return data;
}

I’m getting a compile-time error on out data with message

Cannot convert from out System.Guid? to System.Guid

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use a different variable name:

Guid? data = Guid.TryParse(carName, out Guid _data) ? (Guid?)_data : null;

Currently, you use data for both

  • the end result (a Guid?) and
  • the out parameter of TryParse (which needs to be a Guid, not a Guid?).
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading