RuleDescriptionExampleKPI
AllocationOfReferenceTypeEnumeratorNon-ValueType enumerator may result in an heap allocation.Non-complaint Code:

IList iListData = new[] { 123, 32, 4 };
foreach (var i in iListData) // Allocations (line 19)
{
Console.WriteLine(i);
}


Complaint Code:

int[] intData = new[] { 123, 32, 4 };
foreach (var i in intData)
{
Console.WriteLine(i);
}

Efficiency
ArrayAllocationForParamsParameterUnnecessary array allocation will occur even if no parameter is passed in for the params parameter.Non-complaint Code:

class Test
{
void Method()
{
Params(1, 2);
}
public void Params(params int[] args)
{
}
}


Complaint Code:

class Test
{
void Method()
{
Params(new[] { 1, 2 });

}
public void Params(params int[] args)
{
}
}
Efficiency
CodeAnalysisSuppressionMustHaveJustificationThe SuppressMessage attribute does not include a justification. Justification increases the long term maintainability of the code.Cause:
A violation of this rule occurs when the code contains a Code Analysis SuppressMessage attribute, but a justification for the suppression has not been provided within the attribute.Whenever a Code Analysis rule is suppressed, a justification should be provided.This can increase the long-term maintainability of the code.


Complaint Code:

[SuppressMessage(“Microsoft.Performance”, “TestSuppressAttribute”, Justification = “Used for testing”)]public bool Method1()
{

}

To fix an instance of this violation, add a Justification tag and justification text to the SuppressMessage attribute describing the reason for the suppression.

Robustness
DisposablesShouldCallSuppressFinalizeClasses implementing IDisposable should call the GC.SuppressFinalize method in their finalize method to avoid any finalizer from being called. This rule should be followed even if the class doesn’t have a finalizer as a derived class could have one.Cause:
Classes implementing IDisposable should call the GC.SuppressFinalize method in their finalize method to avoid any finalizer from being called. This rule should be followed even if the class doesn’t have a finalizer as a derived class could have one.

Non-complaint code:
public class MyClass : System.IDisposable
{
public void Dispose()
{
var x = 1;
}
}


Compliant code:
public class MyClass : System.IDisposable
{
public void Dispose()
{
var x = 1;
GC.SuppressFinalize(this);
}
}
Robustness
DoNotChangeLoopVariablesDon’t change a loop variable inside a for loop.Robustness
DontConcatenateStringsInLoopsDon’t concatenate strings in a loop. Using a StringBuilder will require less memory and time.Cause:
Do not concatenate a string on a loop. It will allocate a lot of memory. Use a StringBuilder instead.
It will will require less allocation, less garbage collector work, less CPU cycles, and less overall time.

Non-complaint code:

var values = “”;
foreach (var item in Enum.GetValues(typeof(LoaderOptimization)))
{
values += item.ToString();
}


Complaint code:

var values = “”;
var builder = new StringBuilder();
builder.Append(values);
foreach (var item in Enum.GetValues(typeof(LoaderOptimization)))
{
builder.Append(item.ToString());
}
values = builder.ToString();
Efficiency
MakeFieldReadonlyA field that is only assigned in the constructor can be made readonly.Cause:
A field that is only assigned when declared or in the constructor can be made read-only. A read-only field cannot be assigned anywhere else after the object is created, making it immutable.

Non-complaint code:

class Class1
{
private int x = 0;
}


Complaint code:

class Class1
{
private readonly int x = 0;
}
Maintainability
NonOverriddenVirtualMethodCallOnValueTypeNon-overridden virtual method is called on a value type.Cause:
An additional boxing or constrained instruction is needed for a non-overridden virtual method call on a value type because the CLR will automatically instantiate the corresponding class from a value type if you call some method on it.

Complaint code:

using System;

var normal = new Normal().GetHashCode();
var overridden = new OverrideToHashCode().GetHashCode();

struct Normal
{

}

struct OverrideToHashCode
{

public override int GetHashCode()
{
return -1;
}
}

Efficiency
RemoveEmptyFinalizersAn empty finalizer will stop your object from being collected immediately by the Garbage Collector when no longer used. It will instead be placed in the finalizer queue needlessly using resources.Cause:
Due to the finalize method, GC will not clear the entire memory associated with object in the first attempt. Memory used by managed resources is still in heap as inaccessible reference. Hence it is recommended that the Finalize method should not be used until it is extremely necessary.

Non-complaint code:

public class MyClass
{
//At runtime C# destructor is automatically Converted to Finalize method
~MyClass()
{
Dispose(false);
}
}



Complaint code:
public class MyClass
{
}
Resource Utilization
SwitchWithoutDefaultClauseConsider adding default case to the switchNon-complaint code:

switch (value)
{
case 1:
Console.WriteLine(1);
break;
case 2:
Console.WriteLine(2);
break;
}


Complaint code:
switch (value)
{
case 1:
Console.WriteLine(1);
break;
case 2:
Console.WriteLine(2);
break;
default:
Console.WriteLine(0);
break;
}
Robustness
Invalid Logging Class NameConsider creating the logger name in context to the current class name.// Cause:
//The log instace created should belong to the current class for logging consistency, consider changing the log class name to current class.

//Example For Issue Occurrence:

class ExampleClass
{
private static ILog logger = LogManager.GetLogger("otherClass");

static void Main(string[] args)
{
try
{
logger.Info("");
}
catch (Exception e)
{
}
}
}

//CODE FIX:

class ExampleClass
{
private static ILog logger = LogManager.GetLogger("ExampleClass");

static void Main(string[] args)
{
try
{
logger.Info("");
}
catch (Exception e)
{
}
}
}
Usage
Class Implements ICloneableConsider defining your own Clone() or Copy() methods and document whether they perform deep or shallow copying instead of using ICloneable interface.// Cause:
//Implementing 'ICloneable' is discouraged due to its ambiguous effect on the code.

//Example For Issue Occurrence:

using System;

public class Class1 : ICloneable
{
public int a;
public int b;

public Class1(int aa, int bb)
{
a = aa;
b = bb;
}

public string ToString()
{
return "(" + a + "," + b + ")";
}

public virtual object Cloning()
{
return new Class1(a, b);
}
object ICloneable.Clone()
{
return Cloning();
}
}
Reliability