Thursday, March 28, 2013

The default accessibly is not internal.

To say "the default accessibly is internal" is a simplification. The default accessibility for a class is internal and the default accessibility for a method is private. Observe:

namespace MvcApplication1.Models
{
   class Foo
   {
      string Bar()
      {
         return "bar";
      }
   }
}

 
 

Now look at this:

namespace MvcApplication1.Models
{
   class Baz
   {
      string Qux()
      {
         Foo foo = new Foo();
         return foo.Bar();
      }
   }
}

 
 

The first line of the Qux method would compile fine but not the second. The word Bar in the Qux method would be given a squiggly red line beneath it by ReSharper. Internal works like public within the assembly/project at hand and like private elsewhere and to outsiders.

No comments:

Post a Comment