
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
102 lines
2.4 KiB
Plaintext
102 lines
2.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
There's no point in having a ``++public++`` member in a non-``++public++`` type because objects that can't access the type will never have the chance to access the member.
|
|
|
|
|
|
This rule raises an issue when a type has methods, fields, or inner types with higher visibility than the type itself has.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,csharp]
|
|
----
|
|
internal class MyClass
|
|
{
|
|
public static decimal PI = 3.14m; // Noncompliant
|
|
|
|
public int GetOne() // Noncompliant
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
protected record NestedType // Noncompliant: outer class is internal
|
|
{
|
|
public bool FlipCoin() // Noncompliant: outer class is internal
|
|
{
|
|
return false;
|
|
}
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,csharp]
|
|
----
|
|
public class MyClass // Class visibility upgrade makes members compliant
|
|
{
|
|
public static decimal PI = 3.14m;
|
|
|
|
public int GetOne()
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
protected record NestedType
|
|
{
|
|
public bool FlipCoin() // Outer type is public
|
|
{
|
|
return false;
|
|
}
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Exceptions
|
|
|
|
User defined operators need to be public:
|
|
|
|
[source,csharp]
|
|
----
|
|
public static implicit operator byte(MyClass a) => 1; // Compliant
|
|
public static explicit operator MyClass(byte a) => new MyClass(a); // Compliant
|
|
----
|
|
|
|
Nested types, even if private, can be used and inherited in the parent type. In this case, the visibility of the outer type is considered.
|
|
|
|
[source,csharp]
|
|
----
|
|
internal class MyClass
|
|
{
|
|
private class NestedClass
|
|
{
|
|
public int PublicProperty { get; } // Noncompliant: should be internal
|
|
protected internal int ProtectedInternalProperty { get; } // Compliant: can be used in `InternalsVisibleTo` assemblies
|
|
internal int InternalProperty { get; } // Compliant: can be used in `InternalsVisibleTo` assemblies
|
|
protected int ProtectedProperty { get; } // Compliant: can be used in derived type
|
|
private protected int PrivateProtectedProperty { get; } // Compliant: can be used in derived type
|
|
private int PrivateProperty { get; }
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|