AreNotSame之间的区别

AreNotSame之间的区别

本文介绍了什么是Assert.AreNotEqual和Assert.AreNotSame之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,什么是

之间的区别

  Assert.AreNotEqual
 

  Assert.AreNotSame
 

解决方案

几乎所有这里给出的答案是正确的,但它可能是值得给予一个例子:

 公共静态字符串GetSecondWord(文本字符串)
{
    //是的,一个令人震惊的实现...
    返回text.Split('')[1];
}

串预期=世界;
串实际= GetSecondWord(世界你好);

//很好:这两个字符串应该是*等于*,因为它们具有相同的内容
Assert.AreEqual(预期,实际值);

//错误:两个字符串*引用*会不一样
Assert.AreSame(预期,实际值);
 

AreNotEqual AreNotSame AreEqual 的只是反转和 AreSame 的课程。

编辑:反驳的currently接受的答案 ...

如果您使用 Assert.AreSame 值类型,它们被装箱。换句话说,它是等效于这样做的:

  INT firstNumber = 1;
INT secondNumber = 1;
反对boxedFirstNumber = firstNumber;
反对boxedSecondNumber = secondNumber;

//有重载AreEqual各种值类型
//(假设NUnit的在这里)
Assert.AreEqual(firstNumber,secondNumber);

// ...但不使用AreSame,因为它不适用于值类型使用
Assert.AreSame(boxedFirstNumber,boxedSecondNumber);
 

无论是 firstNumber secondNumber 有一个对象的值,因为 INT 是值类型。究其原因, AreSame 通话将失败,是因为在.NET中,拳击值创建一个新的方块每次。 (在Java中,有时不 - 这之前已经抓到我了)

基本上你应该的永远的使用 AreSame 比较值类型时。当你比较的引用的类型,使用 AreSame 如果你想检查相同的参考;使用 AreEqual 来检查等价等于。编辑:请注意,有的的情况下,NUnit的不只是使用等于直接;它已经内置了对集合,其中在集合中的元素进行测试,以便平等的支持。

在回答声称:

完全取决于变量初始化。如果他们使用的字符串,然后呢,实习将采取照顾。但是,如果你使用:

 字符串firstString = 1.ToString();
字符串secondString = 1.ToString();
 

然后AreSame和AreEqual几乎肯定会的没有的返回相同的值。

至于

我差不多的永远的要检查,以供参考身份。它很少对我很有用。我想检查的等同的这是什么 AreEqual 检查。 (我不是说 AreSame 不应该存在 - 这是一个有用的方法,只是远远很少超过 AreEqual

In C#, what's the difference between

Assert.AreNotEqual

and

Assert.AreNotSame
解决方案

Almost all the answers given here are correct, but it's probably worth giving an example:

public static string GetSecondWord(string text)
{
    // Yes, an appalling implementation...
    return text.Split(' ')[1];
}

string expected = "world";
string actual = GetSecondWord("hello world");

// Good: the two strings should be *equal* as they have the same contents
Assert.AreEqual(expected, actual);

// Bad: the two string *references* won't be the same
Assert.AreSame(expected, actual);

AreNotEqual and AreNotSame are just inversions of AreEqual and AreSame of course.

EDIT: A rebuttal to the currently accepted answer...

If you use Assert.AreSame with value types, they are boxed. In other words, it's 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's not intended for use with value types
Assert.AreSame(boxedFirstNumber, boxedSecondNumber);

Neither firstNumber nor secondNumber has an object value, because int is a value type. The reason the AreSame call will fail is because in .NET, boxing a value creates a new box each time. (In Java it sometimes doesn't - this has caught me out before.)

Basically you should never use AreSame when comparing value types. When you're comparing reference types, use AreSame if you want to check for identical references; use AreEqual to check for equivalence under Equals. EDIT: Note that there are situations where NUnit doesn't just use Equals directly; it has built-in support for collections, where the elements in the collections are tested for equality.

The claim in the answer that:

entirely depends on how the variables are initialized. If they use string literals, then yet, interning will take care of that. If, however, you use:

string firstString = 1.ToString();
string secondString = 1.ToString();

then AreSame and AreEqual will almost certainly not return the same value.

As for:

I almost never want to check for reference identity. It's rarely useful to me. I want to check for equivalence which is what AreEqual checks for. (I'm not saying that AreSame shouldn't be there - it's a useful method, just far more rarely than AreEqual.)

这篇关于什么是Assert.AreNotEqual和Assert.AreNotSame之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:05