3

I'm building a DLL with some basic functions. Long story short, I'm making a few static classes for the use by devs. These classes use some other classes that do the dirty work, which I marked as internal because I don't want people to access them.

The question is: If I declare a class as internal, what the access level of his members will be?

I'll have to mark as internal all of its members or they are automatically labeled as internal too?

It's a good 2 hours I'm googling and searching in stackoverflow and I'm struggling to find a clear and straight answer which doesn't include 1000 speculations, technical not-so-probable hypotesis and useless decorations...

MSDN is confusing as usual (never found a clear answer on msdn).

From what I can read here http://msdn.microsoft.com/en-us/library/ms173121.aspx I guess that no matter how you set a class access level, all his members will be private (methods, variables and so on).

Help, I don't know

svick
  • 236,525
  • 50
  • 385
  • 514
  • 2
    You understood correctly: they are private by default. Although I don't see what the fuss is about and why it took you two hours - you can fire up Visual Studio and test it in 2 minutes. – Theodoros Chatzigiannakis Aug 09 '14 at 09:22
  • @FarhadJabiyev:- `where as members of a struct are public by default.` No thats not true they are private by default – Rahul Tripathi Aug 09 '14 at 09:27
  • Just my two Cents: If you just want to make you lib more readable reduce static class usage and only provide a Interface implementation by implementing Abstract Factory / Singleton. Why bother with language specific stuff like internal modifiers. – ovm Aug 09 '14 at 09:53
  • @ovm I would, if I knew what does that even means. TheodorosChatzigiannakis When I test things in visual studio most of the time I fear unexpected results or inaccurate tests so I prefer a biblical reference or an answer by a professional, not mentioning that testing this would have required building 2 separate projects and trying to link one in the other. Too much of an hassle, it's quicker to google it. And here it comes my fear, you tell me one thing, other people tells the opposite... Why this has to happen??? :( – user3924708 Aug 09 '14 at 10:29

5 Answers5

8

The question is: If I declare a class as internal, what the access level of his members will be?

The default will be private. If anything else, then it depends. If they are anything other than public, then the access modifier applies as described on MSDN (e.g. not visible outside of the assembly).

However, in the link you posted, there is one gotcha which applies to non-static classes:

Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. 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.


In relation to the last paragraph, since static classes cannot implement interfaces or inherit other classes then you can rest assured. As long you declare your static class internal, the members will not be available in other assemblies (unless your devs use reflection).

To exemplify how it does work for non-static classes:

Assembly 1

public interface ISomePublicInterface
{
    int GetValue();
}

internal class InternalClass : ISomePublicInterface
{
    public int GetValue()
    {
        return 100;
    }
}

public static class SomeFactory
{
    public static ISomePublicInterface GetInternalInstanceAsInterface()
    {
        return new InternalClass();
    }
}

Assembly 2

ISomePublicInterface val = SomeFactory.GetInternalInstanceAsInterface();
Console.WriteLine(val.GetValue()); //-->> Calls public method in internal class
Console.WriteLine(val.GetType());

Guess what the output is?

Assembly1.InternalClass

So, now you have access to the type outside of the assembly and via reflection someone could call other internal methods (it's not the only way to get it).

Marcel N.
  • 13,726
  • 5
  • 47
  • 72
3

From MSDN only

The access level for class members and struct members, including nested classes and structs, is private by default.


interfaces default to internal access.

Hope this table helps:

Members of    Default member accessibility
----------    ----------------------------
enum          public
class         private
interface     public
struct        private

Also check this MSDN

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Private unless otherwise stated. However, public will have same result as internal.

If you later promote a class from internal to public, then creating public class objects will become visible, whilst internally scoped methods will stay internal.

You might want to consider behaviour in case your class scope gets updated.

Another stack overflow question.

Community
  • 1
  • 1
kidshaw
  • 3,423
  • 2
  • 16
  • 28
1

If u Declare any class as "internal" then its means you can access this class in same assembly. But what kind of access specifier you use for class member is decide they are accessible are not in different class in same assembly.

0

All the members of internal class would be internal and will be accessible with in the same assembly and will not be outside neither class nor members.

If you want to access class in other assemblies, make the class public and member you don't want to be accessed outside assembly make them internal.