What is assembly in C# in the context of internal access modifier?

1.3k views Asked by At

I thought that assembly is a project in a solution in Visual Studio, but then I was trying to understand inheritance and internal/protected access modifiers and I got lost.
*Project1 and Project2 are separate VS projects in one solution:

namespace Project1
{
    public class C1
    {
        internal int field1;
        internal protected int field2;
    }
}


namespace Project2
{
    public class C2
    {
        public static void f(C1 c1)
        {
            c1.field1 = 1;  //no problems here
            c1.field2 = 2;  //neither here
        }
    }
}


In Project2 I have no inheritance so both fields field1 and field2 should be inaccessible but there's no errors at all. (I added Project1 into Project2 References and add using Project1 in Project2)

2

There are 2 answers

5
CodeCaster On BEST ANSWER

Your solution works, because it contains errors that Visual Studio can ignore.

You have a fully functional project Project1, containing a Main(). This is the project that is set as startup project, which can run because it contains no errors.

Not all project in your solution have to successfully build in order for one project to run; just the projects that the project you want to run depends on. In this case none.

Right-click Project2, click "Set as StartUp Project" and try to build or run it: you'll see the compiler errors you're expecting.

2
Scott Nimrod On

Internal is an access modifier in which the class, method, or property is exposed to any class that is also embedded within the same assembly.

However classes, methods, or properties that are declared internal are NOT exposed to classes belonging to other assemblies.