Click here to Skip to main content
15,881,594 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
what is access modifiers in c# .net?

C#
public :
private :
protected :
internal :
protected internal :


where I can use this all?
what is difference between them?
Give me live example for that.

i am fresher so give answer in easy language.

Gaurang Raval.
Fresher (trainee)
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[^]
 
Share this answer
 
Comments
Abhinav S 10-Jul-12 8:29am    
5!
Prasad_Kulkarni 10-Jul-12 9:01am    
Thank you Abhinav!
Manas Bhardwaj 10-Jul-12 8:33am    
+5!
Prasad_Kulkarni 10-Jul-12 9:01am    
Thank you Manas!
I think the best bet for you is to pick up a good book and then read about modifiers.
These would be described in detail in the initial chapters.
 
Share this answer
 
Comments
Gaurangraval 10-Jul-12 8:38am    
can u suggest me name of any E book for .net C#
Prasad_Kulkarni 10-Jul-12 9:00am    
Good suggestion +5!
public member:public member can be accessible any where in the project as well as other project also.

private member :private member can be accessible anywhere within a class itself not outside from that class.

Internal member: Internal member can b accessible anywhere whithin a project.

protected member :protected member can be accessible within a class where member declare as well as derrived class also.

protected internal : protected internal member enjoy dual effect.it is accessible anywhere in the prject as well as if that class is inherite one other class and that is in other project then only it can be accessible to other project also.
 
Share this answer
 
This might help full for you
http://www.dotnetbull.com/2013/10/public-protected-private-internal-access-modifier-in-c.html [^]

Accessibility of access modifies in c#

Access Modifiers


Inside Assembly


Outside Assembly


With Inheritance


With Type


With Inheritance


With Type


Public


ü


ü


ü


ü


Private


X


X


X


X


Protected


ü


X


ü


X


Internal


ü


ü


X


X


Protected Internal


ü


ü


ü


X



Reference :-
dotnetbull - what is access modifier in c#
 
Share this answer
 
v4
The big two are public and private and all they do is say how much of you class can be seen by the outside world.
Anything you declare as private can only be accessed within the class itself:

C#
public class MyClass
   {
   private void myFunction(){}
   ...
   myFunction();   // LEGAL, no problem
   ...
   }
...
   MyClass mc = new MyClass();
   mc.MyFunction();   // ILLEGAL - no access

Anything you declare as public can be accessed by any code anywhere.

The others fall in the middle of these two:
protected means that it is only available within the class, and in any class derived from the class. It's private but with special access given to derived classes.

internal means public to classes within the same assembly, and private to classes outside teh assembly. So if you build your own DLL, all your classes in side the DLL can access the objects, but classes which reference your DLL cannot see them.

protected internal is a combination of protected and internal - they are visible within the assembly exactly like internal objects, but they are also visible to derived classes outside, the same as protected
 
Share this answer
 
Comments
Manas Bhardwaj 10-Jul-12 8:33am    
Good +5!
Gaurangraval 10-Jul-12 8:35am    
thank you so much......:-)
Shemeer NS 11-Jul-12 6:08am    
5'ed
Is Google broken at your place. Being a freshers doesn't mean you complile list of questions at CP and wait for cooked answer. You should look for these answers yourself.

This can be a good start for you:

Access Modifiers (C# Reference)[^]
 
Share this answer
 
Comments
Prasad_Kulkarni 10-Jul-12 7:51am    
+5!
Gaurangraval 10-Jul-12 7:52am    
sir, i have seen it but i cant understand it so i have ask this Que. pls give live example and give reply in normal language.

public : Access is not restricted.

protected : Access is limited to the containing class or types derived from the containing class.

Internal : Access is limited to the current assembly.

protected internal : Access is limited to the current assembly or types derived from the containing class.

private : Access is limited to the containing type.
Philippe Mori 25-Nov-14 23:48pm    
You just have to click on each word like public on that page to have an example for that keyword. Not hard at all and much faster that posting a question.
Private members are available only within the containing type, where as public members are available anywhere,there is no restriction.
Protected members are available within the containing type and to the type that derives from the containing type.

Internal is available anywhere within the containing assembly.It is a compile time error to access an internal member from outside the containing assembly
Protected Internal can be accessed by any code in the assembly in which it is declared or from within a derived class in another assembly.It is a combination of internal and protected access modifier.
Assembly in .NET is generated if you compile a project.Assembly is of 2 types-executable and .dll. Console, windows applications generate .exe assembly whereas library and asp.net generate .dll assembly.
Assembly is intermediate code langauge.
 
Share this answer
 
v2
Comments
CHill60 3-Sep-13 12:34pm    
Doesn't add anything to the earlier answers and not strictly accurate - you can attract downvotes by answering old, previously adequately answered questions

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