Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What are the level of the access modifier used in the dot net ?
Posted

Access modifiers are an integral part of object-oriented programming.

They support the concept of encapsulation, which promotes the idea of hiding functionality.

Access modifiers allow you to define who does or doesn't have access to certain features.

  • public
    -No restrictions to access
    -The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • protected
    -Access is limited to within the class definition and any class that inherits from the class.
    -The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
  • internal
    -Access is limited exclusively to classes defined within the current project assembly.
    -The type or member can be accessed only by code in the same class or struct.
  • private
    -Access is limited to within the class definition; This is the default access modifier type if none is formally specified.
    -The type or member can be accessed by any code in the same assembly, but not from another assembly.


Refer:
Access Modifiers[^]

This can be a good start for you:
Access Modifiers (C# Reference)[^]
 
Share this answer
 
Comments
Rahul Rajat Singh 21-Aug-12 6:03am    
+5.
Prasad_Kulkarni 21-Aug-12 6:05am    
Thank you Rahul!
bbirajdar 21-Aug-12 8:11am    
Prasad.. Not every person runs after the reputation points like you. It takes me absolutely minimal efforts to google and copy-paste the answers and earn the reputation points. But posting the answers to the questions like this (which can be solved by googling) will encourage the users to post more questions like this one. So I don't answer but I post comments. Of course its my own strategy and I don't expect other to follow it. And neither should you decide what others should comment/answer .. If you think my comments are not appropriate , you should 'report' it to the CP instead of deleting it. CP has already provided you with the necessary tools/APIs for it. Read the point 9 from
here
Manas Bhardwaj 21-Aug-12 9:13am    
Nice +5
Prasad_Kulkarni 22-Aug-12 0:03am    
Thank you Manas!
XML
There are 5 types access modifiers in dotnet, Those are :-

Private = Accessible only within class
protected = Accessible within the class as well as within child class
internal = Accessible within the same project
protected internal = Accessible within project and child class out side project
public = Accessible every where


try this link also
C# Access Modifiers Quick Reference[^]
 
Share this answer
 
v2
public : The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private: The type or member can be accessed only by code in the same class or struct.
protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal: The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

Check MSDN link.
http://msdn.microsoft.com/en-us/library/ms173121.aspx[^]

There is always msdn if you want to know some new functions, concepts with examples in both Vb,c#
 
Share this answer
 
Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features.
In C# there are 5 different types of Access Modifiers.

1.Public
The public keyword is an access modifier for types and type members. Public access is the most permissive access level.There are no restrictions on accessing public members.

Accessibility:

Can be accessed by objects of the class
Can be accessed by derived classes

2.Private

Private access is the least permissive access level.
Private members are accessible only within the body of the class or the struct in which they are declared.
Accessibility:
Cannot be accessed by object
Cannot be accessed by derived classes

3.protected

A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.
Accessibility:
Cannot be accessed by object
By derived classes

4.internal

The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll).
In other words, access is limited exclusively to classes defined within the current project assembly.
Accessibility:
In same assembly (public)
Can be accessed by objects of the class
Can be accessed by derived classes
In other assembly (internal)
Cannot be accessed by object
Cannot be accessed by derived classes



5.protected internal
The protected internal accessibility means protected OR internal, not protected AND internal.
In other words, a protected internal member is accessible from any class in the same assembly, including derived classes.
The protected internal access modifier seems to be a confusing but is a union of protected and internal in terms of providing access but not restricting. It allows:
Inherited types, even though they belong to a different assembly, have access to the protected internal members.
Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.



Reference[^]
 
Share this answer
 
Comments
Prasad_Kulkarni 22-Aug-12 6:08am    
Not every person runs after the reputation points like you.
Really?

It takes me absolutely minimal efforts to google and copy-paste the answers and earn the reputation points.
..and you did it here.

But posting the answers to the questions like this (which can be solved by googling) will encourage the users to post more questions like this one. So I don't answer but I post comments.
A pure lie, as you answered same question on which you have given this huge comment to me.

Of course its my own strategy
To underestimate others for their answers & after a day to answer same question

and I don't expect other to follow it.
I will NOT, for sure.

And neither should you decide what others should comment/answer ..
Better you apply this on yourself, don't tell me what to do, where to answer..
It's always better to wear shoes than to carpet the world.

A comment on country was the only thing I was talking about. Rather than commenting Is google broken in your country you can comment is google broken at your place or your side which would be far more better.

That's it!
bbirajdar 22-Aug-12 6:12am    
Thats it.. I caught you.. You were silently deleting my comments like a coward..... And I wanted you to revert back... So I deliberately posted an alternative to your solution.. And there voila... you came back red and blue....

My strategy is still the same. If I wanted to earn the reputation points I would have posted the solutions to a number of questions since yesterday.. But I posted specifically for this one.. :D
Prasad_Kulkarni 22-Aug-12 7:11am    
I din't deleted any of your comments. I wonder who have deleted even my comments.
bbirajdar 22-Aug-12 7:15am    
Ok.. I take your statement as guaranteed and stopping my argument from the moment..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900