Why use a public method in an internal class?
Asked Answered
S

13

324

There is a lot of code in one of our projects that looks like this:

internal static class Extensions
{
    public static string AddFoo(this string s)
    {
        if (s == null)
        {
            return "Foo";
        }

        return $({s}Foo);
    }
}

Is there any explicit reason to do this other than "it is easier to make the type public later?"

I suspect it only matters in very strange edge cases (reflection in Silverlight) or not at all.

Stodder answered 15/2, 2012 at 22:7 Comment(5)
In my experience, that's the normal practice.Intense
@phoog, OK. but why is it the normal practice? Maybe just easier then changing a bunch of methods to internal? Quick fix --> mark the type internal instead?Stodder
that's what I've always assumed. I don't have a well-thought-out analysis, however, which is why I didn't post an answer.Intense
Eric Lippert's answer sums up my thinking very well, and also brings up something that was lurking in my brain trying to find a way out: implicit implementations of interface members must be public.Intense
Isn't that method equal to return s + "Foo";? The + operator doesn't care about null or empty strings.Bricky
D
521

UPDATE: This question was the subject of my blog in September 2014. Thanks for the great question!

There is considerable debate on this question even within the compiler team itself.

First off, it's wise to understand the rules. A public member of a class or struct is a member that is accessible to anything that can access the containing type. So a public member of an internal class is effectively internal.

So now, given an internal class, should its members that you wish to access in the assembly be marked as public or internal?

My opinion is: mark such members as public.

I use "public" to mean "this member is not an implementation detail". A protected member is an implementation detail; there is something about it that is going to be needed to make a derived class work. An internal member is an implementation detail; something else internal to this assembly needs the member in order to work correctly. A public member says "this member represents the key, documented functionality provided by this object."

Basically, my attitude is: suppose I decided to make this internal class into a public class. In order to do that, I want to change exactly one thing: the accessibility of the class. If turning an internal class into a public class means that I have to also turn an internal member into a public member, then that member was part of the public surface area of the class, and it should have been public in the first place.

Other people disagree. There is a contingent that says that they want to be able to glance at the declaration of a member and immediately know whether it is going to be called only from internal code.

Unfortunately, that doesn't always work out nicely; for example, an internal class that implements an internal interface still has to have the implementing members marked as public, because they are part of the public surface of the class.

Diacritic answered 15/2, 2012 at 22:42 Comment(6)
I like this answer... it fits well with my attitude towards writing code that is self documenting to the greatest extent possible, and scope is a great way to broadcast intent, especially if your other team members understand your convention.Stodder
+1 for saying what I wanted to say but formulating it much better than I was able (also for bringing up the interface angle, though I note that it's only true of implicit member implementations).Intense
The Interface part is important - it's impossible to implement an interface method using internal, it's either public or explicit interface declaration. So the word public is overloaded with two meanings.Demitria
From an idealogical perspective your argument in favor of public is very convincing. However, I find the "doesn't always work out nicely..." to be especially powerful. Losing consistency devalues any "at-a-glance" benefits. Using internal in this way means deliberately building intuitions that are sometimes wrong. Having a mostly-right intuition is IMO a horrible thing to have for programming.Hypothecate
Important quote from your blog post: "My advice is to discuss the issue amongst your team, make a decision, and then stick to it."Stodder
Does this mean you would never have a internal member in a internal class? Of are there situations where a specific member of the class should stay internal if you later make the class itself public?Funda
A
24

If the class is internal, it doesn't matter from an accessibility standpoint whether you mark a method internal or public. However it is still good to use the type you would use if the class were public.

While some have said that this eases transitions from internal to public. It also serves as part of the description of the method. Internal methods typically are considered unsafe for unfettered access, while public methods are considered to be (mostly) free game.

By using internal or public as you would in a public class, you ensure that you are communicating what style of access is expected, while also easing the work required to make the class public in the future.

Astor answered 15/2, 2012 at 22:18 Comment(2)
I tend towards limiting everything to the greatest extent possible while creating a good API and allowing my intended consumer to do what I intend them to do. I'm also with you on marking the member as I would if the class were public. More specifically, I'd only mark any member public if i consider it always safe. Otherwise somebody else marking the class public later (it happens) could expose non-safe members.Stodder
@Eric: If you want to forgo determining the safeness of a method until you are ready that is fine, but I was referring only to methods deemed safe, in which case there is no exposing.Astor
I
13

I suspect that "it is easier to make the type public later?" is it.

The scoping rules mean that the method will only be visible as internal - so it really doesn't matter whether the methods are marked public or internal.

One possibility that comes to mind is that the class was public and was later changed to internal and the developer didn't bother to change all the method accessibility modifiers.

Ind answered 15/2, 2012 at 22:10 Comment(1)
+1 for nailing the obvious "public class became internal" scenario.Stodder
J
13

I often mark my methods in internal classes public instead of internal as a) it doesn't really matter and b) I use internal to indicate that the method is internal on purpose (there is some reason why I don't want to expose this method in a public class. Therefore, if I have an internal method I really have to understand the reason why it's internal before changing it to public whereas if I am dealing with a public method in an internal class I really have to think about why the class is internal as opposed to why each method is internal.

