Gendarme - AvoidUninstantiatedInternalClassesRule

18 views
Skip to first unread message

Nidhi Rawal

unread,
Aug 20, 2007, 7:56:38 PM8/20/07
to mono-soc-2007
Description:
An instance of an assembly-level type is not created by code within
the assembly.

Examples:

1) internal class UninstantiatedInternalClass
{
public void display ()
{
Console.WriteLine ("Display...");
}
public static void Main (string [] args)
{
}
}
This is bad.

2) internal class InstantiatedInternalClass
{
public void display ()
{
Console.WriteLine ("Display...");
}
public static void Main (string [] args)
{
InstantiatedInternalClass i = new
InstantiatedInternalClass ();
i.display ();
}
}
This is good.

3) using System;
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleToAttribute("MyAssembly")]
internal class UninstantiatedVisibleInternalClass
{
internal UninstantiatedVisibleInternalClass ()
{
}
public void display ()
{
Console.WriteLine ("Display...");
}
}
This should not raise error as the class is visible to other
assemblies, it is possible to have been instantiated there.

4) static internal class StaticClassThatCannotBeInstantiated
{
public static void display ()
{
Console.WriteLine ("Display..");
}
public static void Main (string [] args)
{
}
}
This should not generate an error as the class is static and so
cannot be instantiated.

5) internal class Book
{
public static Book Create()
{
return new Book();
}
}
This is bad because instance is returned by the method that is
never used.

Reference:
http://blogs.msdn.com/fxcop/archive/2007/08/09/what-rules-do-microsoft-have-turned-on-internally.aspx

Nidhi Rawal

unread,
Aug 23, 2007, 4:34:41 AM8/23/07
to mono-soc-2007
Reply all
Reply to author
Forward
0 new messages