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
>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
outparameter ofTryParse(which needs to be aGuid, not aGuid?).