Total Pageviews

Thursday, 11 August 2011

Basics of Access Modifiers In C#

Dot Net Framework support four types of Access Modifiers, they are
  1.  Private 
  2. Protected   
  3. Inline 
  4. Public
 Now let’s see them one by One
  1.  Private Modifiers :-
  • Private Members, Variables, classes, and methods are only available within the class.
  • We can’t instantiate Object of the private Class.
  • We cannot create class with private access modifier but we can create class as private which is a internal class of another class, and which is available anywhere in the parent class. 
 Following Example Illustrate you how private works in Inline or Internal Class.

Fig. How Private Class wors in Parent class
     2. Protected Modifiers : -
  • Protected Members, Variables, classes, and methods are only available within the inheritance.

Fig. Error in Calling Explicit protected method







 
  • Class "Employee" inheriting class "Person",  "GetName ()" mothod of "Person" class now available in the class "Employee".



  • Protected can be available outside the assemble also but only in Inheritace chain.
  • we can not create protected class directely, if you want a class to be protected it should be internal class.