Access modifiers in C#

Posted on Updated on

Image result for C#Access modifiers in C#

Modifier modify the declarations of types and type members.Some of the modifiers used in C# are-
1) Access Modifiers
2) abstract
3) async
4) const
5) event
6) extern
7) new
8) override
9) partial
10) readonly
11) sealed
12) static
13) unsafe
14) virtual
15) volatile

1) Access Modifiers: Access modifiers are defined to give the permission to access the properties and methods.There are four types of access modifiers-

a) public: This keyword is used for data members and member functions. If the member of the class is declared bas public it can be accessed from outside of the class where it belengs and also from outside of the assembly.

class ClassTest
{
//Public method
public void MethodPublic()
{
// defination of MethodPublic
}
}

// to access the method
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();

objClassTest.MethodPublic(); // valid code to access.
}
}

b) private: tis keyword is member access modifier.It restricts the member variables and member methods within the class.If a constructor of a class is declared as private, the object of that class can’t be created i.e. that class can’t be accessed from outside of that class.

Example 1

class ClassTest
{
//Private method
private void MethodPrivate()
{
// defination of MethodPrivate
}
}
// to access the method
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();

objClassTest.MethodPrivate(); // invalid code to access.
}
}

objClassTest.MethodPrivate(); //invalid code to access.

OUTPUT
======

void ClassTest.MethodPrivate()

Error:
‘ConsoleApplication2.ClassTest.MethodPrivate()’ is inaccessible due to its protection level

Example 2 Private Constructor

class ClassTest
{
private ClassTest() { } // private constructor
}
// to access the method
class Program
{
static void Main(string[] args)
{
// invalid code. can’t create an object of this class
ClassTest objClassTest = new ClassTest();
}
}

c) Protected: This keyword allows the class and subclass to access the member variables and methods i.e. the members can be accessed by that class and the class that inherits it. Struct members don’t support protected access modifier because struct members can’t be inherited.

Example 1:

The protected member of a base class can be accessed in the derived class only, if the access is through the derived class.

class A
{
protected int x = 123;
}

         class B : A
{
static void Main()
{
A a = new A();
B b = new B();

// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;

// OK, because this class derives from A.
b.x = 10;
}
}

Example 2:

class Point
{
protected int x;
protected int y;
}

class DerivedPoint: Point
{
static void Main()
{
DerivedPoint dpoint = new DerivedPoint();

// Direct access to protected members:
dpoint.x = 10;
dpoint.y = 15;
Console.WriteLine(“x = {0}, y = {1}”, dpoint.x, dpoint.y);
}
}
// Output: x = 10, y = 15

d) Internal: This access modifier is introduced in C#. Members of internal access modifier can be accessed within its assembly i.e. as Protected, it also allows to access the methods and variables from that class and the sub classes of that class. It added the assembly into it.

class ClassTest
{
internal void MethodInternal()
{
// do your code
}
}
// to access the method
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();

         objClassTest.MethodInternal(); // valid code to access.
}
}

Protected Internal:

Protected Internal allows to access the variables and methods to access from that class and sub classes of that class within the same assembly. This means in protected, if the class is inheriting the super class and the method or variable is protected, then the assembly doesn’t matter to access. But in the Internal, the assembly matters if the class is inheriting the super class. That is why we use Protected Internal access modifier.

Example 1

There are two files, Assembly1.cs and Assembly1_a.cs. The first file contains an internal base class, BaseClass. In the second file, an attempt to instantiate BaseClass will produce an error.

// Assembly1.cs
// Compile with: /target:library
internal class BaseClass
{
public static int intM = 0;
}

// Assembly1_a.cs
// Compile with: /reference:Assembly1.dll
class TestAccess
{
static void Main()
{
BaseClass myBase = new BaseClass(); // CS0122
}
}

Example 2

In this example, the same files are used as in example 1, and only the accessibility level of BaseClass is changed to public. Also the accessibility level of the member IntM is changed to internal. In this case, instantiate the class, but one cannot access the internal member.

// Assembly2.cs
// Compile with: /target:library
public class BaseClass
{
internal static int intM = 0;
}

// Assembly2_a.cs
// Compile with: /reference:Assembly1.dll
public class TestAccess
{
static void Main()
{
BaseClass myBase = new BaseClass(); // Ok.
BaseClass.intM = 444; // CS0117
}
}

]

+
6300000000000000000000000000

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s