Reputation: 9910
Why is Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNotNull()
declared as:
public static void IsNotNull(Object value)
and not:
public static void IsNotNull<T>(T value) where T : class
I just think that some less experienced developers will pass in a value type thinking that it will throw an exception if the value is 0.
UPDATE: Is there a way to constrain the parameter to a reference type in C# 1.0?
Upvotes: 3
Views: 801
Reputation: 113342
What if one has code which can, in different situations produce non-nullable value types and either or both of nullable value types and reference types?
Aside from there not being a good way to cover both reference types and nullable value types in a constraint, the sort of code I just described is no longer testable for situations where it should not result in null. (E.g. Enumerable.Max()
can return all of those types, but must never return null if there was at least one non-null value in the sequence, and must return null if the type can be null and the sequence is empty).
Reducing functionality to avoid mistaken use is only valuable if you can show that all such uses are also mistaken. Here that can not only not be shown to be true, but it can be shown to be false.
Upvotes: 3
Reputation: 10875
As I said in a comment, the answer to "why" is because generics were not available in C# 1.0 when Assert.IsNotNull
was written. Changing it, while arguably (much) more correct, has the potential to break existing code. For better or worse, Microsoft places a very high priority on keeping existing code working.
Upvotes: 3
Reputation: 1381
Because the purpose of the function is to assert that value != null
, and null
is a value that is independent of any type parameters, so adding a type parameter would be pointless overhead.
Upvotes: -2