[英]Why does NUnit's Assert.That(..) have ref overloads?
Maybe I'm just being dumb but when and why would you use: 也许我只是傻瓜,但是什么时候以及为什么要使用:
NUnit.Framework.Assert.That<T>(ref T, NUnit.Framework.Constraints.IResolveConstraint, string, params object[])
NUnit.Framework.Assert.That<T>(ref T, NUnit.Framework.Constraints.IResolveConstraint, string)
NUnit.Framework.Assert.That<T>(ref T, NUnit.Framework.Constraints.IResolveConstraint)
in place of: 代替:
NUnit.Framework.Assert.That(object, NUnit.Framework.Constraints.IResolveConstraint, string, params object[])
NUnit.Framework.Assert.That(object, NUnit.Framework.Constraints.IResolveConstraint, string)
NUnit.Framework.Assert.That(object, NUnit.Framework.Constraints.IResolveConstraint)
What advantage does passing by ref bring to this these methods? 引用传递给这些方法带来什么好处?
Digging into NUnit source code, I found this: 深入研究NUnit源代码,我发现了这一点:
static public void That<T>(ref T actual, IResolveConstraint expression, string message, params object[] args)
{
Constraint constraint = expression.Resolve();
Assert.IncrementAssertCount();
if (!constraint.Matches(ref actual))
{
MessageWriter writer = new TextMessageWriter(message, args);
constraint.WriteMessageTo(writer);
throw new AssertionException(writer.ToString());
}
}
public virtual bool Matches<T>(ref T actual)
{
return Matches(actual);
}
versus: 与:
static public void That(object actual, IResolveConstraint expression, string message, params object[] args)
{
Constraint constraint = expression.Resolve();
Assert.IncrementAssertCount();
if (!constraint.Matches(actual))
{
MessageWriter writer = new TextMessageWriter(message, args);
constraint.WriteMessageTo(writer);
throw new AssertionException(writer.ToString());
}
}
As you can see, there is no difference in the implementation. 如您所见,实现没有什么区别。 The Ref T actual
overload allows you to pass value types as reference as well, while reference types are already passed as reference. Ref T actual
重载允许您也将值类型作为引用传递,而引用类型已经作为引用传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.