I have the below method that accepts BuildAppStat
object as parameter.
However it can be null sometimes.
So I am calling it like as :
sourceId: bas.BuildAppStatId ?? null
and getting compile error
operator ' ' cannot be applied to operand of type
<int>
andnull
.
How can I fix this?
public PageBuildVrsn EditPageBuildVrsn(string caption, string nameInUse, string description, BuildAppStat bas = null, int pageBuildVrsnTypeId = 1)
{
PageBuildVrsn pbv = Create(pageBuildVrsnTypeId, caption, nameInUse, description);
pbv.ParentBuildAppStats = new List<BuildAppStat>();
pbv.ParentBuildAppStats.Add(
BasFactory.Create(
DateTimeOffset.Now,
true,
sourceId: bas.BuildAppStatId ?? null
)
);
return pbv;
}
You can only use the null-coalescing operator with a first operand which is a nullable type (either a nullable value type, or any reference type). It sounds like bas.BuildAppStatId
is just an int
, which means you can't use the null-coalescing operator with it.
If you were trying to guard against bas
itself being null, you want:
sourceId: bas == null ? (int?) null : bas.BuildAppStatId,