James
James

Reputation: 356

Compounded Null Checking With Non-Nullable Parameter

I've been looking through some MS reference material for an upcoming exam and have found a supposedly previous question where I disagree with the answer.

Upon consideration, I'm going to post a screenshot of the question rather than condense the problem just in case I abstract out any pertinent information to the question at hand.

enter image description here

The issue I have is that they give the answer as B whereas I think it should be A for the following reasons.

The where clause in the LINQ query has two criteria it has to compare against one where the Years match in the DateTime? object and the method parameter.

However, I'm more interested in why they think a null check is necessary due to the parameter type being a non-nullable int. Unassignment and attempting to assign null prior to passing the parameter by value will result in a compiler error.

If the year param can never be null, there will never be a null DateTime match - rendering the null check superfluous.

I can see why B would also yield expected results, but is there anything wrong with answering A.

(P.s. I've read - Comparing non nullable `int` to `null` (LINQ) which seems to back up my theory, I'm just squeamish about disagreeing with reference material)

Upvotes: 1

Views: 86

Answers (2)

TJR
TJR

Reputation: 3732

The application must meet the following requirements:

  • Return only orders that have an OrderDate value other than null.
  • Return only orders that were placed in the year specified in the year parameter

Also, line 1 seems to be showing you the definition of OrderDate

public DateTime? OrderDate;

The question is telling you that the OrderDate is a nullable DateTime object, and that you need to check that it has a value.

Edit: I did try this for myself using SQLEXPRESS and LINQPad 5, and I see now that answer A does work in the context of LINQ to SQL because it gets converted to

WHERE DATEPART(Year,OrderDate) = @year

And this will filter out null OrderDates.

All I can say, then, is why I believe answer B is the "preferred" answer.

Answer A in all other C# contexts will throw an InvalidOperationException "Nullable object must have a value", and it just happens to work in this context because it is LINQ to SQL.

Answer B will work both in a C# context and in a LINQ to SQL context, so of the two possible answers, it is the safest, and will help prevent you from using an answer like A in a context where it would fail. Also, I believe that it communicates more clearly and explicitly that you want to filter out null order dates. There may be a minor performance hit doing the additional null check, but unless there is a measurable performance problem, then readability is more important than minor performance improvements. If I did use answer A, I would include plenty of comments describing the LINQ to SQL behavior explaining why the null check is not necessary.

Upvotes: 0

Ibrahem Uwk
Ibrahem Uwk

Reputation: 135

Suppose you choose A , if the Date is null you will get the following Exception

Nullable object must have a value.
  + System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource)
  + Nullable<T>.get_Value()

for example

DateTime? dt = null;
 if (dt.Value.Year == 2010) Console.WriteLine("Accepted Value");

Give Error

Upvotes: 1

Related Questions