Updated March 20, 2023
Introduction to Protected in C#
In this article, we will see how protected in c# can be implemented in detail. With the help of access modifiers we can restrict the accessibility level of parameters and classes. There are following access modifiers in C#
- Public
- Private
- Protected
- Internal
Protected Modifiers in C#
In c#, we can use the protected modifier to specify that the access is limited to the containing type. Also, we can use it for the types derived from the containing class. The word protected means it can be accessible or visible to itself and also to the derived classes.
With the help of this member or type only be accessed by code which is used in the same class or which is used in the derived class. The keyword protected is between the private and public modifiers. It is almost the same as a private modifier but it allows the member to access the derived classes. We mostly use the protected keyword when we want to give access to children’s properties to their parents. So we can reuse the logic with the help of protected keyword.
Example:
using System;
class Test
{
protected int _x; private int _y;
}
class Test1 : Test
{
public Test1 ()
{
// In this we can access the variable protected int but we cannot access private int variable
Console.WriteLine(this._x);
}
}
class Program
{
static void Main()
{ Test1 b = new Test1 (); }
}
Consider 2 classes, Test and Test1. Class Test1 is derived from Test. If we look inside the class Test, we can see two int field has been declared. 1 protected and 1 private.
In class B Test1 we can access the protected int, but we cannot access the private int. So the protected modifier gives us additional access in the derived class. So with the help of protected keyword, we can access the protected fields includes all the derived classes.
A class can also be protected. Below is the example of how to declare it
Syntax:
public class Test
{
protected class Child
{
}
}
Only in nested class, we can declare the class as protected. We cannot define it inside a namespace.
Examples to Implement Protected in C#
Below are the examples to show how we can implement protected in C#:
Example #1 – Without Implementing Child Class
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class demo
{
// String Variable declared as protected
protected string name;
public void print()
{
Console.WriteLine("\name is " + name);
}
}
class Program
{
static void Main(string[] args) // main method
{
demo d = new demo();
Console.Write("Enter your name:\t");
d.name = Console.ReadLine();
d.print();
Console.ReadLine();
}
}
}
In the above example, the string is declared as protected. This program will raise an error because protected will hide its members from other classes. So it will only accessible in child class.
Example #2 – Implementing with Inheritance
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class Demo
{
protected string name = "Protected Keyword";
protected void Display(string val)
{
Console.WriteLine("This is " + val);
}
}
class Program : Demo // inheritance
{
static void Main(string[] args)
{
Program program = new Program();
// Accessing protected variable
Console.WriteLine("This is " + program.name);
// Accessing protected function
program.Display("protected example");
Console.ReadLine();
}
}
}
In the above example, Parent class consists of protected members. Protected is used to declare the string. Now child class is derived from a parent class and the concept of inheritance is used to access the protected members.
Output:
Example #3
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class Parent
{
private String Private = "My name is John"; // string declared as private
protected String Protected = "My name is Dan"; // string declared as protected
}
class Child : Parent // inheritance
{
public void Show()
{
Console.WriteLine(Protected);
}
}
class Program
{
static int Main(string[] args) // main method
{
Child child = new Child(); // child object
child.Show();
Console.ReadKey();
return 0;
}
}
}
In the above example, the parent class contains private and protected strings. The child class is derived from the parent class. Show() can’t access private though but it can access protected. A child class object is used to call the method. Protected is used to protect the members from being accessed outside of class.
Output:
We can also declare the constructor as protected. So by declaring any constructor as protected, we can call it from a subclass.
Syntax:
public class TEst : Test1
{
public Test() : base() // here we can Call the protected base constructor
{
}
}
We cannot call a protected method. We can call the protected constructor from the derived class.
Protected Internal Access Modifier
With the help of protected internal, we can specify that the access is limited to current types that are derived from the containing classes. So this makes insure that the member and type can be accessed by code in the same class or by the derived class which is written in another assembly.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class Demo
{
protected internal string name; // variable is declared as protected internal
public void print()
{
Console.WriteLine("name is " + name);
}
}
class Program
{
static void Main(string[] args) // main method
{
Demo d = new Demo();
Console.Write("Enter your name:\t");
// Accepting value in protected internal variable
d.name = Console.ReadLine();
d.print();
Console.ReadLine();
}
}
}
Output:
Importance of Protected in C#
Protected keyword is useful because this type of variable can be accessed by the code which is used in the same class. It is useful when we want to give authority to child class so that it can access parent class members. So in that sense, it is important in achieving code reusability.
Conclusion
So we can use protected with variables and access them using the inheritance concept. It can be used where members can be accessed by the class itself or subclass.
Recommended Articles
This is a guide to Protected in C#. Here we discuss the introduction and various examples to implement protected in C# along with protected access modifiers. You may also look at the following articles to learn more –