Jurist answered 15/2, 2012 at 22:18 Comment(2)
Thanks for the answer. I tend to stubbornly insist that "everything" matters when coding, though :)Stodder
You are correct Eric, it was a bit of a throw away comment. Hope that the rest of my answer was of some use. I think Eric Lippert's answer is what I was trying to express but he explained it so much better.Dibb
O
9

In some cases, it may also be that the internal type implements a public interface which would mean that any methods defined on that interface would still need to be declared as public.

Oilstone answered 15/2, 2012 at 22:36 Comment(0)
L
2

It's the same, the public method will be really marked as internal since it's inside a internal class, but it has an advantaje(as you guested), if you want to mark the class as public, you have to change fewer code.

Loveland answered 15/2, 2012 at 22:13 Comment(1)
there is already a related question: #711861Loveland
W
2

For the same reason as using public methods in any other class - so that they're public to the outside of the containing type.

Type's access modifier has exactly zero to do with its members' access modifiers. The two decisions are made completely independently.

Just because certain combinations of type and members' modifiers produce seemingly (or as others call it "effectively") the same result doesn't mean they're semantically the same.

Local access modifier of a an entity (as declared in code) and its global effective access level (as evaluated through the chain of containment) are completely different things, too. An open office inside of a locked building is still open, even though you can't really enter it from the street.

Don't think of the end effect. Think of what you need locally, first.

  • Public's Public: classic situation.
  • Public's Internal: type is public but you want some semi-legal access in the assembly to do some hacky-wacky stuff.
  • Internal's Public: you hide the whole type but within the assembly it has a classic public surface
  • Internal's Internal: I can't think of any real world example. Perhaps something soon to become public's internal?

Internal's Public vs Internal's Internal is a false dilemma. The two have completely different meaning and should be used each in their own set of situations, non-overlapping.

Wellheeled answered 28/9, 2021 at 12:29 Comment(1)
Yet another way to think about this. I got what I needed from the accepted answer, but this is useful, too. "Think of what you need locally, first." Roger that.Stodder
G
1

It's not relevant for your example, but for classes where interfaces being implemented in C# up to 7.3 inclusive it is:

internal interface IAmOnlyInternalViewable
{
    void CallMeOnlyInsideAssembly();
    //internal void CallMeOnlyInsideAssembly();  // <-- is not possible up to C# 7.3 inclusive
}

internal class OnlyInternalVisible : IAmOnlyInternalViewable
{
    public void CallMeOnlyInsideAssembly() { } //Must be public to implement the interface
}

So if you want to have an internal class, which implements an interface, the implemented methods need to be public.

Gertudegerty answered 28/6, 2023 at 9:37 Comment(0)
I
1

I'm a bit late to the party, but thought I would give my two cents.

I always lean towards making everything as restricted as possible until there is a valid need to open things up. I'm not a believer in taking shortcuts just because it may or may not make my life ever so slightly easier in the future. Following this guideline I would never make a method of an internal class public unless forced to due to inheritance. There is otherwise no point into making it public at that point in time.

