What is the difference between these two Unit Test statements?

Moved through the following MS Unit Test:

[TestMethod] public void PersonRepository_AddressCountForSinglePerson_IsNotEqualToZero() { // Arrange. Person person; // Act. person = personRepository.FindSingle(1); // Assert. Assert.AreNotEqual<int>(person.Addresses.Count, 0); } 

I have never seen the use of generics in making statements.

This is how I write the statement:

 // Assert. Assert.AreNotEqual(person.Addresses.Count, 0); 

What is the difference?

When I'm overloaded with the AreNotEqual() overload that I use, the method uses an overload that compares two doubles (not sure why there is no int, int overload).

And if I do , enter the generic parameter type <int> , ReSharper says it's redundant.

So my question is: if the way I do this is still type safe, why use general statements?

+7
generics c # unit-testing assertions mstest
source share
1 answer

So my question is: if the method I'm doing is still type safe, why use general statements?

Now you are using the general statement. The compiler sees the general method and selects it at compile time. That's why Resharper says that <int> redundant - this is not because the method is redundant, but rather that the compiler can implicitly determine that the Assert.AreNotEqual<int> method is the correct method.

In general, you often do not need to specify a type in generics. This is what makes LINQ syntax bearable - without it, you will need to specify types each time the method is called in the chain, and anonymous types are unusable.

As the saying goes, there are times when you need to specify a generic type. This is mainly if the compiler cannot determine the type automatically, for example, if there is no corresponding parameter in the argument list (i.e. Foo.Create<MyClass>() ), or if the type cannot be determined for any other reason.

+5
source share

All Articles