Oliver Orchard Oliver Orchard - 1 year ago 85
C# Question

C# Best Practice - What is the best practice when converting int? to int

I have a nullable int coming in from the database from a stored procedure that I know will not have a null value. I have done the bellow:

public List<EngineerDetails> GetCarouselEngineerDetailsList(int customerID)
using (var db = new MainEntities80())
var carouselEngineerDetailsList = db.uspUIDashboardEngineerCarousel().Select(s => new EngineerDetails()
EmployeeID = s.EmployeeID,
EngineerName = s.EngineerName,
PhotoFace = Encoding.Default.GetString(s.PhotoFace),
Status = s.Status,
CurrentJobID = s.CurrentJobID.Value,
CurrentSiteName = s.CurrentSiteName,
NextJobID = s.NextJobID.Value,
NextSiteName = s.NextSiteName,
TotalVisitsToday = s.TotalVisitsToday,
CompletedVisitsToday = s.CompletedVisitsToday

return carouselEngineerDetailsList;

But I wanted to know although I know that the value will always be there. Is it good practice to check before getting the value. Maybe with a turnary expression.
Or because we know it will not be null should we forget about the check?

Answer Source

An InvalidOperationException will be thrown if s.CurrentJobId is actually null anyway. That's almost always the best outcome for situations of "the world is not the way I expect it to be" so it makes sense to use the code exactly as-is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download