Complete the code to declare a protected method named ShowMessage.
protected void [1]() { Console.WriteLine("Hello from protected method!"); }
The method name must be ShowMessage as declared.
Complete the code to allow the derived class to access the base class's protected method.
class BaseClass { protected void [1]() { Console.WriteLine("Base class method"); } } class DerivedClass : BaseClass { public void CallBase() { [1](); } }
The protected method name is BaseMethod, which the derived class can call.
Fix the error in accessing the protected member from outside the class hierarchy.
class MyClass { protected int [1] = 10; } class OtherClass { public void Access() { MyClass obj = new MyClass(); Console.WriteLine(obj.[1]); } }
The protected member is named secret. However, it cannot be accessed from OtherClass because it is not derived from MyClass.
This code will cause a compile error because protected members are only accessible within the class and its subclasses.
Fill both blanks to correctly override a protected method in the derived class.
class Parent { protected virtual void [1]() { Console.WriteLine("Parent method"); } } class Child : Parent { protected override void [2]() { Console.WriteLine("Child method"); } }
The method name must be the same in both parent and child classes to override correctly. Here, Show is used.
Fill all three blanks to create a protected property with a getter and setter in a class.
class Sample { protected [1] [2] { get; [3]; } }
The property is of type int, named Value, with a getter and setter. The setter keyword is needed to allow setting the property.