Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.IsNotNull() should constrain parameter to class

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?

like image 681
David Klempfner Avatar asked May 05 '26 01:05

David Klempfner


1 Answers

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.

like image 130
Ðаn Avatar answered May 09 '26 14:05

Ðаn