Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of a public member of an internal class?

Tags:

c#

class

internal

For example:

internal class C
{
    public void M()
    {
        Console.WriteLine("foo");
    }
}

To me, that reads "a method that can be accessed by anyone, regardless of assembly living inside a class that can only be accessed from code in the same assembly".

My experience with the compiler tells me that if I do something like that and do not get a warning, there is probably a valid reason to have it.

So, I suppose either

  1. Something is lacking in my understanding of protection levels.
  2. There could be a warning but there isn't one.

(if 2, this is not an attempt to complain about it - I just want to understand)

like image 332
Aaron Anodide Avatar asked Apr 30 '12 21:04

Aaron Anodide


People also ask

Can internal class have public members?

Class, record, and struct member accessibility However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

Is an internal class public?

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.

What is the difference between public and internal?

Internal is only available within the assembly it resides in. Public is available to any assembly referencing the one it resides in. If you can access the internal class from another assembly you either have "InternalsVisibleTo" set up, or you're not referencing the class you think you are.

What are internal members?

Internal members are listed as part of the Company Directory, can be added to multiple workspaces at one time, and can view items marked "Internal." Internal Members are generally people within your company. External Members are generally customers, clients, partners, vendors, etc.


2 Answers

To me, that reads "a method that can be accessed by anyone, regardless of assembly living inside a class that can only be accessed from code in the same assembly".

To me that means "the accessibility domain of C is restricted to this assembly; the accessibility domain of M is the unrestricted subset of the accessibility domain of its container, C".

"public" means that to me because that's what the specification says it means. I encourage you to read the portion of the specification which covers accessibility domains if you have questions or concerns about this.

like image 199
Eric Lippert Avatar answered Sep 23 '22 11:09

Eric Lippert


See this SO Question for a detailed answer on how it functions.

In my experience, I like to mark internal members public in anticipation of a future time when I want to change the scope of the class to public. This way I can do that and all the previously marked internal methods are automatically public.

like image 34
Steve Danner Avatar answered Sep 23 '22 11:09

Steve Danner