Visual Studio Team System
Avoid uninstantiated internal classes

TypeName

AvoidUninstantiatedInternalClasses

CheckId

CA1812

Category

Microsoft.Performance

Breaking Change

NonBreaking

Cause

An instance of an assembly-level type is not created by code within the assembly. The following types are not examined by this rule:

  • Value types.

  • Abstract types.

  • Enumerations.

  • Delegates.

  • Compiler-emitted array types.

  • Uninstantiable types that define static methods only.

Rule Description

This rule tries to locate a call to one of the type's constructors, and reports a violation if no call is found.

How to Fix Violations

To fix a violation of this rule, remove the type, or add the code that uses it. If the type contains only static methods, add one of the following to the type to prevent the compiler from emitting a default public instance constructor:

  • A private constructor (for types targeting .NET Framework versions 1.0 and 1.1).

  • The static modifier (for types targeting .NET Framework 2.0).

When to Exclude Warnings

It is safe to exclude a warning from this rule, but there are no known scenarios where this is required.

Related Rules

Avoid uncalled private code

Review unused parameters

Remove unused locals

Tags :


Community Content

David M. Kean - MSFT
Does not fire on internal constructors if InternalsVisibleToAttribute is present

Currently, if you have the InternalsVisibleToAttribute applied to the assembly being analyzed, this rule will not fire on any constructors marked as internal (Friend in Visual Basic, public private in C++) as it cannot be sure that a field is not being used by another friend assembly.

While it is not possible to work around this limitation in Visual Studio Code Analysis, the external standalone FxCop will fire on internal constructors if every friend assembly is present in the analysis.

Tags :

David M. Kean - MSFT
Fires false positives in some situations

Although this rule states that there are no known scenarios where it is required to exclude (or suppress) this warning, there a couple of instances where this rule will incorrectly fire a false positive:

  • If the class is created via late-bound Reflection methods such as Activator.CreateInstance.
  • If the class is created automatically by the runtime or ASP.NET, for example, classes that implement IConfigurationSectionHandler or IHttpHandler.
  • If the class is an implementation of a interface that represents a Windows Communication Foundation (WCF) service contract.
  • If the class is passed as generic type parameter with a new() constraint. For example, the following will fire this rule:

[C#]
 
internal class Foo
{
public Foo()
{
}
}
 
public class Bar<T> where T : new()
{
public T Create()
{
return new T();
}
}
  

[...]
 
Bar<Foo> bar = new Bar<Foo>();
bar.Create();

In these situations it is safe (and recommended) to simply suppress the warning.

Tags : code c#

David M. Kean - MSFT
Fires on types that instantiated by unused methods on the same type

This rule will also fire on types that are only ever instantiated by unused methods on that exist on the same type. This is by-design.

For example, the following type will fire this rule:

[C#]
 
internal class Book
{
public static Book Create()
{
return new Book();
}
}

[Visual Basic]
 
Friend Class Book
 
    Public Shared Function Create() As Book
Return New Book()
End Function
 
End Class

Tags : code c# vb

Page view tracker