Consider the following class:
C#class MyClass
{
private readonly Version m_version1;
private readonly Version m_version2;
public MyClass(Version version1, Version version2)
{
m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null.");
m_version2 = version2;
}
}
If invoking the "Add Contract->Throw Exceptions Contract" in the constructor the following code is generated:
C#public MyClass(Version version1, Version version2)
{
if (version1 == null)
{
throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null.");
}
if (version2 == null)
{
throw new ArgumentNullException(nameof(version2), $"{nameof(version2)} is null.");
}
m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null.");
m_version2 = version2;
}
There is a redundant null check for version1 now . Expected the following code:
C#public MyClass(Version version1, Version version2)
{
if (version2 == null)
{
throw new ArgumentNullException(nameof(version2), $"{nameof(version2)} is null.");
}
m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null.");
m_version2 = version2;
}
or possibly the more compact (should probably be controllable by some option in that case):
C#public MyClass(Version version1, Version version2)
{
m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null.");
m_version2 = version2 ?? throw new ArgumentNullException(nameof(version2), $"{nameof(version2)} is null.");
}
Hi Peter,
Thanks for the code snippets.
We are working on this issue now and will notify you when we have any results.