Hello friends,
In this article we will learn, c# public private protected access modifiers, private member can only be accessed by code in the same class, public member can be accessed by any other code in the same assembly or another assembly that references it and protected member can only be accessed by code in the same class or in a derived class.
1) private access modifier
The type or member can only be accessed by code in the same class.
namespace Application
{
public class BaseClass
{
//by default private
int a;
}
class Program
{
static void Main()
{
BaseClass b = new BaseClass();
System.Console.WriteLine(b.a);
System.Console.ReadKey();
}
}
}
Above code will show compile time error.
Error CS0122 'BaseClass.a' is inaccessible due to protection level.
2) public access modifiers
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
namespace Application
{
public class BaseClass
{
public int a;
//Default Constructor
public BaseClass()
{
a = 10;
}
//Parametarized Constructor overloading
public BaseClass(int a)
{
this.a = a;
}
//Class member function
public int BaseMethod => a;
}
class Program
{
static void Main()
{
BaseClass b = new BaseClass();
System.Console.WriteLine(b.a);
b = new BaseClass(20);
System.Console.WriteLine(b.a);
System.Console.ReadKey();
}
}
}
3) protected access modifier
The type or member can only be accessed by code in the same class or in a derived class.
Code with public access modifier.
namespace Application
{
public class BaseClass
{
public int a;
//Default Constructor
public BaseClass()
{
a = 10;
}
//public method
public int BaseClassMethod() => a;
}
// Single level Inheritance
public class DerivedClassOfBaseClass:BaseClass
{
// Returns base class method
public int DerivedClassMethod() => BaseClassMethod();
}
class Program
{
static void Main()
{
int i = 0;
//Base class object
BaseClass b = new BaseClass();
//Invoke Baseclass method
i = b.BaseClassMethod();
System.Console.WriteLine(i);
//Derived class object
DerivedClassOfBaseClass d = new DerivedClassOfBaseClass();
//Invoke baseclass method by derived class
i = d.BaseClassMethod();
System.Console.WriteLine(i);
//Invoke derivedclass method by derived class
i = d.DerivedClassMethod();
System.Console.WriteLine(i);
System.Console.ReadKey();
}
}
}
Now replace
public int BaseClassMethod() => a;
with
protected int BaseClassMethod() => a;
Following will work fine because BaseClassMethod() is accessible with in inheritance scope.
// Single level Inheritance
public class DerivedClassOfBaseClass:BaseClass
{
public int DerivedClassMethod() => BaseClassMethod();
}
Following two line will throw exception at compile time due to restriction of method out side inheritance.
i = b.BaseClassMethod();
i = d.BaseClassMethod();
Error CS0122 'BaseClass.BaseClassMethod()' is inaccessible due to protection level.
Error CS0122 'BaseClass.BaseClassMethod()' is inaccessible due to protection level.
If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.
Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.