2024-06-27 12:08:26 +00:00

60 lines
2.0 KiB
Plaintext

:operationName: type or member
:visibility: private/internal
include::../why.adoc[]
=== Exceptions
This rule doesn't raise issues on:
* empty constructors
* members with attributes
* the `Main` method of the application
* `void` methods with two parameters when the second parameter type derives from https://learn.microsoft.com/en-us/dotnet/api/system.eventargs[EventArgs]
* empty serialization constructor on type with https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute[System.SerializableAttribute] attribute.
* field and property members of types marked with https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute[System.SerializableAttribute] attribute
* internal members in assemblies that have a https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute[System.Runtime.CompilerServices.InternalsVisibleToAttribute] attribute.
* types and members decorated with the https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] attribute (available in .NET 5.0+) or a custom attribute named `DynamicallyAccessedMembersAttribute`.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public class Foo
{
private void UnusedPrivateMethod(){...} // Noncompliant, this private method is unused and can be removed.
private class UnusedClass {...} // Noncompliant, unused private class that can be removed.
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
public class Foo
{
public Foo()
{
UsedPrivateMethod();
}
private void UsedPrivateMethod()
{
var c = new UsedClass();
}
private class UsedClass {...}
}
----
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers[Access Modifiers (C# Programming Guide)]
include::rspecator.adoc[]