Click here to Skip to main content
15,891,859 members
Articles / Programming Languages / C#

Learning Type Access Modifiers Basics !!!

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
30 Jul 2010CPOL 6K   1  
Learning Type Access Modifiers Basics !!!

When I started developing my module, I had an interface IParamCountBasedAlgo declared as a nested type in a class AlgorithmOneExecutor, declared as follows:

C#
namespace DataStructuresAndAlgo
{
   partial class AlgorithmOneExecutor
   {
      private interface IParamCountBasedAlgo
      {
         void Validate();
         void Execute();
      }
   }
}

There were other concrete nested types inside AlgorithmOneExecutor that implemented IParamCountBasedAlgo. But later, other types nested in other than AlgorithmOneExecutor emerged that required to implement IParamCountBasedAlgo. So I moved IParamCountBasedAlgo from a nested type to a direct type under the namespace DataStructuresAndAlgo, as declared below:

C#
namespace DataStructuresAndAlgo
{
 private interface IParamCountBasedAlgo
 {
   void Validate();
   void Execute();
 }
}

And the compiler spat an error "Namespace elements cannot be explicitly declared as private, protected, or protected internal". Then a simple research gave me an insight that types directly under namespace can be declared either public or internal only, and the default is internal. Seems reasonable because if declared private, it gives an ambiguous look that it cannot be accessed or created and protected seems rather very unrelated. Hence either public or internal only.

A subtle point to note is that not all access modifiers are applicable for all types and at all declaration levels. To learn the basics of type access modifiers, visit http://msdn2.microsoft.com/en-us/library/ms173121.aspx.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --