Home > Software > .NET > C-Sharp
Interview Questions   Tutorials   Discussions   

C-Sharp - Difference between keyword internal and protected?




292
views
asked mar October 4, 2014 03:24 AM  

Difference between keyword internal and protected?


           

1 Answers



 
answered By siddhant   0  

Internal

The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace. The variable or classes that are declared with internal can be access by any member within application. It is the default access specifiers for a class in C# programming.

    using System;

namespace Internal_Access_Specifier
{
  class access
   {
     // String Variable declared as internal
     internal string name;
     public void print()
      {
        Console.WriteLine("\nMy name is " + name);
      }
   }

  class Program
   {
     static void Main(string[] args)
      {
        access ac = new access();
        Console.Write("Enter your name:\t");
        // Accepting value in internal variable
        ac.name = Console.ReadLine();
        ac.print();
        Console.ReadLine();
      }
   }
}

Protected

The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.

using System;

namespace Protected_Internal
{
  class access
   {
     // String Variable declared as protected internal
     protected internal string name;
     public void print()
      {
        Console.WriteLine("\nMy name is " + name);
      }
   }


  class Program
   {
     static void Main(string[] args)
      {
        access ac = new access();
        Console.Write("Enter your name:\t");
        // Accepting value in protected internal variable
        ac.name = Console.ReadLine();
        ac.print();
        Console.ReadLine();
      }
   }
}
flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert