Why this matters
Using new Guid() instead of Guid.Empty or Guid.NewGuid() can be misleading and lead to unintended behavior.
new Guid() When Not NeededUsing new Guid() instead of Guid.Empty or Guid.NewGuid() can be misleading and lead to unintended behavior.
Using new Guid() instead of Guid.Empty or Guid.NewGuid() can be misleading and lead to unintended behavior.
Side-by-side examples engineers can pattern-match during review.
public void Foo()
{
var g1 = new Guid(); // Noncompliant - what's the intent?
Guid g2 = new(); // Noncompliant
var g3 = default(Guid); // Noncompliant
Guid g4 = default; // Noncompliant
}public void Foo(byte[] bytes)
{
var g1 = Guid.Empty;
var g2 = Guid.NewGuid();
var g3 = new Guid(bytes);
}From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.