Vous êtes sur la page 1sur 3

Access Specifiers These are also called modifiers used to define the scope of a type as well as their members

i.e. who can access them & who cant. C Sharp supports 5 Access Specifiers, those are : ! pri"ate #! $nternal %! protected &! protected internal 5! public 'ote: (embers that are defined in a type )i.e. class * structure! with any scope or specifiers are always accessible within the type. +estrictions comes into picture only when we try to access them outside of the type. (embers declared as pri"ate under a class or structure cant be accessed outside of the type but can be accessed only in which they are defined and moreo"er the default scope is pri"ate only. Types cant be declared as pri"ate. So, pri"ate can be used only with members. 'ote: $nterface cant be contain any pri"ate members and default scope for interface members is public. (embers declared as protected under a class can be accessed only within the class or in the child class, non-child classes cant consume them. Types cant be declared as protected also. So, these can also be used on members. (embers and types that are declared as internal can be consumed only with in the pro,ect both from a child or non-child. The default scope for any type in c- is internal only. (embers declared as protected internal will ha"e dual scope i.e. within the pro,ect they beha"e as pro"idin. access to anywhere in the pro,ect, outside the pro,ect they will chan.e to protected and still pro"ides access to their child classes. Types cant be declared as protected internal also, so this can also be used only onm members. A type or member of a type if declared as public is .lobal in scope which can be accessed from anywhere.

private: -

protected: -

Internal:

protected internal:

public: -

Case Case Case Case Case

: #: %: &: 5:

Accessin. Accessin. Accessin. Accessin. Accessin.

members members members members members

with with with with with

in in in in in

the same class or structure child class of same pro,ect a non-child class of same pro,ect. a child class of another pro,ect. a non-child class of another pro,ect.

/! 0ow to restrict a class not to be accessible for any other class to consume1 A! This can be done by declarin. them all the class constructors as pri"ate. /! 0ow to restrict a class not to be inherited for any other class1 A! This can be done by declarin. the class as sealed. /! 0ow to restrict a class not to be accessible for any other class to consume by creatin. its ob,ect1 A! This can be done by declarin. all the class constructors as protected. 23ample:4pen 5S .o to 6ile (enu Select new 7ro,ect Select 8an.ua.e as C- choose console A77 7ro,ect template, name the pro,ect as Access9emo and rename the solution as (ySolution. - :y default the pro,ect comes with the class pro.ram, write the followin. code in it by ma;in. the class as public. public class 7ro.ram < pri"ate "oid Test ) ! < Console.=rite8ine)>7ri"ate (ethod>!? @ internal "oid Test#) ! < Console.=rite8ine)>$nternal (ethod>!? @ protected "oid Test%) ! < Console.=rite8ine)>7rotected (ethod>!? @ protected internal "oid Test&) ! < Console.=rite8ine)>7rotected $nternal (ethod>!? @ public "oid Test5) ! < Console.=rite8ine)>7ublic (ethod>!? @ static "oid (ain)strin.AB ar.s! < 7ro.ram p C new 7ro.ram)!? p.Test )!? p.Test#)!? p.Test%)!? p.Test&)!? p.Test5)!? @ @ - Add a new class Two.cs in the same pro,ect & write the followin. code: class Two : 7ro.ram <

static "oid (ain) ! < Two t C new Two)!? t.Test#)!? t.Test%)!? t.Test&)!? t.Test5)!? Console.+ead8ine)!? @ @ Add a new class Three.cs in the same pro,ect & write the followin. code: class Three < static "oid (ain) ! < 7ro.ram p C new 7ro.ram)!? p.Test#)!? p.Test&)!? p.Test5)!? Console.+ead8ine)!? @ @ Add a new 7ro,ect under the solution of type Console Application, name it as Access9emo#, rename the default file 7ro.ram.cs as 6our.cs so that the class name also chan.es as 6our, now add the reference of Access9emo assembly from its physical location to Access9emo# pro,ect & then write the followin. code: class 6our : Access9emo .7ro.ram < static "oid (ain)strin.AB ar.s! < 6our f C new 6our)!? f.Test%)!? f.Test&)!? f.Test5)!? Console.+ead8ine)!? @ @ Add a new class under Access9emo# pro,ect, namin. it as 6i"e.cs and write the followin. code: class 6i"e < static "oid (ain) ! < 7ro.ram p C new 7ro.ram)!? p.Test5)!? Console.+ead8ine)!? @ @

Vous aimerez peut-être aussi