c sharp

C# Protected

Access Modifiers are keywords in C# used to specify an accessibility level for all classes, class members, and data types. These are mostly used to prevent unintended data changes by third-party programs or classes. Access modifiers are used to protect members who aren’t ready for public use from being abused. Developers value access modifiers because they simplify reading and writing code. Of all the access modifiers, the protected access modifier is one of the most significant. In this article, we will discuss the fundamental access modifier that is a “protected” modifier. “Protected” is a keyword in C# that restricts access to members of a class.

What Is the Protected Modifier in C# in Ubuntu 20.04?

The protected modifier lies between the private and public access modifier domains. It’s similar to private, but it lets derived classes access the member. Other classes and objects can’t see the protected modifier member variables and functions. All child classes have access to this sort of variable or function. To share useful features to derived classes, we employ protected keywords.

Syntax of the Protected Modifier in C# in Ubuntu 20.04:

The protected access modifier has the following syntax:

Public class Class_Name

   {

      protected class Child_Class

   {

//child class body

  }

}

The previous implementation of the protected modifier is used in the C# programming language. We have to use protected keywords with the class or with the member of the class. We can only declare a class as protected in nested classes. It is not possible to specify it within a namespace. Protected access modifier can be used in three different ways in C#:

  1. Protected
  2. Protected internal
  3. Private protected

How to Use the Protected Modifier in C# in Ubuntu 20.04?

We use the keyword protected in the C# program to define a class as protected. When we want to provide parents access to their children’s properties, we usually utilize the protected keyword. Let’s get started on defining the protected keyword in C#. Take a look at the examples provided below to understand how to use the protected modifier in the other combination. For all of the executions, we utilized Ubuntu 20.04.

Example # 1: Using the Protected Access Modifier in C# in Ubuntu 20.04

We can use the protected access modifier to limit access to variables and methods to program in the same class or struct or a derived class. The protected Modifier allows classes that extend the class to access its members, regardless of the assembly. The following example uses a derived class type to access a base class’s protected members in a derived class:

The previous program has the namespace “Program1”, and, in the block of the namespace, we have defined the class of name “Employee”. The class has required variables created with the keyword “protected”. The first protected variable has a type string defined with the name “EmpName”. The second protected variable is assigned the name “EmpDept”, and the third protected variable is assigned the “EmpAge” name. Then the class “Employee” method is created in the class block and given the name “GetEmployeeDetails”.

We have set the class method with the protected access modifier. In the block of the protected method, we have used the writeLine method, which will print the values of the protected variables used in the protected method. We have another class, “SetInfo”, defined as the child class of the parent class “Employee”. In the child class, we use the protected members of the parent class “Employee” by referencing it with the child class “SetInfo”. The parent class “Employee” object “e” is defined as invoked by the parent class. The object for the child class “SetInfo” is also created as “s”. The protected variables are initialized with the values in the child class. The protected method of the parent class is called the child class and gets the values of the variables.

The outcome of using the protected modifiers in our C# program is shown on the following screen that shows restricted access to types or members of the same or child class based on our needs:

Example # 2: Using the Internal Access Modifier in C# in Ubuntu 20.04

The internal modifier specifies that access is restricted to the existing assembly in the C# programming language. Any program in the same assembly can access the type or member, but not from another assembly.

The previous program has the namespace defined as “program2” in which we have created a class “ComplexNumber” with the keyword internal. The internal class has two variable declarations as “real_number” and “img_number” of type integer. The internal class has also declared a public method as “SetValues” and created a constructor in which we have passed two objects as “r” and “i”. Then, we have another public method represented with the name “showValues”, where we have printed the variables “real_number” and “img_number”. After this, the derived class is declared as “AssignValues” within the same assembly where we instantiate the class “ComplexNumber” in object “c”. The “SetValues” and “showValues” methods are accessible in this derived class.

Upon execution of the previous C# program, we have received the following result:

Example # 3: Using the Protected Internal Access Modifier in C# in Ubuntu 20.04

The protected internal modifier restricts access to the current assembly or types inherited from the contained class.

The public class is defined as “ParentClass” in the previous program. We declared a variable “value” as a protected internal of type integer in the class. Then, a child class is created, which inherits the parent class’s property. We’ve generated an object called “obj2” for the child class. Using an object reference “obj2” to the parent class, we are accessing the variables and methods in the child class.

The output is shown on the following terminal screen of the previous program:

Example # 4: Using the Protected Private Access Modifier in C# in Ubuntu 20.04

In the private protected modifier, the current assembly’s enclosing class and its derived types have access. Here’s an example of declaring members with the private protected modifier:

The base class is created as “Order” in the previous program. In the base class, we have declared two variables of the private protected modifier. The two variables are represented by “Item” and “Location”. The method is also created with the private protected keyword and represents it with the name “GetOrderDetails”. The private protected method has the writeLine method in which we have set the previous two variables. Then, we have a derived class “SetOrder” in which we are accessing the base class member through the reference of the derived class object “s”.

The execution of the previous program has the following results:

Conclusion

Finally, in the C# Protected modifier article, we have gone over the basics of being protected in C# and some illustrations of how to use protected access modifiers. The protected modifier specifies how other kinds in a C# program, such as derived types, can access a type and its members. We have different examples of using protected modifiers and the internal and private modifiers with the protected keyword. Furthermore, the Protected access modifier allows us to maintain a high level of information concealment while maintaining a rich object structure.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.