If by chance you do convert an internal class to a public class, that conversion does not automatically mean you want your methods to be public. Instead the better practice is to go through and explicitly decide which methods should be public. Otherwise you are liable to lazily just make everything public, and not realize it was a mistake until someone comes complaining about some behavior or abuse of that class.

Now with that said, I do think it is valid to preemptively to go through that process, and consider which methods would be public if the class was public. This is fine, but it is a process that needs to be strictly enforced, which is often more difficult than just keeping everything internal until it actually needs to be public.

The gist of my advice, and all programming advice I will ever give, is don't be a lazy programmer. The majority of issues you run into with code will be the result of laziness. In this case, not being lazy means keeping access as restricted as possible until there is an actual need for it to be more open.

Italic answered 6/2 at 15:54 Comment(1)
I liked your comments. People shouldn't write code as they may save some time later or try to find it easy. There are tons of way to do that.Unlade
B
0

internal says the member can only be accessed from within the same assembly. Other classes in that assembly can access the internal public member, but would not be able to access a private or protected member, internal or not.

Biflagellate answered 15/2, 2012 at 22:10 Comment(3)
But if methods were marked internal instead of public, their visibility wouldn't change. I believe that's what the OP is asking about.Ind
@zapthedingbat - the post is factually correct, but is it actually answering the question?Ind
Right, the question is WHY mark them that way. I understand the basics of scope. Thanks for the attempt, though!Stodder
S
0

I actually struggled with this today. Until now I would have said that methods should all be marked with internal if the class was internal and would have considered anything else simply bad coding or laziness, specially in enterprise development; however, I had to sub class a public class and override one of it's methods:

internal class SslStreamEx : System.Net.Security.SslStream
{
    public override void Close()
    {
        try
        {
            // Send close_notify manually
        }
        finally
        {
            base.Close();
        }
    }
}

The method MUST be public and it got me thinking that there's really no logical point to setting methods as internal unless they really must be, as Eric Lippert said.

Until now I've never really stopped to think about it, I just accepted it, but after reading Eric's post it really got me thinking and after a lot of deliberating it makes a lot of sense.

Sophomore answered 26/5, 2015 at 15:26 Comment(0)
H
0

There does be a difference. In our project we have made a lot of classes internal, but we do unit test in another assembly and in our assembly info we used InternalsVisibleTo to allow the UnitTest assembly to call the internal classes. I've noticed if internal class has an internal constructor we are not able to create instance using Activator.CreateInstance in the unit test assembly for some reason. But if we change the constructor to public but class is still internal, it works fine. But I guess this is a very rare case (Like Eric said in the original post: Reflection).

Haun answered 13/6, 2017 at 3:58 Comment(0)
I
0

I think I have an additional opinion on this. At first, I was wondering about how it makes sense to declare something to public in an internal class. Then I have ended up here, reading that it could be good if you later decide to change the class to public. True. So, a pattern formed in my mind: If it does not change the current behavior, then be permissive, and allow things that does not makes sense (and does not hurt) in the current state of code, but later it would, if you change the declaration of the class.

Like this:

public sealed class MyCurrentlySealedClass
{
    protected void MyCurretlyPrivateMethod()
    {
    }
}

According to the "pattern" I have mentioned above, this should be perfectly fine. It follows the same idea. It behaves as a private method, since you can not inherit the class. But if you delete the sealed constraint, it is still valid: the inherited classes can see this method, which is absolutely what I wanted to achieve. But you get a warning: CS0628, or CA1047. Both of them is about do not declare protected members in a sealed class. Moreover, I have found full agreement, about that it is senseless: 'Protected member in sealed class' warning (a singleton class)

So after this warning and the discussion linked, I have decided to make everything internal or less, in an internal class, because it conforms more that kind of thinking, and we don't mix different "patterns".

Inappetence answered 19/4, 2018 at 11:6 Comment(1)
I drastically prefer to do it the way (or at least for the reasons) Eric Lippert said. His article is definitely worth a read.Stodder

© 2022 - 2024 — McMap. All rights reserved.