Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
Internal Visibility


The namespace N with classes P and I are here:

namespace N{

  public class P{
    internal static int i = 5;
    public static int p = 6;
    private I anI;
  }
  
  internal class I{
    internal static int i = P.i;
    public static int p = P.p; 
  }

}

The class A is here:

using N;
class A {

  public static void M(){
    P aP;
//  I aI;   // Not accessible

//  int x = P.i;  // Not visible outside x.dll
    int y = P.p;

//  int z = I.i;  // Not visible outside x.dll
//  int w = I.p;  // Not visible outside x.dll
  }

}

Here are the answers to the questions:

  1. Can you declare variables of type P in class A? Yes. The class P is public in its assembly.
  2. Can you declare variables of type I in class A? No. The class I is internal in its assembly. Therefore we cannot use I from another assembly.
  3. Can you access P.i and and P.p in A? p is public in P, and therefore it can be used from A. i internal in P, and therefore it cannot be used from another assembly.
  4. Can you access I.i and and I.p in A? No. None of the members in I can be seen outside its assembly. Notice that this is even the case for I.p.

If class A and the classes in the namespace N are compiled together, to a single assembly, all the visibility problems vanish. This compilation can be done with:

  csc /t:library /out:y.dll n.cs aa.cs