Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Protected in C#

Two possibilities:
1. Protected member that is in the same class.
2. We can access the 'Protected' member only if the class inherits from a class from where the member is defined.


Case 1:
  1. using System;
  2. using System.IO;
  3.  
  4. namespace forgetCode
  5. {
  6. class program1
  7. {
  8. protected string name = "Forget Code";
  9.  
  10.  
  11. static void Main(string[] args)
  12. {
  13.  
  14. program1 obj = new program1();
  15. Console.WriteLine(obj.name);
  16.  
  17.  
  18. }
  19. }
  20. }


Output:
Forget Code


Case 2:
  1. using System;
  2. using System.IO;
  3.  
  4. namespace forgetCode
  5. {
  6. class program1
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.WriteLine("Example for Protected :");
  11. program3 obj = new program3();
  12.  
  13. }
  14. }
  15.  
  16. class program2
  17. {
  18. protected string name = "Forget Code";
  19. }
  20.  
  21. class program3:program2
  22. {
  23. public program3()
  24. {
  25. Console.WriteLine(name);
  26. }
  27. }
  28. }
  29.  


Output:
Example for Protected :
Forget Code

..