0% found this document useful (0 votes)
3 views7 pages

Access Modifiers

The document explains C# access modifiers, which control the accessibility of classes and their members to enhance data security. It covers three main modifiers: public, which allows access throughout the program; private, which restricts access to the defining class only; and protected, which permits access within the class and its derived classes. Examples illustrate the use of these modifiers in practical scenarios.

Uploaded by

nishantnishu964
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)
3 views7 pages

Access Modifiers

The document explains C# access modifiers, which control the accessibility of classes and their members to enhance data security. It covers three main modifiers: public, which allows access throughout the program; private, which restricts access to the defining class only; and protected, which permits access within the class and its derived classes. Examples illustrate the use of these modifiers in practical scenarios.

Uploaded by

nishantnishu964
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

C# Access Modifiers


Access Modifiers are keywords that define the accessibility of a member, class
or data type in a program. These are mainly used to restrict unwanted data
manipulation by external programs or classes. These modifiers control the
scope and extent of access to classes, methods, properties, and other
members within and from external classes or assemblies. By using modifiers
we ensure that certain data or functionality is exposed only when necessary
otherwise keep it hidden.

1. Public Modifier
public modifier provides access to the entire program. It means that another
method or another assembly which contains the class reference can access
these members or types. This access modifier has the most permissive access
level in comparison to all other access modifiers.

using System;

class Student
{
public int rollNo;
public string name;

public Student(int r, string n)


{
rollNo = r;
name = n;
}

public int getRollNo()


{
return rollNo;
}

public string getName()


{
return name;
}
}
class person
{
static void Main(string[] args)
{
Student S = new Student(1, "XYZ");

// accessible through another method


[Link]("Displaying using class members");
[Link]("Roll number: {0}", [Link]);
[Link]("Name: {0}", [Link]);

[Link]();

[Link]("Displaying Using methods");


[Link]("Roll number: {0}", [Link]());
[Link]("Name: {0}", [Link]());
}
}

Output
Displaying using class members
Roll number: 1
Name: XYZ

Displaying Using methods


Roll number: 1
Name: XYZ

2. private modifier
private access modifier limits access to members of a class so that only the
class itself can use them. Private members cannot be accessed from outside
the class, not even by derived classes.
Example:

using System;
class Parent
{
private int value;
// value is Accessible
// only inside the class
public void setValue(int v)
{
value = v;
}

public int getValue()


{
return value;
}
}
class Child : Parent
{

public void showValue()


{
// Trying to access value
// Inside a derived class
// [Link]( "Value = " + value );
// Gives an error
}
}

class person
{
static void Main(string[] args)
{
Parent obj = new Parent();

// [Link] = 5;
// Also gives an error

// Use public functions to assign


// and use value of the member 'value'
[Link](4);
[Link]("Value = " +
[Link]());
}
}

Output
Value = 4

3. protected Modifier
protected access modifier allows members to be accessible within the class
and by derived classes (subclasses). It does not allow access from outside the
class or its derived classes.
Example:

using System;

class Student
{
protected string name;

public Student(string studentName)


{
name = studentName;
}
}

class person : Student


using System;

class Student
{
protected string name;

public Student(string studentName)


{
name = studentName; // Initialize name
}
}

class person : Student


{

public person(string x) : base(y)


{

public string GetName()


{
// Access protected member from base class
return name;
}
}

class person
{
static void Main(string[] args)
{

person obj = new person("SDM");

// Access the protected member through the derived class method


[Link]("The student's name is: {0}",
[Link]());
}
}
{

public person(string x) : base(y)


{

public string GetName()


{
// Access protected member from base class
return name;
}
}

class person
{
static void Main(string[] args)
{

person obj = new person("SDM");

// Access the protected member through the derived class method


[Link]("The student's name is: {0}",
[Link]());
}
}

Output
The student's name is: SDM

You might also like