In this tutorial, you will learn access modifiers in c# with following topics. They are
- How to use private access modifier in c#
- how to use public access modifier in c#
- how to use protected access modifier in c#
- internal access modifier in c#
ACCESS MODIFIERS
- It is used to decide the scope of various programming elements like variables, methods, constructor, properties etc,…
- It gives a scope and lifetime 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 is a default modifier to variables and methods in c#.
- 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 the same class and local scopes to both variables and methods in the same namespace.
I. EXAMPLE OF PRIVATE MODIFIER
1. SOURCE CODE
2. OUTPUT
DRAWBACKS OF PRIVATE MODIFIER
- It is not possible to access the private variables outside of class and outside of the 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
{
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)
{
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(“——————————“);
}
}
}
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;
// 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;
}
}
}
// user defined namespace ‘AccModifiers’
namespace AccModifiers
{
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
3. PROTECTED MODIFIER
- It is accessible only within a same class as well as in subclass (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 the 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;
class ProtectedM
{
// protect variable declaration
protected int i = 191;
static void Main()
{
// object creation & calling default 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(“————————————“);
}
}
2. OUTPUT
DRAWBACKS OF PROTECTED MODIFIER
- It is not possible to access the protected variable outside of class and outside of the same namespace or other namespace.
4. INTERNAL MODIFIER (DEFAULT MODIFIER)
- It is a default modifier to class, struct, interface, delegate, etc.
- 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 the same namespace.
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)
{
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”);
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();
…
}
2. OUTPUT
DRAWBACKS OF INTERNAL MODIFIER
- It is not possible to access the internal variable outside of the same namespace.
VISIBILITY OF FIELDS IN A CLASS
DEFAULT MODIFIERS OF VARIOUS ELEMENTS
S.N | PROGRAMMING ELEMENTS | DEFAULT MODIFIERS |
1. | namespace | – (it does not support modifiers) |
2. | class, structure, enum, interface, delegate | internal |
3. | class members (variables, methods, constructor, destructor, properties, etc,..) | private |
MORE TUTORIALS
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…