What is Protected Access Modifier in C#?Only the
members of a class can be marked with protected access modifier.
When you mark a member to be protected, then it will be accessible within the class and all the derived classes of this class. Consider the following example: public class
sampleClass { In this example, protected member called member1 of sampleClass is accessed directly inside derivedClass which inherits from sampleClass. This is acceptable because protected members will be visible in derived classes of sampleClass. Consider the following code below: public class
testClass { Here you access the protected member inside a class that is not derived from sampleClass. Hence you will end up in the following error: sampleClass.member1 is inaccessible due to protection level
|