0% found this document useful (0 votes)
64 views12 pages

CS Access Modifiers SN 27.09.21

This document discusses C# access modifiers. It explains that access modifiers determine the scope and lifetime of variables, methods, and other elements. There are four main access modifiers in C#: private, public, protected, and internal. Each modifier determines how accessible elements are within and between classes and namespaces. Examples are provided to demonstrate the usage of each access modifier.

Uploaded by

Jayapritha N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views12 pages

CS Access Modifiers SN 27.09.21

This document discusses C# access modifiers. It explains that access modifiers determine the scope and lifetime of variables, methods, and other elements. There are four main access modifiers in C#: private, public, protected, and internal. Each modifier determines how accessible elements are within and between classes and namespaces. Examples are provided to demonstrate the usage of each access modifier.

Uploaded by

Jayapritha N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

| C# Tutorial – Access Modifiers

C# ACCESS MODIFIERS

ACCESS MODIFIERS

 It is used to decide the scope of various programming elements like


variables, methods, constructor, properties etc,…
 It gives a scope and life time to object properties (instance variable
and method) and class properties (static variable and method)
 It is used to access the state (e.g. variable) and behavior (e.g. method)
of objects and class
SCOPE

 Indicates the availability of variables within the program


 Variables have two types of the scopes
 Local scope
 Global scope
TYPES OF MODIFIERS

1. Private 2. Public 3. Protected 4. Internal

1. PRIVATE MODIFIER

 It can be accessible only within a same class


 Private data can’t be accessible in outside of a class
 Like java, it is a default modifier to variables, methods and properties
in c#
 It gives global scope to both the variables (instance variable, static
variable) and methods (instance method, static method, constructor,
properties, etc..) in a same class and local scopes to both variables
and methods in a same namespace.

C#.NET (Unit 1) Page 1


| C# Tutorial – Access Modifiers

I. EXAMPLE OF PRIVATE MODIFIER

1. SOURCE CODE

using k=System.Console;
// instance class definition Private Instance Variable
class PrivateM
{
// private variable definition
private int j = 99;
// instance method
public void info()
{
k.WriteLine("j= "+j);
}
// main() method is placed in SAME CLASS
static void Main()
{
// object creation and calling default constructor
PrivateM obj = new PrivateM();
k.WriteLine("---------------------------------------");
k.WriteLine("\tC# Private Modifier ");
k.WriteLine("---------------------------------------");
// calling instance via object
obj.info(); Object Creation
}
}
2. OUTPUT

C#.NET (Unit 1) Page 2


| C# Tutorial – Access Modifiers

DRAWBACKS

 It is not possible to access the private variables in outside of class


and outside of same namespace or other namespace.

2. PUBLIC MODIFIER

 It is accessible anywhere in the programming (same namespace or


other namespace)

 It gives global scope to both the variables (instance variable, static


variable) and the methods (instance method, static method,
constructor, properties, etc..,) in a same namespace or other
namespace

II. EXAMPLE OF PUBLIC MODIFIER


(Accessing public data in outside of a class and inside of a namespace)

