In this lesson, you will learn about the different types of visibility and access modifiers in C# and how/when to use them.

C# Visibility

Visibility or access modifier allows us to define the way a class, method, variable, or a property is seen and accessed. Encapsulation is one of the uses of a private modifier, where you don’t want properties of method from a class to be used from outside that class. C# let you define the accessibility level so it allows you to open or restrict access to members as needed by your program.

Access Modifiers types in C#

There are five access modifiers in C#:

  • public: an object can be assessed without any restrictions.
  • private: an object is private and can only be accessed within the class definition. In C#, private is the default access modifier.
  • protected: an object can be assessed within the class definition and any other class that inherits from that class
  • internal: an object cannot be access from outside the current project assembly.
  • protected internal: an object can only be accessed within the current assembly and types derived from the containing class.
        public class ProgrammingTutorials
        {
            public void Show()
            {

                Console.Write("TutorialsPanel.com");

            }

        }


        static void Main(string[] args)
        {
            ProgrammingTutorials _Tutorial = new ProgrammingTutorials();

            _Tutorial.Show();

            Console.ReadKey();

        }

The method Show() can be assessed from anywhere.

Example of private modifier in C#

public class ProgrammingTutorials
        {
            private void Show()
            {

                Console.Write("TutorialsPanel.com");

            }

        }

        static void Main(string[] args)
        {
            ProgrammingTutorials _Tutorial = new ProgrammingTutorials();

            _Tutorial.Show();

            Console.ReadKey();

        }

The code above will generate the following error:

Error 1 ‘CSharpConsoleApp.Abs.Vehicle.ProgrammingTutorials.Show()’ is inaccessible due to its protection level

In conclusion, private members can only be accessed within the same class.

Example of protected modifier in C#

    class A
        {
            //Protected variable
            protected string Name;
        }

        class B : A
        {
            B()
            {
                this.Name = "TutorialsPanel.com"; // can access from this derived class
            }
        }

        class C
        {
            C()
            {
                this.Name = "TutorialsPanel"; // cannot access from this class
            }
        }

In the example above, the protected variable “Name” that is defined in class A, can be accessed from derived class B, but not from the class C. The error below will occur when trying to access the variable from class C:

Error 1 ‘CSharpConsoleApp.Abs.MainClass.C’ does not contain a definition for ‘Name’ and no extension method ‘Name’ accepting a first argument of type ‘CSharpConsoleApp.Abs.MainClass.C’ could be found (are you missing a using directive or an assembly reference?)

Example of internal modifier in C#

  class TutorialsPanel
        {
            internal void InternalMethod()
            {
                // your code goes here
            }
        }
        // access the method from the main method
        class Program
        {
            static void Main(string[] args)
            {
                TutorialsPanel _instance = new TutorialsPanel();

                _instance.InternalMethod(); // valid code to access.
            }
        }

The method above can be accessed from anywhere within the assembly, but not from other assemblies. internal identifier limits access from the assembly which improves the security and encapsulate methods that need to remain private within their assembly.

Example of protected internal modifier in C#

 class TutorialsPanel
        {
            protected internal string name;  
            public void Show()
            {
                Console.WriteLine("Welcome to " + name);
            }
        }
        // to access the method
        class Program
        {
            static void Main(string[] args)
            {
                TutorialsPanel _instance = new  TutorialsPanel();

                _instance.name = "TutorialsPanel.com";
                _instance.Show();
            }
        }

In the example above, the function member “name” can be accessed from anywhere in the assembly, as well as from derived class in another assembly. 

 

Last modified: January 22, 2019

Comments

Write a Reply or Comment

Your email address will not be published.