-4

I have Created one ConsoleApplication to understand Access Specifiers.

Below is my code for internal, I can access this class from outside the Assembly.

namespace Assembly_1 //This is first assembly.
{       
   public class Base
   {
       //internal class
       internal class B
       {
          public static void fnB()
          {
            Console.WriteLine("fnB");
          }
       }        
   }    
}

namespace Assembly_2 //This is second assembly.
{   
    public class Derived : Assembly_1.Base
    {            
        public class D
        {
            public void fnD()
            {
                B.fnB();//how can I access this class?
            }
        }
    }
}

And this is where I am Accessing it.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {                
            Assembly_2.Derived.D d = new Assembly_2.Derived.D();
            d.fnD();
        }
    }
}

My Question

Right now I can Access Class B and it's methods like fnB() in Derived.

Everything works fine. but How?

How can I access the B Class outside Assembly_1?

Bharat
  • 5,869
  • 4
  • 38
  • 58
  • *Right now I can Access Class B and it's methods like fnB() in Derived* Did you mean to say that you **can't** access them? – DavidG Apr 20 '17 at 12:25
  • 3
    If you want to access it from outside its assembly, why did you mark it internal in the first place? – CodeCaster Apr 20 '17 at 12:25
  • 1
    You can't that's the point of access modifiers, make it public if you want to use it outside of this project – EpicKip Apr 20 '17 at 12:25
  • @DavidG : no, I can access it, but i don't know why? – Bharat Apr 20 '17 at 12:27
  • 3
    Might it be that you are confusing namespace and assembly terms? – Ofir Winegarten Apr 20 '17 at 12:28
  • If its just the two assemblies and you need to share internals (for whatever reasons - i agree to @EpicKip) just use the InternalsVisibleToAttribute on first assembly. https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx – Digvijay Apr 20 '17 at 12:29
  • @DavidG - i totally agree as in my comment. But just wanted to present that there is a way built in framework for such scenario! – Digvijay Apr 20 '17 at 12:31
  • please see the question first and then reply, i can access B.fnB(). and i dont know why? and why all are downgrading it without understanding it properly? – Bharat Apr 20 '17 at 12:32
  • 1
    @Bharat as in my comment, if you can access it, it means that both namespaces are within the same assembly. Create another class library project and put there the Base classs. then add a reference to it from the main console project – Ofir Winegarten Apr 20 '17 at 12:33
  • You say you created *one* console application. How exactly do you have two assemblies in one application? When I take your code and put it in two separate assemblies, it causes a compile-time error ("Base.B is inaccessible due to its protection level") as expected. – Luaan Apr 20 '17 at 12:34
  • @OfirWinegarten, Yes i am focused with your comment and testing it in my code, but meanwhile I got 2 downgrades, this peoples dont read the question carefully.. – Bharat Apr 20 '17 at 12:35
  • @Bharat don't worry, focus on the code not the downvotes :) – Ofir Winegarten Apr 20 '17 at 12:37
  • Yes, it's not a duplicate of the question CodeCaster selected. But it's still a poor question; in part because of the confusing language (e.g. CodeCaster answers your question "How can I access ...", rather than what your *intended question* is, which seems to be "*Why* can I access ..."), in part because the problem cannot be reproduced (most likely due to your confusion about what an assembly is). – Luaan Apr 20 '17 at 12:37
  • @Luaan wait for updates, I am running it in my system, and it's working – Bharat Apr 20 '17 at 12:38
  • @CodeCaster : please remove the duplicate tag, and read the question again. – Bharat Apr 20 '17 at 12:46
  • @OfirWinegarten my friend, I have not mistaken anywhere, can you please check it again.. – Bharat Apr 20 '17 at 12:49
  • That edit didn't really help anything. How is the console application relevant? Are you expecting that an `internal` member that is accessed in a `public` member should turn that public member into an internal member? Why? How? Are you also expecting that as long as there's a public member accessing a private member, it will make another class unable to use the public member? – Luaan Apr 20 '17 at 12:50
  • @Bharat how many projects do you have in the solution? – Ofir Winegarten Apr 20 '17 at 12:51
  • @OfirWinegarten and #luaan, lots off, and it's just for R&D, but here it's so simple, i can run it and printing the console of B.fnB() – Bharat Apr 20 '17 at 12:52
  • 2
    Are you certain that `namespace Assembly_1` is indeed defined on a different project? i'm quite certain its not. It is in the same project(assembly) as `namespace Assembly_2` – Ofir Winegarten Apr 20 '17 at 12:54
  • @Luaan now I am also confused because of your comment, is internal member become public if I access it from public method? I don't know at all and that's why i asked.. – Bharat Apr 20 '17 at 12:54
  • @OfirWinegarten you are right, it's in a same project with different namespace.. – Bharat Apr 20 '17 at 12:55
  • You never access `B.fnB` in the console project, only `D.fnD`. `D.fnD` is public, and it doesn't matter what it calls internally. It would be entirely useless if it did. – Luaan Apr 20 '17 at 12:55
  • 2
    So, @Bharat read about what is the difference between an assembly and a namespace in google – Ofir Winegarten Apr 20 '17 at 12:57
  • @Luaan, ok but because of the protection level, i must not access the B.fnB() from function fnD(), am I right? – Bharat Apr 20 '17 at 12:57
  • @OfirWinegarten I think you are right, I need to check for assembly and a namespace, finally I got my answer..all are just in hurry for getting rewards..you can add this as an answer, .. maybe it will help other users like me.. – Bharat Apr 20 '17 at 12:59
  • 1
    _"all are just in hurry for getting rewards"_ - no, this site is meant to be a collection of clear, unique questions that are helpful for others, not just the asker. Unclear questions should be closed until they are clarified. – CodeCaster Apr 20 '17 at 13:01
  • @CodeCaster my friend, initially you have not read the question at all, and just make it as duplicate?? – Bharat Apr 20 '17 at 13:02
  • 2
    "Might it be that you are confusing namespace and assembly terms?" was Ofir's comment just a few minutes after you asked your question. It's been reiterated multiple times during this "conversation". – Luaan Apr 20 '17 at 13:02
  • 1
    I can assure you I read the initial question, but its language, along with David's first comment, led me to believe you were asking how you could make this code work. – CodeCaster Apr 20 '17 at 13:06

2 Answers2

3

As I worte in the comments:

You are confusing the namespace and assembly terms. You can read about it here:(Assemblies and Namespace)

Many namespaces can be defined in a single assembly.

If you would like to check and understand the internal modifier, then you would have to create a new class library project (that will compile into a different assembly), define the Base class there and add a reference to it in your main console application. Then you will see that you don't have access to it anymore and the code will not compile.

Community
  • 1
  • 1
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
1

How can I access the B Class outside Assembly_1?

Because you're confusing namespaces and assemblies. An assembly is a collection of one or more namespaces, contained within a .dll or .exe file.

See also: MSDN: Assemblies in the Common Language Runtime and Understanding and Using Assemblies and Namespaces in .NET.

What you call Assembly_1 and Assembly_2 are namespaces within the same assembly.

Because internal members are visible within the same assembly, you can use Assembly_1.B from Assembly_2.D, because both namespaces reside in the same assembly.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • sorry @CodeCaster, but Ofir stands with me in all discussion, so he should get the extra points, thank you too.. – Bharat Apr 20 '17 at 13:18
  • 1
    @Bharat I really couldn't care less about reputation points, that's why I posted this as a community wiki. – CodeCaster Apr 20 '17 at 13:20