Why does NUnit Assert.That (..) have a ref overload?

Maybe I'm just dumb, but when and why will 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) 

instead:

 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 is the benefit of going through ref with these methods?

+8
c # nunit
source share
1 answer

Delving into the source code of NUnit, I found this:

 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); } 

against

  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 implementation. Overloading Ref T actual also allows you to pass value types as a reference, while reference types are already passed as a reference.

+5
source share

All Articles