Almost all of the answers given here are correct, but you should probably give an example:
public static string GetSecondWord(string text) {
AreNotEqual and AreNotSame are just the inversions of AreEqual and AreSame , of course.
EDIT: Denial of the currently accepted answer ...
If you use Assert.AreSame with type values, they are put into the box. In other words, this is equivalent to doing:
int firstNumber = 1; int secondNumber = 1; object boxedFirstNumber = firstNumber; object boxedSecondNumber = secondNumber; // There are overloads for AreEqual for various value types // (assuming NUnit here) Assert.AreEqual(firstNumber, secondNumber); // ... but not for AreSame, as it not intended for use with value types Assert.AreSame(boxedFirstNumber, boxedSecondNumber);
Neither firstNumber nor secondNumber have an object value, because int is a value type. The reason the AreSame call fails is because in .NET the box value creates a new box every time. (In Java, sometimes this is not the case - it has already bit me.)
Basically, you should never use AreSame when comparing value types. When you compare link types, use AreSame if you want to check for identical links; use AreEqual to check equivalence in Equals . EDIT: Note that there are situations where NUnit does not just use Equals directly; it has built-in support for collections, where items in collections are checked for equality.
The requirement in response is that:
Using the above example, changing int to string, AreSame and AreEqual will return the same value.
depends entirely on how the variables are initialized. If they use string literals, then, nevertheless, interning will take care of this. If, however, you use:
string firstString = 1.ToString(); string secondString = 1.ToString();
then AreSame and AreEqual will almost certainly not return the same value.
Concerning:
A general rule is to use AreEqual over value types and AreSame on reference types.
I almost never want to verify link identification. This is rarely useful to me. I want to check for equivalence, which AreEqual checks. (I'm not saying that AreSame should not be there - this is a useful method, much less often than AreEqual .)