Click here to Skip to main content
12,251,027 members (39,146 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET C#4.0 .NET4
what is access modifiers in c# .net?

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 10-Jul-12 1:45am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

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#
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:

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
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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)[^]
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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.
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

Public : access anywhere in the class and also outside class.
Private: access only class scope and also access in derived class/child class if it properties have public.
Protected: access in same class and inherit class also.

You can also visit my blog Click here
  Permalink  
v3
Comments
Andreas Gieriet 4-Feb-14 6:01am
   
My downvote for this: This is not answering the question (what about internal?) and what you write is confusing (your description on private is wrong).
Such information does not invite to visit your blog (do we have to expect more of such incorrect info there?)...
Andi

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 25 Nov 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100