2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-09-14 16:28:32 +02:00
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.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-09-14 16:28:32 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-09-14 16:28:32 +02:00
----
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;
}
// ...
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-09-14 16:28:32 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-09-14 16:28:32 +02:00
----
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;
}
// ...
}
}
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-09-14 16:28:32 +02:00
User defined operators need to be public:
2023-05-25 14:18:12 +02:00
[source,csharp]
2021-09-14 16:28:32 +02:00
----
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.
2023-05-25 14:18:12 +02:00
[source,csharp]
2021-09-14 16:28:32 +02:00
----
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; }
}
}
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]