I have a method that returns null if the postcode is invalid, and just returns the string if it is valid. It also transforms the data in certain cases.
I have the following unit test below but I am getting syntax errors on the lines where it is using string?. Could anyone tell me why?
public void IsValidUkPostcodeTest_ValidPostcode()
{
MockSyntaxValidator target = new MockSyntaxValidator("", 0);
string fieldValue = "BB1 1BB";
string fieldName = "";
int lineNumber = 0;
string? expected = "BB1 1BB";
string? actual;
actual = target.IsValidUkPostcode(fieldValue, fieldName, lineNumber);
Assert.AreEqual(expected, actual);
}
The
?suffix on the name of a type is an alias for usingNullable<T>(section 4.1.10 of the C# 4 spec). The type parameter forNullable<T>has thestructconstraint:This constrains
Tto be a non-nullable value type. That prohibits you from usingstring, asSystem.Stringis a reference type.Fortunately, as string is a reference type, you don’t need to use
Nullable<T>– it already has a null value (the null reference):