1. SOURCE CODE
using k = System.Console;
// user defined namespace ‘AccModifiers’
namespace AccModifiers
{
class Person
{ Container Class
public string name;
public int id;
}
class PublicM1
{
Person obj = new Person();
// Accessing public variables in outside of a class
void input(string s, int i)
{

C#.NET (Unit 1) Page 3


| C# Tutorial – Access Modifiers

obj.name = s;
obj.id = i;
}
void details()
{
k.WriteLine("Name\t: "+obj.name);
k.WriteLine("Id\t: " + obj.id);
}
static void Main()
{
k.WriteLine("-------------------------------------------------------");
k.WriteLine("\t\tC# Public Modifier ");
k.WriteLine("\tAccessing Public Modifier in Outside of
class,\n\tInside of Same Namespace ");
k.WriteLine("-------------------------------------------------------");
// object creation
PublicM1 pm = new PublicM1();
// calling 2 instance methods
pm.input("Surya",99);
pm.details();
k.WriteLine("-------------------------------------------------------");
}
}
}

C#.NET (Unit 1) Page 4


| C# Tutorial – Access Modifiers

2. OUTPUT

III. EXAMPLE OF PUBLIC MODIFIER


(Accessing public data in outside of a class and outside of a namespace)
1. SOURCE CODE

using k=System.Console; First Namespace


// user defined namespace 'pack'
namespace pack
{
class Test
{
public string name;
public int id;
public void input(string n, int i)
{
name = n;
id = i;
} Second Namespace
}
}
// user defined namespace 'AccModifiers'
namespace AccModifiers
{

C#.NET (Unit 1) Page 5


| C# Tutorial – Access Modifiers

class PublicM2
{
static void Main()
{
k.WriteLine("----------------------------------------------------------------");
k.WriteLine("\t\tC# Public Modifier II ");
k.WriteLine("\tAccessing Public Modifier in Outside of
class,\n\tOutside of Same Namespace ");
k.WriteLine("----------------------------------------------------------------");
// object creation for Test class
pack.Test tt = new pack.Test();
// accessing the public variables in outside of a namespace
// set the values of public variables in outside of a namespace
tt.input("Ganesh",201);
// print the details public variables in outside of a namespace
k.WriteLine("Name\t: " + tt.name);
k.WriteLine("Id\t: " + tt.id);
k.WriteLine("----------------------------------------------------------------");
}
}
}

2. OUTPUT

C#.NET (Unit 1) Page 6


| C# Tutorial – Access Modifiers

3. PROTECTED MODIFIER
 It is accessible only within a same class as well as in sub class (if
inheritance is involved)
 It is same as private modifier
 It gives global scope to the variables (instance variable, static
variable) and the methods (instance method, static method,
constructor, properties, etc..,) in a same class.
 It gives global scope to both variables and methods in a same
namespace (if inheritance is involved)
 It provides partial security to the application
IV. EXAMPLE OF PROTECTED MODIFIER

1. SOURCE CODE

using k = System.Console; Protected Instance Variable


class ProtectedM
{
// protect variable declaration
protected int i = 191;
static void Main()
{
// object creation & calling defult constructor
ProtectedM obj = new ProtectedM();
k.WriteLine("-------------------------------------------------------");
k.WriteLine("\tC# Protected Modifier\n\tAccessing protected
variable\n\tonly within a same class");
k.WriteLine("-------------------------------------------------------");
// calling protected variable via object
k.WriteLine("i= " + obj.i);
k.WriteLine("-------------------------------------------------------");
}
}

C#.NET (Unit 1) Page 7


| C# Tutorial – Access Modifiers

2. OUTPUT

DRAWBACKS

 It is not possible to access the protected variable in outside of


class and outside of same namespace or other namespace.

4. INTERNAL MODIFIER

 It is accessible anywhere in a same namespace


 It gives public scope (like public modifier) only to same namespace
not other namespace
 It is not possible to access the internal variable in outside of same
namespace
 It gives global scope to both variables and methods in same
namespace

C#.NET (Unit 1) Page 8


| C# Tutorial – Access Modifiers

V. EXAMPLE OF INTERNAL MODIFIER

1. SOURCE CODE

using System;
using k = System.Console;
namespace AccModifiers // user defined namespace
{
class A // complete class
{
internal int i = 283;
}
class B // B is container class
{
A obj = new A();
public void set(int a)
{ Internal Instance Variable
obj.i = a;
}
}
class C // C is also container class
{
A obj = new A();
public void disp()
{
k.WriteLine("i= " + obj.i);
}
}
class InternalM
{
static void Main()
{
k.WriteLine("-----------------------------------------------------");
k.WriteLine("\tC# Internal Modifier\n\tAccessing internal
variable\n\tanywhere in the current namespace");
C#.NET (Unit 1) Page 9
| C# Tutorial – Access Modifiers

k.WriteLine("----------------------------------------------------");
B b1 = new B();
C c1 = new C();
// Accessing internal variable in class 'B' (Assignment)
b1.set(306);
// Accessing internal variable in class 'C' (Calling)
c1.disp();
k.WriteLine("----------------------------------------------------");
}
}

Container Class
 If a class contains an object of another class, then it is called as
container class

EXAMPLE
class A // Component or Control class
{

}
class B // Container class or Holder class
{
// object creation for class A
A obj =new A();

}

C#.NET (Unit 1) Page 10


| C# Tutorial – Access Modifiers

2. OUTPUT

DRAWBACKS

 It is not possible to access the internal variable in outside of same


namespace.
VISIBILITY OF FIELDS IN A CLASS
S. Modifier Sam Sub class Other Sub class Other
N s e in the class in in the class in
class same the same other the other
namespac namespac namespac namespac
e e e e

1. Private Yes No No No No

2. Protecte Yes Yes No No No


d

3. Internal Yes Yes Yes Yes No

4. Public Yes Yes Yes Yes Yes

C#.NET (Unit 1) Page 11


| C# Tutorial – Access Modifiers

DEFAULT MODIFIERS OF VARIOUS ELEMENTS


S.N PROGRAMMING ELEMENTS DEFAULT MODIFIERS

1. namespace - (it does not support modifiers)

2. class, structure, enum, internal


interface, delegate

3. class members (variables, private


methods, constructor,
destructor, properties, etc,..)

C#.NET (Unit 1) Page 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy